| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef NFTABLES_RULE_H | ||
| 2 | #define NFTABLES_RULE_H | ||
| 3 | |||
| 4 | #include <nftables.h> | ||
| 5 | #include <list.h> | ||
| 6 | #include <netinet/in.h> | ||
| 7 | #include <libnftnl/object.h> /* For NFTNL_CTTIMEOUT_ARRAY_MAX. */ | ||
| 8 | #include <linux/netfilter/nf_tables.h> | ||
| 9 | #include <cache.h> | ||
| 10 | |||
| 11 | /** | ||
| 12 | * struct handle_spec - handle ID | ||
| 13 | * | ||
| 14 | * @location: location this handle was defined at | ||
| 15 | * @id: handle ID value | ||
| 16 | */ | ||
| 17 | struct handle_spec { | ||
| 18 | struct location location; | ||
| 19 | uint64_t id; | ||
| 20 | }; | ||
| 21 | |||
| 22 | /** | ||
| 23 | * struct position_spec - position ID | ||
| 24 | * | ||
| 25 | * @location: location this position was defined at | ||
| 26 | * @id: position ID value | ||
| 27 | */ | ||
| 28 | struct position_spec { | ||
| 29 | struct location location; | ||
| 30 | uint64_t id; | ||
| 31 | }; | ||
| 32 | |||
| 33 | struct table_spec { | ||
| 34 | struct location location; | ||
| 35 | const char *name; | ||
| 36 | }; | ||
| 37 | |||
| 38 | struct chain_spec { | ||
| 39 | struct location location; | ||
| 40 | const char *name; | ||
| 41 | }; | ||
| 42 | |||
| 43 | struct set_spec { | ||
| 44 | struct location location; | ||
| 45 | const char *name; | ||
| 46 | }; | ||
| 47 | |||
| 48 | struct flowtable_spec { | ||
| 49 | struct location location; | ||
| 50 | const char *name; | ||
| 51 | }; | ||
| 52 | |||
| 53 | struct obj_spec { | ||
| 54 | struct location location; | ||
| 55 | const char *name; | ||
| 56 | }; | ||
| 57 | |||
| 58 | /** | ||
| 59 | * struct handle - handle for tables, chains, rules and sets | ||
| 60 | * | ||
| 61 | * @family: protocol family | ||
| 62 | * @table: table name | ||
| 63 | * @chain: chain name (chains and rules only) | ||
| 64 | * @set: set name (sets only) | ||
| 65 | * @obj: stateful object name (stateful object only) | ||
| 66 | * @flowtable: flow table name (flow table only) | ||
| 67 | * @handle: rule handle (rules only) | ||
| 68 | * @position: rule position (rules only) | ||
| 69 | * @set_id: set ID (sets only) | ||
| 70 | */ | ||
| 71 | struct handle { | ||
| 72 | uint32_t family; | ||
| 73 | struct table_spec table; | ||
| 74 | struct chain_spec chain; | ||
| 75 | struct set_spec set; | ||
| 76 | struct obj_spec obj; | ||
| 77 | struct flowtable_spec flowtable; | ||
| 78 | struct handle_spec handle; | ||
| 79 | struct position_spec position; | ||
| 80 | struct position_spec index; | ||
| 81 | uint32_t set_id; | ||
| 82 | uint32_t chain_id; | ||
| 83 | uint32_t rule_id; | ||
| 84 | uint32_t position_id; | ||
| 85 | }; | ||
| 86 | |||
| 87 | extern void handle_merge(struct handle *dst, const struct handle *src); | ||
| 88 | extern void handle_free(struct handle *h); | ||
| 89 | |||
| 90 | /** | ||
| 91 | * struct scope | ||
| 92 | * | ||
| 93 | * @parent: pointer to parent scope | ||
| 94 | * @symbols: symbols bound in the scope | ||
| 95 | */ | ||
| 96 | struct scope { | ||
| 97 | const struct scope *parent; | ||
| 98 | struct list_head symbols; | ||
| 99 | }; | ||
| 100 | |||
| 101 | extern struct scope *scope_alloc(void); | ||
| 102 | extern struct scope *scope_init(struct scope *scope, const struct scope *parent); | ||
| 103 | extern void scope_release(const struct scope *scope); | ||
| 104 | extern void scope_free(struct scope *scope); | ||
| 105 | |||
| 106 | /** | ||
| 107 | * struct symbol | ||
| 108 | * | ||
| 109 | * @list: scope symbol list node | ||
| 110 | * @identifier: identifier | ||
| 111 | * @expr: initializer | ||
| 112 | * @refcnt: reference counter | ||
| 113 | */ | ||
| 114 | struct symbol { | ||
| 115 | struct list_head list; | ||
| 116 | const char *identifier; | ||
| 117 | struct expr *expr; | ||
| 118 | unsigned int refcnt; | ||
| 119 | }; | ||
| 120 | |||
| 121 | extern void symbol_bind(struct scope *scope, const char *identifier, | ||
| 122 | struct expr *expr); | ||
| 123 | extern int symbol_unbind(const struct scope *scope, const char *identifier); | ||
| 124 | extern struct symbol *symbol_lookup(const struct scope *scope, | ||
| 125 | const char *identifier); | ||
| 126 | struct symbol *symbol_lookup_fuzzy(const struct scope *scope, | ||
| 127 | const char *identifier); | ||
| 128 | struct symbol *symbol_get(const struct scope *scope, const char *identifier); | ||
| 129 | |||
| 130 | enum table_flags { | ||
| 131 | TABLE_F_DORMANT = (1 << 0), | ||
| 132 | TABLE_F_OWNER = (1 << 1), | ||
| 133 | TABLE_F_PERSIST = (1 << 2), | ||
| 134 | }; | ||
| 135 | #define TABLE_FLAGS_MAX 3 | ||
| 136 | |||
| 137 | const char *table_flag_name(uint32_t flag); | ||
| 138 | unsigned int parse_table_flag(const char *name); | ||
| 139 | |||
| 140 | /** | ||
| 141 | * struct table - nftables table | ||
| 142 | * | ||
| 143 | * @list: list node | ||
| 144 | * @handle: table handle | ||
| 145 | * @location: location the table was defined at | ||
| 146 | * @chains: chains contained in the table | ||
| 147 | * @sets: sets contained in the table | ||
| 148 | * @objs: stateful objects contained in the table | ||
| 149 | * @flowtables: flow tables contained in the table | ||
| 150 | * @flags: table flags | ||
| 151 | * @refcnt: table reference counter | ||
| 152 | */ | ||
| 153 | struct table { | ||
| 154 | struct list_head list; | ||
| 155 | struct cache_item cache; | ||
| 156 | struct handle handle; | ||
| 157 | struct location location; | ||
| 158 | struct scope scope; | ||
| 159 | struct cache chain_cache; | ||
| 160 | struct cache set_cache; | ||
| 161 | struct cache obj_cache; | ||
| 162 | struct cache ft_cache; | ||
| 163 | struct list_head chains; | ||
| 164 | struct list_head sets; | ||
| 165 | struct list_head objs; | ||
| 166 | struct list_head flowtables; | ||
| 167 | struct list_head chain_bindings; | ||
| 168 | enum table_flags flags; | ||
| 169 | unsigned int refcnt; | ||
| 170 | uint32_t owner; | ||
| 171 | const char *comment; | ||
| 172 | bool has_xt_stmts; | ||
| 173 | bool is_from_future; | ||
| 174 | }; | ||
| 175 | |||
| 176 | extern struct table *table_alloc(void); | ||
| 177 | extern struct table *table_get(struct table *table); | ||
| 178 | extern void table_free(struct table *table); | ||
| 179 | extern struct table *table_lookup_fuzzy(const struct handle *h, | ||
| 180 | const struct nft_cache *cache); | ||
| 181 | |||
| 182 | /** | ||
| 183 | * enum chain_flags - chain flags | ||
| 184 | * | ||
| 185 | * @CHAIN_F_BASECHAIN: chain is a base chain | ||
| 186 | */ | ||
| 187 | enum chain_flags { | ||
| 188 | CHAIN_F_BASECHAIN = 0x1, | ||
| 189 | CHAIN_F_HW_OFFLOAD = 0x2, | ||
| 190 | CHAIN_F_BINDING = 0x4, | ||
| 191 | }; | ||
| 192 | |||
| 193 | /** | ||
| 194 | * enum flowtable_flags - flowtable flags | ||
| 195 | * | ||
| 196 | */ | ||
| 197 | enum flowtable_flags { | ||
| 198 | FLOWTABLE_F_HW_OFFLOAD = 0x1, /* NF_FLOWTABLE_HW_OFFLOAD in linux nf_flow_table.h */ | ||
| 199 | }; | ||
| 200 | |||
| 201 | /** | ||
| 202 | * struct prio_spec - extendend priority specification for mixed | ||
| 203 | * textual/numerical parsing. | ||
| 204 | * | ||
| 205 | * @expr: expr of the standard priority value | ||
| 206 | */ | ||
| 207 | struct prio_spec { | ||
| 208 | struct location loc; | ||
| 209 | struct expr *expr; | ||
| 210 | }; | ||
| 211 | |||
| 212 | struct hook_spec { | ||
| 213 | struct location loc; | ||
| 214 | const char *name; | ||
| 215 | unsigned int num; | ||
| 216 | }; | ||
| 217 | |||
| 218 | struct chain_type_spec { | ||
| 219 | struct location loc; | ||
| 220 | const char *str; | ||
| 221 | }; | ||
| 222 | |||
| 223 | /** | ||
| 224 | * struct chain - nftables chain | ||
| 225 | * | ||
| 226 | * @list: list node in table list | ||
| 227 | * @handle: chain handle | ||
| 228 | * @location: location the chain was defined at | ||
| 229 | * @refcnt: reference counter | ||
| 230 | * @flags: chain flags | ||
| 231 | * @hookstr: unified and human readable hook name (base chains) | ||
| 232 | * @hooknum: hook number (base chains) | ||
| 233 | * @priority: hook priority (base chains) | ||
| 234 | * @policy: default chain policy (base chains) | ||
| 235 | * @type: chain type | ||
| 236 | * @dev: device (if any) | ||
| 237 | * @rules: rules contained in the chain | ||
| 238 | */ | ||
| 239 | struct chain { | ||
| 240 | struct list_head list; | ||
| 241 | struct cache_item cache; | ||
| 242 | struct handle handle; | ||
| 243 | struct location location; | ||
| 244 | unsigned int refcnt; | ||
| 245 | uint32_t flags; | ||
| 246 | const char *comment; | ||
| 247 | struct { | ||
| 248 | struct location loc; | ||
| 249 | struct prio_spec priority; | ||
| 250 | struct hook_spec hook; | ||
| 251 | struct expr *policy; | ||
| 252 | struct chain_type_spec type; | ||
| 253 | const char **dev_array; | ||
| 254 | struct expr *dev_expr; | ||
| 255 | int dev_array_len; | ||
| 256 | }; | ||
| 257 | struct scope scope; | ||
| 258 | struct list_head rules; | ||
| 259 | }; | ||
| 260 | |||
| 261 | #define STD_PRIO_BUFSIZE 100 | ||
| 262 | extern int std_prio_lookup(const char *std_prio_name, int family, int hook); | ||
| 263 | extern const char *chain_hookname_lookup(const char *name); | ||
| 264 | extern struct chain *chain_alloc(void); | ||
| 265 | extern struct chain *chain_get(struct chain *chain); | ||
| 266 | extern void chain_free(struct chain *chain); | ||
| 267 | extern struct chain *chain_lookup_fuzzy(const struct handle *h, | ||
| 268 | const struct nft_cache *cache, | ||
| 269 | const struct table **table); | ||
| 270 | extern struct chain *chain_binding_lookup(const struct table *table, | ||
| 271 | const char *chain_name); | ||
| 272 | |||
| 273 | extern const char *family2str(unsigned int family); | ||
| 274 | #define __NF_ARP_INGRESS 255 | ||
| 275 | extern const char *hooknum2str(unsigned int family, unsigned int hooknum); | ||
| 276 | extern const char *chain_policy2str(uint32_t policy); | ||
| 277 | extern void chain_print_plain(const struct chain *chain, | ||
| 278 | struct output_ctx *octx); | ||
| 279 | extern void chain_rules_print(const struct chain *chain, | ||
| 280 | struct output_ctx *octx, const char *indent); | ||
| 281 | |||
| 282 | /** | ||
| 283 | * struct rule - nftables rule | ||
| 284 | * | ||
| 285 | * @list: list node in chain list | ||
| 286 | * @handle: rule handle | ||
| 287 | * @location: location the rule was defined at | ||
| 288 | * @stmt: list of statements | ||
| 289 | * @num_stmts: number of statements in stmts list | ||
| 290 | * @comment: comment | ||
| 291 | * @refcnt: rule reference counter | ||
| 292 | */ | ||
| 293 | struct rule { | ||
| 294 | struct list_head list; | ||
| 295 | struct handle handle; | ||
| 296 | struct location location; | ||
| 297 | struct list_head stmts; | ||
| 298 | unsigned int num_stmts; | ||
| 299 | const char *comment; | ||
| 300 | unsigned int refcnt; | ||
| 301 | }; | ||
| 302 | |||
| 303 | extern struct rule *rule_alloc(const struct location *loc, | ||
| 304 | const struct handle *h); | ||
| 305 | extern struct rule *rule_get(struct rule *rule); | ||
| 306 | extern void rule_free(struct rule *rule); | ||
| 307 | extern void rule_print(const struct rule *rule, struct output_ctx *octx); | ||
| 308 | extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle); | ||
| 309 | extern struct rule *rule_lookup_by_index(const struct chain *chain, | ||
| 310 | uint64_t index); | ||
| 311 | void rule_stmt_append(struct rule *rule, struct stmt *stmt); | ||
| 312 | void rule_stmt_insert_at(struct rule *rule, struct stmt *nstmt, | ||
| 313 | struct stmt *stmt); | ||
| 314 | |||
| 315 | /** | ||
| 316 | * struct set - nftables set | ||
| 317 | * | ||
| 318 | * @list: table set list node | ||
| 319 | * @handle: set handle | ||
| 320 | * @location: location the set was defined/declared at | ||
| 321 | * @refcnt: reference count | ||
| 322 | * @flags: bitmask of set flags | ||
| 323 | * @gc_int: garbage collection interval | ||
| 324 | * @count: count of kernel-allocated elements | ||
| 325 | * @timeout: default timeout value | ||
| 326 | * @key: key expression (data type, length)) | ||
| 327 | * @data: mapping data expression | ||
| 328 | * @objtype: mapping object type | ||
| 329 | * @existing_set: reference to existing set in the kernel | ||
| 330 | * @init: initializer | ||
| 331 | * @rg_cache: cached range element (left) | ||
| 332 | * @policy: set mechanism policy | ||
| 333 | * @automerge: merge adjacents and overlapping elements, if possible | ||
| 334 | * @comment: comment | ||
| 335 | * @errors: expr evaluation errors seen | ||
| 336 | * @elem_has_comment: element with comment seen (for printing) | ||
| 337 | * @desc.size: count of set elements | ||
| 338 | * @desc.field_len: length of single concatenated fields, bytes | ||
| 339 | * @desc.field_count: count of concatenated fields | ||
| 340 | */ | ||
| 341 | struct set { | ||
| 342 | struct list_head list; | ||
| 343 | struct cache_item cache; | ||
| 344 | struct handle handle; | ||
| 345 | struct location location; | ||
| 346 | unsigned int refcnt; | ||
| 347 | uint32_t flags; | ||
| 348 | uint32_t gc_int; | ||
| 349 | uint32_t count; | ||
| 350 | uint64_t timeout; | ||
| 351 | struct expr *key; | ||
| 352 | struct expr *data; | ||
| 353 | uint32_t objtype; | ||
| 354 | struct set *existing_set; | ||
| 355 | struct expr *init; | ||
| 356 | struct expr *rg_cache; | ||
| 357 | uint32_t policy; | ||
| 358 | struct list_head stmt_list; | ||
| 359 | bool root; | ||
| 360 | bool automerge; | ||
| 361 | bool key_typeof_valid; | ||
| 362 | bool errors; | ||
| 363 | bool elem_has_comment; | ||
| 364 | const char *comment; | ||
| 365 | struct { | ||
| 366 | uint32_t size; | ||
| 367 | uint8_t field_len[NFT_REG32_COUNT]; | ||
| 368 | uint8_t field_count; | ||
| 369 | } desc; | ||
| 370 | }; | ||
| 371 | |||
| 372 | extern struct set *set_alloc(const struct location *loc); | ||
| 373 | extern struct set *set_get(struct set *set); | ||
| 374 | extern void set_free(struct set *set); | ||
| 375 | extern struct set *set_clone(const struct set *set); | ||
| 376 | extern struct set *set_lookup_global(uint32_t family, const char *table, | ||
| 377 | const char *name, struct nft_cache *cache); | ||
| 378 | extern struct set *set_lookup_fuzzy(const char *set_name, | ||
| 379 | const struct nft_cache *cache, | ||
| 380 | const struct table **table); | ||
| 381 | extern const char *set_policy2str(uint32_t policy); | ||
| 382 | extern void set_print(const struct set *set, struct output_ctx *octx); | ||
| 383 | extern void set_print_plain(const struct set *s, struct output_ctx *octx); | ||
| 384 | |||
| 385 | 9624957 | static inline bool set_is_datamap(uint32_t set_flags) | |
| 386 | { | ||
| 387 | 9624957 | return set_flags & NFT_SET_MAP; | |
| 388 | } | ||
| 389 | |||
| 390 | 9385495 | static inline bool set_is_objmap(uint32_t set_flags) | |
| 391 | { | ||
| 392 | 9385495 | return set_flags & NFT_SET_OBJECT; | |
| 393 | } | ||
| 394 | |||
| 395 | 183445 | static inline bool set_is_map(uint32_t set_flags) | |
| 396 | { | ||
| 397 |
4/4✓ Branch 0 (3→4) taken 10104 times.
✓ Branch 1 (3→6) taken 173341 times.
✓ Branch 2 (5→6) taken 429 times.
✓ Branch 3 (5→7) taken 9675 times.
|
183445 | return set_is_datamap(set_flags) || set_is_objmap(set_flags); |
| 398 | } | ||
| 399 | |||
| 400 | 1506725 | static inline bool set_is_anonymous(uint32_t set_flags) | |
| 401 | { | ||
| 402 | 1506725 | return set_flags & NFT_SET_ANONYMOUS; | |
| 403 | } | ||
| 404 | |||
| 405 | 852 | static inline bool set_is_literal(uint32_t set_flags) | |
| 406 | { | ||
| 407 |
4/4✓ Branch 0 (3→4) taken 847 times.
✓ Branch 1 (3→7) taken 5 times.
✓ Branch 2 (5→6) taken 846 times.
✓ Branch 3 (5→7) taken 1 times.
|
852 | return !(set_is_anonymous(set_flags) || set_is_map(set_flags)); |
| 408 | } | ||
| 409 | |||
| 410 | 235 | static inline bool map_is_literal(uint32_t set_flags) | |
| 411 | { | ||
| 412 |
4/4✓ Branch 0 (3→4) taken 233 times.
✓ Branch 1 (3→7) taken 2 times.
✓ Branch 2 (5→6) taken 231 times.
✓ Branch 3 (5→7) taken 2 times.
|
235 | return !(set_is_anonymous(set_flags) || !set_is_map(set_flags)); |
| 413 | } | ||
| 414 | |||
| 415 | 12421 | static inline bool set_is_meter(uint32_t set_flags) | |
| 416 | { | ||
| 417 |
3/4✓ Branch 0 (3→4) taken 1740 times.
✓ Branch 1 (3→6) taken 10681 times.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 1740 times.
|
12421 | return set_is_anonymous(set_flags) && (set_flags & NFT_SET_EVAL); |
| 418 | } | ||
| 419 | |||
| 420 | 6 | static inline bool set_is_meter_compat(uint32_t set_flags) | |
| 421 | { | ||
| 422 | 6 | return set_flags & NFT_SET_EVAL; | |
| 423 | } | ||
| 424 | |||
| 425 | 33545 | static inline bool set_is_interval(uint32_t set_flags) | |
| 426 | { | ||
| 427 | 33545 | return set_flags & NFT_SET_INTERVAL; | |
| 428 | } | ||
| 429 | |||
| 430 | 1414612 | static inline bool set_is_non_concat_range(const struct set *s) | |
| 431 | { | ||
| 432 |
4/4✓ Branch 0 (2→3) taken 714997 times.
✓ Branch 1 (2→5) taken 699615 times.
✓ Branch 2 (3→4) taken 693412 times.
✓ Branch 3 (3→5) taken 21585 times.
|
1414612 | return (s->flags & NFT_SET_INTERVAL) && s->desc.field_count <= 1; |
| 433 | } | ||
| 434 | |||
| 435 | #include <statement.h> | ||
| 436 | |||
| 437 | struct counter { | ||
| 438 | uint64_t packets; | ||
| 439 | uint64_t bytes; | ||
| 440 | }; | ||
| 441 | |||
| 442 | struct quota { | ||
| 443 | uint64_t bytes; | ||
| 444 | uint64_t used; | ||
| 445 | uint32_t flags; | ||
| 446 | }; | ||
| 447 | |||
| 448 | struct ct_helper { | ||
| 449 | char name[16]; | ||
| 450 | uint16_t l3proto; | ||
| 451 | uint8_t l4proto; | ||
| 452 | }; | ||
| 453 | |||
| 454 | struct timeout_state { | ||
| 455 | struct list_head head; | ||
| 456 | struct location location; | ||
| 457 | uint8_t timeout_index; | ||
| 458 | const char *timeout_str; | ||
| 459 | unsigned int timeout_value; | ||
| 460 | }; | ||
| 461 | |||
| 462 | struct ct_timeout { | ||
| 463 | uint16_t l3proto; | ||
| 464 | uint8_t l4proto; | ||
| 465 | uint32_t timeout[NFTNL_CTTIMEOUT_ARRAY_MAX]; | ||
| 466 | struct list_head timeout_list; | ||
| 467 | }; | ||
| 468 | |||
| 469 | struct ct_expect { | ||
| 470 | uint16_t l3proto; | ||
| 471 | uint8_t l4proto; | ||
| 472 | uint16_t dport; | ||
| 473 | uint32_t timeout; | ||
| 474 | uint8_t size; | ||
| 475 | }; | ||
| 476 | |||
| 477 | struct limit { | ||
| 478 | uint64_t rate; | ||
| 479 | uint64_t unit; | ||
| 480 | uint32_t burst; | ||
| 481 | uint32_t type; | ||
| 482 | uint32_t flags; | ||
| 483 | }; | ||
| 484 | |||
| 485 | struct synproxy { | ||
| 486 | uint16_t mss; | ||
| 487 | uint8_t wscale; | ||
| 488 | uint32_t flags; | ||
| 489 | }; | ||
| 490 | |||
| 491 | struct secmark { | ||
| 492 | char ctx[NFT_SECMARK_CTX_MAXLEN]; | ||
| 493 | }; | ||
| 494 | |||
| 495 | enum tunnel_type { | ||
| 496 | TUNNEL_UNSPEC = 0, | ||
| 497 | TUNNEL_ERSPAN, | ||
| 498 | TUNNEL_VXLAN, | ||
| 499 | TUNNEL_GENEVE, | ||
| 500 | }; | ||
| 501 | |||
| 502 | struct tunnel_geneve { | ||
| 503 | struct list_head list; | ||
| 504 | uint16_t geneve_class; | ||
| 505 | uint8_t type; | ||
| 506 | uint8_t data[NFTNL_TUNNEL_GENEVE_DATA_MAXLEN]; | ||
| 507 | uint32_t data_len; | ||
| 508 | }; | ||
| 509 | |||
| 510 | struct tunnel { | ||
| 511 | uint32_t id; | ||
| 512 | struct expr *src; | ||
| 513 | struct expr *dst; | ||
| 514 | uint16_t sport; | ||
| 515 | uint16_t dport; | ||
| 516 | uint8_t tos; | ||
| 517 | uint8_t ttl; | ||
| 518 | enum tunnel_type type; | ||
| 519 | union { | ||
| 520 | struct { | ||
| 521 | uint32_t version; | ||
| 522 | struct { | ||
| 523 | uint32_t index; | ||
| 524 | } v1; | ||
| 525 | struct { | ||
| 526 | uint8_t direction; | ||
| 527 | uint8_t hwid; | ||
| 528 | } v2; | ||
| 529 | } erspan; | ||
| 530 | struct { | ||
| 531 | uint32_t gbp; | ||
| 532 | } vxlan; | ||
| 533 | struct list_head geneve_opts; | ||
| 534 | }; | ||
| 535 | }; | ||
| 536 | |||
| 537 | int tunnel_geneve_data_str2array(const char *hexstr, | ||
| 538 | uint8_t *out_data, | ||
| 539 | uint32_t *out_len); | ||
| 540 | |||
| 541 | /** | ||
| 542 | * struct obj - nftables stateful object statement | ||
| 543 | * | ||
| 544 | * @list: table set list node | ||
| 545 | * @location: location the stateful object was defined/declared at | ||
| 546 | * @handle: counter handle | ||
| 547 | * @type: type of stateful object | ||
| 548 | * @refcnt: object reference counter | ||
| 549 | */ | ||
| 550 | struct obj { | ||
| 551 | struct list_head list; | ||
| 552 | struct cache_item cache; | ||
| 553 | struct location location; | ||
| 554 | struct handle handle; | ||
| 555 | uint32_t type; | ||
| 556 | unsigned int refcnt; | ||
| 557 | const char *comment; | ||
| 558 | union { | ||
| 559 | struct counter counter; | ||
| 560 | struct quota quota; | ||
| 561 | struct ct_helper ct_helper; | ||
| 562 | struct limit limit; | ||
| 563 | struct ct_timeout ct_timeout; | ||
| 564 | struct secmark secmark; | ||
| 565 | struct ct_expect ct_expect; | ||
| 566 | struct synproxy synproxy; | ||
| 567 | struct tunnel tunnel; | ||
| 568 | }; | ||
| 569 | }; | ||
| 570 | |||
| 571 | struct obj *obj_alloc(const struct location *loc); | ||
| 572 | extern struct obj *obj_get(struct obj *obj); | ||
| 573 | void obj_free(struct obj *obj); | ||
| 574 | struct obj *obj_lookup_fuzzy(const char *obj_name, | ||
| 575 | const struct nft_cache *cache, | ||
| 576 | const struct table **t); | ||
| 577 | void obj_print(const struct obj *n, struct output_ctx *octx); | ||
| 578 | void obj_print_plain(const struct obj *obj, struct output_ctx *octx); | ||
| 579 | const char *obj_type_name(uint32_t type); | ||
| 580 | enum cmd_obj obj_type_to_cmd(uint32_t type); | ||
| 581 | |||
| 582 | struct flowtable { | ||
| 583 | struct list_head list; | ||
| 584 | struct cache_item cache; | ||
| 585 | struct handle handle; | ||
| 586 | struct scope scope; | ||
| 587 | struct location location; | ||
| 588 | struct hook_spec hook; | ||
| 589 | struct prio_spec priority; | ||
| 590 | const char **dev_array; | ||
| 591 | struct expr *dev_expr; | ||
| 592 | int dev_array_len; | ||
| 593 | uint32_t flags; | ||
| 594 | unsigned int refcnt; | ||
| 595 | }; | ||
| 596 | |||
| 597 | extern struct flowtable *flowtable_alloc(const struct location *loc); | ||
| 598 | extern struct flowtable *flowtable_get(struct flowtable *flowtable); | ||
| 599 | extern void flowtable_free(struct flowtable *flowtable); | ||
| 600 | extern struct flowtable *flowtable_lookup_fuzzy(const char *ft_name, | ||
| 601 | const struct nft_cache *cache, | ||
| 602 | const struct table **table); | ||
| 603 | |||
| 604 | void flowtable_print(const struct flowtable *n, struct output_ctx *octx); | ||
| 605 | void flowtable_print_plain(const struct flowtable *ft, struct output_ctx *octx); | ||
| 606 | |||
| 607 | /** | ||
| 608 | * enum cmd_ops - command operations | ||
| 609 | * | ||
| 610 | * @CMD_INVALID: invalid | ||
| 611 | * @CMD_ADD: add object (non-exclusive) | ||
| 612 | * @CMD_REPLACE, replace object | ||
| 613 | * @CMD_CREATE: create object (exclusive) | ||
| 614 | * @CMD_INSERT: insert object | ||
| 615 | * @CMD_DELETE: delete object | ||
| 616 | * @CMD_GET: get object | ||
| 617 | * @CMD_LIST: list container | ||
| 618 | * @CMD_RESET: reset container | ||
| 619 | * @CMD_FLUSH: flush container | ||
| 620 | * @CMD_RENAME: rename object | ||
| 621 | * @CMD_IMPORT: import a ruleset in a given format | ||
| 622 | * @CMD_EXPORT: export the ruleset in a given format | ||
| 623 | * @CMD_MONITOR: event listener | ||
| 624 | * @CMD_DESCRIBE: describe an expression | ||
| 625 | * @CMD_DESTROY: destroy object | ||
| 626 | */ | ||
| 627 | enum cmd_ops { | ||
| 628 | CMD_INVALID, | ||
| 629 | CMD_ADD, | ||
| 630 | CMD_REPLACE, | ||
| 631 | CMD_CREATE, | ||
| 632 | CMD_INSERT, | ||
| 633 | CMD_DELETE, | ||
| 634 | CMD_GET, | ||
| 635 | CMD_LIST, | ||
| 636 | CMD_RESET, | ||
| 637 | CMD_FLUSH, | ||
| 638 | CMD_RENAME, | ||
| 639 | CMD_IMPORT, | ||
| 640 | CMD_EXPORT, | ||
| 641 | CMD_MONITOR, | ||
| 642 | CMD_DESCRIBE, | ||
| 643 | CMD_DESTROY, | ||
| 644 | }; | ||
| 645 | |||
| 646 | /** | ||
| 647 | * enum cmd_obj - command objects | ||
| 648 | * | ||
| 649 | * @CMD_OBJ_INVALID: invalid | ||
| 650 | * @CMD_OBJ_ELEMENTS: set element(s) | ||
| 651 | * @CMD_OBJ_SET: set | ||
| 652 | * @CMD_OBJ_SETS: multiple sets | ||
| 653 | * @CMD_OBJ_SETELEMS: set elements | ||
| 654 | * @CMD_OBJ_RULE: rule | ||
| 655 | * @CMD_OBJ_CHAIN: chain | ||
| 656 | * @CMD_OBJ_CHAINS: multiple chains | ||
| 657 | * @CMD_OBJ_TABLE: table | ||
| 658 | * @CMD_OBJ_FLOWTABLE: flowtable | ||
| 659 | * @CMD_OBJ_FLOWTABLES: flowtables | ||
| 660 | * @CMD_OBJ_RULESET: ruleset | ||
| 661 | * @CMD_OBJ_EXPR: expression | ||
| 662 | * @CMD_OBJ_MONITOR: monitor | ||
| 663 | * @CMD_OBJ_MARKUP: import/export | ||
| 664 | * @CMD_OBJ_METER: meter | ||
| 665 | * @CMD_OBJ_METERS: meters | ||
| 666 | * @CMD_OBJ_COUNTER: counter | ||
| 667 | * @CMD_OBJ_COUNTERS: multiple counters | ||
| 668 | * @CMD_OBJ_QUOTA: quota | ||
| 669 | * @CMD_OBJ_QUOTAS: multiple quotas | ||
| 670 | * @CMD_OBJ_LIMIT: limit | ||
| 671 | * @CMD_OBJ_LIMITS: multiple limits | ||
| 672 | * @CMD_OBJ_SECMARK: secmark | ||
| 673 | * @CMD_OBJ_SECMARKS: multiple secmarks | ||
| 674 | * @CMD_OBJ_SYNPROXY: synproxy | ||
| 675 | * @CMD_OBJ_SYNPROXYS: multiple synproxys | ||
| 676 | * @CMD_OBJ_TUNNEL: tunnel | ||
| 677 | * @CMD_OBJ_TUNNELS: multiple tunnels | ||
| 678 | * @CMD_OBJ_HOOKS: hooks, used only for dumping | ||
| 679 | */ | ||
| 680 | enum cmd_obj { | ||
| 681 | CMD_OBJ_INVALID, | ||
| 682 | CMD_OBJ_ELEMENTS, | ||
| 683 | CMD_OBJ_SET, | ||
| 684 | CMD_OBJ_SETELEMS, | ||
| 685 | CMD_OBJ_SETS, | ||
| 686 | CMD_OBJ_RULE, | ||
| 687 | CMD_OBJ_RULES, | ||
| 688 | CMD_OBJ_CHAIN, | ||
| 689 | CMD_OBJ_CHAINS, | ||
| 690 | CMD_OBJ_TABLE, | ||
| 691 | CMD_OBJ_RULESET, | ||
| 692 | CMD_OBJ_EXPR, | ||
| 693 | CMD_OBJ_MONITOR, | ||
| 694 | CMD_OBJ_MARKUP, | ||
| 695 | CMD_OBJ_METER, | ||
| 696 | CMD_OBJ_METERS, | ||
| 697 | CMD_OBJ_MAP, | ||
| 698 | CMD_OBJ_MAPS, | ||
| 699 | CMD_OBJ_COUNTER, | ||
| 700 | CMD_OBJ_COUNTERS, | ||
| 701 | CMD_OBJ_QUOTA, | ||
| 702 | CMD_OBJ_QUOTAS, | ||
| 703 | CMD_OBJ_CT_HELPER, | ||
| 704 | CMD_OBJ_CT_HELPERS, | ||
| 705 | CMD_OBJ_LIMIT, | ||
| 706 | CMD_OBJ_LIMITS, | ||
| 707 | CMD_OBJ_FLOWTABLE, | ||
| 708 | CMD_OBJ_FLOWTABLES, | ||
| 709 | CMD_OBJ_CT_TIMEOUT, | ||
| 710 | CMD_OBJ_CT_TIMEOUTS, | ||
| 711 | CMD_OBJ_SECMARK, | ||
| 712 | CMD_OBJ_SECMARKS, | ||
| 713 | CMD_OBJ_CT_EXPECT, | ||
| 714 | CMD_OBJ_CT_EXPECTATIONS, | ||
| 715 | CMD_OBJ_SYNPROXY, | ||
| 716 | CMD_OBJ_SYNPROXYS, | ||
| 717 | CMD_OBJ_TUNNEL, | ||
| 718 | CMD_OBJ_TUNNELS, | ||
| 719 | CMD_OBJ_HOOKS, | ||
| 720 | }; | ||
| 721 | |||
| 722 | struct markup { | ||
| 723 | uint32_t format; | ||
| 724 | }; | ||
| 725 | |||
| 726 | struct markup *markup_alloc(uint32_t format); | ||
| 727 | void markup_free(struct markup *m); | ||
| 728 | |||
| 729 | enum { | ||
| 730 | CMD_MONITOR_OBJ_ANY, | ||
| 731 | CMD_MONITOR_OBJ_TABLES, | ||
| 732 | CMD_MONITOR_OBJ_CHAINS, | ||
| 733 | CMD_MONITOR_OBJ_RULES, | ||
| 734 | CMD_MONITOR_OBJ_SETS, | ||
| 735 | CMD_MONITOR_OBJ_ELEMS, | ||
| 736 | CMD_MONITOR_OBJ_RULESET, | ||
| 737 | CMD_MONITOR_OBJ_TRACE, | ||
| 738 | CMD_MONITOR_OBJ_MAX | ||
| 739 | }; | ||
| 740 | |||
| 741 | enum cmd_monitor_event { | ||
| 742 | CMD_MONITOR_EVENT_ANY, | ||
| 743 | CMD_MONITOR_EVENT_NEW, | ||
| 744 | CMD_MONITOR_EVENT_DEL | ||
| 745 | }; | ||
| 746 | #define CMD_MONITOR_EVENT_MAX (CMD_MONITOR_EVENT_DEL + 1) | ||
| 747 | |||
| 748 | struct monitor { | ||
| 749 | struct location location; | ||
| 750 | uint32_t format; | ||
| 751 | uint32_t flags; | ||
| 752 | uint32_t type; | ||
| 753 | enum cmd_monitor_event event; | ||
| 754 | }; | ||
| 755 | |||
| 756 | struct monitor *monitor_alloc(uint32_t format, uint32_t type, | ||
| 757 | enum cmd_monitor_event event); | ||
| 758 | void monitor_free(struct monitor *m); | ||
| 759 | |||
| 760 | #define NFT_NLATTR_LOC_MAX 32 | ||
| 761 | |||
| 762 | struct nlerr_loc { | ||
| 763 | uint32_t seqnum; | ||
| 764 | uint32_t offset; | ||
| 765 | const struct location *location; | ||
| 766 | }; | ||
| 767 | |||
| 768 | /** | ||
| 769 | * struct cmd - command statement | ||
| 770 | * | ||
| 771 | * @list: list node | ||
| 772 | * @location: location of the statement | ||
| 773 | * @op: operation | ||
| 774 | * @obj: object type to perform operation on | ||
| 775 | * @handle: handle for operations working without full objects | ||
| 776 | * @seqnum: sequence number to match netlink errors | ||
| 777 | * @union: object | ||
| 778 | * @arg: argument data | ||
| 779 | */ | ||
| 780 | struct cmd { | ||
| 781 | struct list_head list; | ||
| 782 | struct location location; | ||
| 783 | enum cmd_ops op; | ||
| 784 | enum cmd_obj obj; | ||
| 785 | struct handle handle; | ||
| 786 | uint32_t seqnum_from; | ||
| 787 | uint32_t seqnum_to; | ||
| 788 | union { | ||
| 789 | void *data; | ||
| 790 | struct expr *expr; | ||
| 791 | struct set *set; | ||
| 792 | struct { | ||
| 793 | struct expr *expr; /* same offset as cmd->expr */ | ||
| 794 | struct set *set; | ||
| 795 | } elem; | ||
| 796 | struct rule *rule; | ||
| 797 | struct chain *chain; | ||
| 798 | struct table *table; | ||
| 799 | struct flowtable *flowtable; | ||
| 800 | struct monitor *monitor; | ||
| 801 | struct markup *markup; | ||
| 802 | struct obj *object; | ||
| 803 | }; | ||
| 804 | struct nlerr_loc *attr; | ||
| 805 | uint32_t attr_array_len; | ||
| 806 | uint32_t num_attrs; | ||
| 807 | const void *arg; | ||
| 808 | }; | ||
| 809 | |||
| 810 | extern struct cmd *cmd_alloc(enum cmd_ops op, enum cmd_obj obj, | ||
| 811 | const struct handle *h, const struct location *loc, | ||
| 812 | void *data); | ||
| 813 | extern struct cmd *cmd_alloc_obj_ct(enum cmd_ops op, int type, | ||
| 814 | const struct handle *h, | ||
| 815 | const struct location *loc, struct obj *obj); | ||
| 816 | extern void cmd_free(struct cmd *cmd); | ||
| 817 | |||
| 818 | #include <payload.h> | ||
| 819 | #include <expression.h> | ||
| 820 | |||
| 821 | struct eval_recursion { | ||
| 822 | uint16_t binop; | ||
| 823 | uint16_t list; | ||
| 824 | }; | ||
| 825 | |||
| 826 | /** | ||
| 827 | * struct eval_ctx - evaluation context | ||
| 828 | * | ||
| 829 | * @nft: nftables context | ||
| 830 | * @msgs: message queue | ||
| 831 | * @cmd: current command | ||
| 832 | * @table: current table | ||
| 833 | * @rule: current rule | ||
| 834 | * @set: current set | ||
| 835 | * @stmt: current statement | ||
| 836 | * @stmt_len: current statement template length | ||
| 837 | * @recursion: expr evaluation recursion counters | ||
| 838 | * @cache: cache context | ||
| 839 | * @debug_mask: debugging bitmask | ||
| 840 | * @ectx: expression context | ||
| 841 | * @_pctx: payload contexts | ||
| 842 | * @inner_desc: inner header description | ||
| 843 | */ | ||
| 844 | struct eval_ctx { | ||
| 845 | struct nft_ctx *nft; | ||
| 846 | struct list_head *msgs; | ||
| 847 | struct cmd *cmd; | ||
| 848 | struct table *table; | ||
| 849 | struct rule *rule; | ||
| 850 | struct set *set; | ||
| 851 | struct stmt *stmt; | ||
| 852 | uint32_t stmt_len; | ||
| 853 | struct eval_recursion recursion; | ||
| 854 | struct expr_ctx ectx; | ||
| 855 | struct proto_ctx _pctx[2]; | ||
| 856 | const struct proto_desc *inner_desc; | ||
| 857 | }; | ||
| 858 | |||
| 859 | extern int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd); | ||
| 860 | |||
| 861 | extern struct error_record *rule_postprocess(struct rule *rule); | ||
| 862 | |||
| 863 | struct netlink_ctx; | ||
| 864 | extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd); | ||
| 865 | |||
| 866 | struct timeout_protocol { | ||
| 867 | uint32_t array_size; | ||
| 868 | const char *const *state_to_name; | ||
| 869 | uint32_t *dflt_timeout; | ||
| 870 | }; | ||
| 871 | |||
| 872 | extern struct timeout_protocol timeout_protocol[UINT8_MAX + 1]; | ||
| 873 | extern int timeout_str2num(uint16_t l4proto, struct timeout_state *ts); | ||
| 874 | |||
| 875 | #endif /* NFTABLES_RULE_H */ | ||
| 876 |