FILE
nf_tables.h
File preview with syntax highlighting.
linux-6.19.14/include/net/netfilter/nf_tables.h
1/* SPDX-License-Identifier: GPL-2.0 */ 2#ifndef _NET_NF_TABLES_H 3#define _NET_NF_TABLES_H 4 5#include <linux/unaligned.h> 6#include <linux/list.h> 7#include <linux/netfilter.h> 8#include <linux/netfilter/nfnetlink.h> 9#include <linux/netfilter/x_tables.h> 10#include <linux/netfilter/nf_tables.h> 11#include <linux/u64_stats_sync.h> 12#include <linux/rhashtable.h> 13#include <net/netfilter/nf_flow_table.h> 14#include <net/netlink.h> 15#include <net/flow_offload.h> 16#include <net/netns/generic.h> 17 18#define NFT_MAX_HOOKS (NF_INET_INGRESS + 1) 19 20struct module; 21 22#define NFT_JUMP_STACK_SIZE 16 23 24enum { 25 NFT_PKTINFO_L4PROTO = (1 << 0), 26 NFT_PKTINFO_INNER = (1 << 1), 27 NFT_PKTINFO_INNER_FULL = (1 << 2), 28}; 29 30struct nft_pktinfo { 31 struct sk_buff *skb; 32 const struct nf_hook_state *state; 33 u8 flags; 34 u8 tprot; 35 u16 fragoff; 36 u16 thoff; 37 u16 inneroff; 38}; 39 40static inline struct sock *nft_sk(const struct nft_pktinfo *pkt) 41{ 42 return pkt->state->sk; 43} 44 45static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt) 46{ 47 return pkt->thoff; 48} 49 50static inline struct net *nft_net(const struct nft_pktinfo *pkt) 51{ 52 return pkt->state->net; 53} 54 55static inline unsigned int nft_hook(const struct nft_pktinfo *pkt) 56{ 57 return pkt->state->hook; 58} 59 60static inline u8 nft_pf(const struct nft_pktinfo *pkt) 61{ 62 return pkt->state->pf; 63} 64 65static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt) 66{ 67 return pkt->state->in; 68} 69 70static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt) 71{ 72 return pkt->state->out; 73} 74 75static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, 76 struct sk_buff *skb, 77 const struct nf_hook_state *state) 78{ 79 pkt->skb = skb; 80 pkt->state = state; 81} 82 83static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt) 84{ 85 pkt->flags = 0; 86 pkt->tprot = 0; 87 pkt->thoff = 0; 88 pkt->fragoff = 0; 89} 90 91/** 92 * struct nft_verdict - nf_tables verdict 93 * 94 * @code: nf_tables/netfilter verdict code 95 * @chain: destination chain for NFT_JUMP/NFT_GOTO 96 */ 97struct nft_verdict { 98 u32 code; 99 struct nft_chain *chain; 100}; 101 102struct nft_data { 103 union { 104 u32 data[4]; 105 struct nft_verdict verdict; 106 }; 107} __attribute__((aligned(__alignof__(u64)))); 108 109#define NFT_REG32_NUM 20 110 111/** 112 * struct nft_regs - nf_tables register set 113 * 114 * @data: data registers 115 * @verdict: verdict register 116 * 117 * The first four data registers alias to the verdict register. 118 */ 119struct nft_regs { 120 union { 121 u32 data[NFT_REG32_NUM]; 122 struct nft_verdict verdict; 123 }; 124}; 125 126struct nft_regs_track { 127 struct { 128 const struct nft_expr *selector; 129 const struct nft_expr *bitwise; 130 u8 num_reg; 131 } regs[NFT_REG32_NUM]; 132 133 const struct nft_expr *cur; 134 const struct nft_expr *last; 135}; 136 137/* Store/load an u8, u16 or u64 integer to/from the u32 data register. 138 * 139 * Note, when using concatenations, register allocation happens at 32-bit 140 * level. So for store instruction, pad the rest part with zero to avoid 141 * garbage values. 142 */ 143 144static inline void nft_reg_store8(u32 *dreg, u8 val) 145{ 146 *dreg = 0; 147 *(u8 *)dreg = val; 148} 149 150static inline u8 nft_reg_load8(const u32 *sreg) 151{ 152 return *(u8 *)sreg; 153} 154 155static inline void nft_reg_store16(u32 *dreg, u16 val) 156{ 157 *dreg = 0; 158 *(u16 *)dreg = val; 159} 160 161static inline void nft_reg_store_be16(u32 *dreg, __be16 val) 162{ 163 nft_reg_store16(dreg, (__force __u16)val); 164} 165 166static inline u16 nft_reg_load16(const u32 *sreg) 167{ 168 return *(u16 *)sreg; 169} 170 171static inline __be16 nft_reg_load_be16(const u32 *sreg) 172{ 173 return (__force __be16)nft_reg_load16(sreg); 174} 175 176static inline __be32 nft_reg_load_be32(const u32 *sreg) 177{ 178 return *(__force __be32 *)sreg; 179} 180 181static inline void nft_reg_store64(u64 *dreg, u64 val) 182{ 183 put_unaligned(val, dreg); 184} 185 186static inline u64 nft_reg_load64(const u32 *sreg) 187{ 188 return get_unaligned((u64 *)sreg); 189} 190 191static inline void nft_data_copy(u32 *dst, const struct nft_data *src, 192 unsigned int len) 193{ 194 if (len % NFT_REG32_SIZE) 195 dst[len / NFT_REG32_SIZE] = 0; 196 memcpy(dst, src, len); 197} 198 199/** 200 * struct nft_ctx - nf_tables rule/set context 201 * 202 * @net: net namespace 203 * @table: the table the chain is contained in 204 * @chain: the chain the rule is contained in 205 * @nla: netlink attributes 206 * @portid: netlink portID of the original message 207 * @seq: netlink sequence number 208 * @flags: modifiers to new request 209 * @family: protocol family 210 * @level: depth of the chains 211 * @report: notify via unicast netlink message 212 * @reg_inited: bitmap of initialised registers 213 */ 214struct nft_ctx { 215 struct net *net; 216 struct nft_table *table; 217 struct nft_chain *chain; 218 const struct nlattr * const *nla; 219 u32 portid; 220 u32 seq; 221 u16 flags; 222 u8 family; 223 u8 level; 224 bool report; 225 DECLARE_BITMAP(reg_inited, NFT_REG32_NUM); 226}; 227 228enum nft_data_desc_flags { 229 NFT_DATA_DESC_SETELEM = (1 << 0), 230}; 231 232struct nft_data_desc { 233 enum nft_data_types type; 234 unsigned int size; 235 unsigned int len; 236 unsigned int flags; 237}; 238 239int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, 240 struct nft_data_desc *desc, const struct nlattr *nla); 241void nft_data_hold(const struct nft_data *data, enum nft_data_types type); 242void nft_data_release(const struct nft_data *data, enum nft_data_types type); 243int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data, 244 enum nft_data_types type, unsigned int len); 245 246static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg) 247{ 248 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; 249} 250 251static inline enum nft_registers nft_type_to_reg(enum nft_data_types type) 252{ 253 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE; 254} 255 256int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest); 257int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg); 258 259int nft_parse_register_load(const struct nft_ctx *ctx, 260 const struct nlattr *attr, u8 *sreg, u32 len); 261int nft_parse_register_store(const struct nft_ctx *ctx, 262 const struct nlattr *attr, u8 *dreg, 263 const struct nft_data *data, 264 enum nft_data_types type, unsigned int len); 265 266/** 267 * struct nft_userdata - user defined data associated with an object 268 * 269 * @len: length of the data 270 * @data: content 271 * 272 * The presence of user data is indicated in an object specific fashion, 273 * so a length of zero can't occur and the value "len" indicates data 274 * of length len + 1. 275 */ 276struct nft_userdata { 277 u8 len; 278 unsigned char data[]; 279}; 280 281/* placeholder structure for opaque set element backend representation. */ 282struct nft_elem_priv { }; 283 284/** 285 * struct nft_set_elem - generic representation of set elements 286 * 287 * @key: element key 288 * @key_end: closing element key 289 * @data: element data 290 * @priv: element private data and extensions 291 */ 292struct nft_set_elem { 293 union { 294 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 295 struct nft_data val; 296 } key; 297 union { 298 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 299 struct nft_data val; 300 } key_end; 301 union { 302 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 303 struct nft_data val; 304 } data; 305 struct nft_elem_priv *priv; 306}; 307 308static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv) 309{ 310 return (void *)priv; 311} 312 313 314/** 315 * enum nft_iter_type - nftables set iterator type 316 * 317 * @NFT_ITER_UNSPEC: unspecified, to catch errors 318 * @NFT_ITER_READ: read-only iteration over set elements 319 * @NFT_ITER_UPDATE: iteration under mutex to update set element state 320 * @NFT_ITER_UPDATE_CLONE: clone set before iteration under mutex to update element 321 */ 322enum nft_iter_type { 323 NFT_ITER_UNSPEC, 324 NFT_ITER_READ, 325 NFT_ITER_UPDATE, 326 NFT_ITER_UPDATE_CLONE, 327}; 328 329struct nft_set; 330struct nft_set_iter { 331 u8 genmask; 332 enum nft_iter_type type:8; 333 unsigned int count; 334 unsigned int skip; 335 int err; 336 int (*fn)(const struct nft_ctx *ctx, 337 struct nft_set *set, 338 const struct nft_set_iter *iter, 339 struct nft_elem_priv *elem_priv); 340}; 341 342/** 343 * struct nft_set_desc - description of set elements 344 * 345 * @ktype: key type 346 * @klen: key length 347 * @dtype: data type 348 * @dlen: data length 349 * @objtype: object type 350 * @size: number of set elements 351 * @policy: set policy 352 * @gc_int: garbage collector interval 353 * @timeout: element timeout 354 * @field_len: length of each field in concatenation, bytes 355 * @field_count: number of concatenated fields in element 356 * @expr: set must support for expressions 357 */ 358struct nft_set_desc { 359 u32 ktype; 360 unsigned int klen; 361 u32 dtype; 362 unsigned int dlen; 363 u32 objtype; 364 unsigned int size; 365 u32 policy; 366 u32 gc_int; 367 u64 timeout; 368 u8 field_len[NFT_REG32_COUNT]; 369 u8 field_count; 370 bool expr; 371}; 372 373/** 374 * enum nft_set_class - performance class 375 * 376 * @NFT_SET_CLASS_O_1: constant, O(1) 377 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N) 378 * @NFT_SET_CLASS_O_N: linear, O(N) 379 */ 380enum nft_set_class { 381 NFT_SET_CLASS_O_1, 382 NFT_SET_CLASS_O_LOG_N, 383 NFT_SET_CLASS_O_N, 384}; 385 386/** 387 * struct nft_set_estimate - estimation of memory and performance 388 * characteristics 389 * 390 * @size: required memory 391 * @lookup: lookup performance class 392 * @space: memory class 393 */ 394struct nft_set_estimate { 395 u64 size; 396 enum nft_set_class lookup; 397 enum nft_set_class space; 398}; 399 400#define NFT_EXPR_MAXATTR 16 401#define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ 402 ALIGN(size, __alignof__(struct nft_expr))) 403 404/** 405 * struct nft_expr - nf_tables expression 406 * 407 * @ops: expression ops 408 * @data: expression private data 409 */ 410struct nft_expr { 411 const struct nft_expr_ops *ops; 412 unsigned char data[] 413 __attribute__((aligned(__alignof__(u64)))); 414}; 415 416static inline void *nft_expr_priv(const struct nft_expr *expr) 417{ 418 return (void *)expr->data; 419} 420 421struct nft_expr_info; 422 423int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, 424 struct nft_expr_info *info); 425int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp); 426void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); 427int nft_expr_dump(struct sk_buff *skb, unsigned int attr, 428 const struct nft_expr *expr, bool reset); 429bool nft_expr_reduce_bitwise(struct nft_regs_track *track, 430 const struct nft_expr *expr); 431 432struct nft_set_ext; 433 434/** 435 * struct nft_set_ops - nf_tables set operations 436 * 437 * @lookup: look up an element within the set 438 * @update: update an element if exists, add it if doesn't exist 439 * @delete: delete an element 440 * @insert: insert new element into set 441 * @activate: activate new element in the next generation 442 * @deactivate: lookup for element and deactivate it in the next generation 443 * @flush: deactivate element in the next generation 444 * @remove: remove element from set 445 * @walk: iterate over all set elements 446 * @get: get set elements 447 * @ksize: kernel set size 448 * @usize: userspace set size 449 * @adjust_maxsize: delta to adjust maximum set size 450 * @commit: commit set elements 451 * @abort: abort set elements 452 * @privsize: function to return size of set private data 453 * @estimate: estimate the required memory size and the lookup complexity class 454 * @init: initialize private data of new set instance 455 * @destroy: destroy private data of set instance 456 * @gc_init: initialize garbage collection 457 * @abort_skip_removal: skip removal of elements from abort path 458 * @elemsize: element private size 459 * 460 * Operations lookup, update and delete have simpler interfaces, are faster 461 * and currently only used in the packet path. All the rest are slower, 462 * control plane functions. 463 */ 464struct nft_set_ops { 465 const struct nft_set_ext * (*lookup)(const struct net *net, 466 const struct nft_set *set, 467 const u32 *key); 468 const struct nft_set_ext * (*update)(struct nft_set *set, 469 const u32 *key, 470 const struct nft_expr *expr, 471 struct nft_regs *regs); 472 bool (*delete)(const struct nft_set *set, 473 const u32 *key); 474 475 int (*insert)(const struct net *net, 476 const struct nft_set *set, 477 const struct nft_set_elem *elem, 478 struct nft_elem_priv **priv); 479 void (*activate)(const struct net *net, 480 const struct nft_set *set, 481 struct nft_elem_priv *elem_priv); 482 struct nft_elem_priv * (*deactivate)(const struct net *net, 483 const struct nft_set *set, 484 const struct nft_set_elem *elem); 485 void (*flush)(const struct net *net, 486 const struct nft_set *set, 487 struct nft_elem_priv *priv); 488 void (*remove)(const struct net *net, 489 const struct nft_set *set, 490 struct nft_elem_priv *elem_priv); 491 void (*walk)(const struct nft_ctx *ctx, 492 struct nft_set *set, 493 struct nft_set_iter *iter); 494 struct nft_elem_priv * (*get)(const struct net *net, 495 const struct nft_set *set, 496 const struct nft_set_elem *elem, 497 unsigned int flags); 498 u32 (*ksize)(u32 size); 499 u32 (*usize)(u32 size); 500 u32 (*adjust_maxsize)(const struct nft_set *set); 501 void (*commit)(struct nft_set *set); 502 void (*abort)(const struct nft_set *set); 503 u64 (*privsize)(const struct nlattr * const nla[], 504 const struct nft_set_desc *desc); 505 bool (*estimate)(const struct nft_set_desc *desc, 506 u32 features, 507 struct nft_set_estimate *est); 508 int (*init)(const struct nft_set *set, 509 const struct nft_set_desc *desc, 510 const struct nlattr * const nla[]); 511 void (*destroy)(const struct nft_ctx *ctx, 512 const struct nft_set *set); 513 void (*gc_init)(const struct nft_set *set); 514 515 bool abort_skip_removal; 516 unsigned int elemsize; 517}; 518 519/** 520 * struct nft_set_type - nf_tables set type 521 * 522 * @ops: set ops for this type 523 * @features: features supported by the implementation 524 */ 525struct nft_set_type { 526 const struct nft_set_ops ops; 527 u32 features; 528}; 529#define to_set_type(o) container_of(o, struct nft_set_type, ops) 530 531struct nft_set_elem_expr { 532 u8 size; 533 unsigned char data[] 534 __attribute__((aligned(__alignof__(struct nft_expr)))); 535}; 536 537#define nft_setelem_expr_at(__elem_expr, __offset) \ 538 ((struct nft_expr *)&__elem_expr->data[__offset]) 539 540#define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \ 541 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ 542 __size < (__elem_expr)->size; \ 543 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) 544 545#define NFT_SET_EXPR_MAX 2 546 547/** 548 * struct nft_set - nf_tables set instance 549 * 550 * @list: table set list node 551 * @bindings: list of set bindings 552 * @refs: internal refcounting for async set destruction 553 * @table: table this set belongs to 554 * @net: netnamespace this set belongs to 555 * @name: name of the set 556 * @handle: unique handle of the set 557 * @ktype: key type (numeric type defined by userspace, not used in the kernel) 558 * @dtype: data type (verdict or numeric type defined by userspace) 559 * @objtype: object type (see NFT_OBJECT_* definitions) 560 * @size: maximum set size 561 * @field_len: length of each field in concatenation, bytes 562 * @field_count: number of concatenated fields in element 563 * @in_update_walk: true during ->walk() in transaction phase 564 * @use: number of rules references to this set 565 * @nelems: number of elements 566 * @ndeact: number of deactivated elements queued for removal 567 * @timeout: default timeout value in jiffies 568 * @gc_int: garbage collection interval in msecs 569 * @policy: set parameterization (see enum nft_set_policies) 570 * @udlen: user data length 571 * @udata: user data 572 * @pending_update: list of pending update set element 573 * @ops: set ops 574 * @flags: set flags 575 * @dead: set will be freed, never cleared 576 * @genmask: generation mask 577 * @klen: key length 578 * @dlen: data length 579 * @num_exprs: numbers of exprs 580 * @exprs: stateful expression 581 * @catchall_list: list of catch-all set element 582 * @data: private set data 583 */ 584struct nft_set { 585 struct list_head list; 586 struct list_head bindings; 587 refcount_t refs; 588 struct nft_table *table; 589 possible_net_t net; 590 char *name; 591 u64 handle; 592 u32 ktype; 593 u32 dtype; 594 u32 objtype; 595 u32 size; 596 u8 field_len[NFT_REG32_COUNT]; 597 u8 field_count; 598 bool in_update_walk; 599 u32 use; 600 atomic_t nelems; 601 u32 ndeact; 602 u64 timeout; 603 u32 gc_int; 604 u16 policy; 605 u16 udlen; 606 unsigned char *udata; 607 struct list_head pending_update; 608 /* runtime data below here */ 609 const struct nft_set_ops *ops ____cacheline_aligned; 610 u16 flags:13, 611 dead:1, 612 genmask:2; 613 u8 klen; 614 u8 dlen; 615 u8 num_exprs; 616 struct nft_expr *exprs[NFT_SET_EXPR_MAX]; 617 struct list_head catchall_list; 618 unsigned char data[] 619 __attribute__((aligned(__alignof__(u64)))); 620}; 621 622static inline bool nft_set_is_anonymous(const struct nft_set *set) 623{ 624 return set->flags & NFT_SET_ANONYMOUS; 625} 626 627static inline void *nft_set_priv(const struct nft_set *set) 628{ 629 return (void *)set->data; 630} 631 632static inline enum nft_data_types nft_set_datatype(const struct nft_set *set) 633{ 634 return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; 635} 636 637static inline bool nft_set_gc_is_pending(const struct nft_set *s) 638{ 639 return refcount_read(&s->refs) != 1; 640} 641 642static inline struct nft_set *nft_set_container_of(const void *priv) 643{ 644 return (void *)priv - offsetof(struct nft_set, data); 645} 646 647struct nft_set *nft_set_lookup_global(const struct net *net, 648 const struct nft_table *table, 649 const struct nlattr *nla_set_name, 650 const struct nlattr *nla_set_id, 651 u8 genmask); 652 653struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, 654 const struct nft_set *set); 655 656static inline unsigned long nft_set_gc_interval(const struct nft_set *set) 657{ 658 u32 gc_int = READ_ONCE(set->gc_int); 659 660 return gc_int ? msecs_to_jiffies(gc_int) : HZ; 661} 662 663/** 664 * struct nft_set_binding - nf_tables set binding 665 * 666 * @list: set bindings list node 667 * @chain: chain containing the rule bound to the set 668 * @flags: set action flags 669 * 670 * A set binding contains all information necessary for validation 671 * of new elements added to a bound set. 672 */ 673struct nft_set_binding { 674 struct list_head list; 675 const struct nft_chain *chain; 676 u32 flags; 677}; 678 679enum nft_trans_phase; 680void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set); 681void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, 682 struct nft_set_binding *binding, 683 enum nft_trans_phase phase); 684int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, 685 struct nft_set_binding *binding); 686void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set); 687 688/** 689 * enum nft_set_extensions - set extension type IDs 690 * 691 * @NFT_SET_EXT_KEY: element key 692 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges 693 * @NFT_SET_EXT_DATA: mapping data 694 * @NFT_SET_EXT_FLAGS: element flags 695 * @NFT_SET_EXT_TIMEOUT: element timeout 696 * @NFT_SET_EXT_USERDATA: user data associated with the element 697 * @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element 698 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element 699 * @NFT_SET_EXT_NUM: number of extension types 700 */ 701enum nft_set_extensions { 702 NFT_SET_EXT_KEY, 703 NFT_SET_EXT_KEY_END, 704 NFT_SET_EXT_DATA, 705 NFT_SET_EXT_FLAGS, 706 NFT_SET_EXT_TIMEOUT, 707 NFT_SET_EXT_USERDATA, 708 NFT_SET_EXT_EXPRESSIONS, 709 NFT_SET_EXT_OBJREF, 710 NFT_SET_EXT_NUM 711}; 712 713/** 714 * struct nft_set_ext_type - set extension type 715 * 716 * @len: fixed part length of the extension 717 * @align: alignment requirements of the extension 718 */ 719struct nft_set_ext_type { 720 u8 len; 721 u8 align; 722}; 723 724extern const struct nft_set_ext_type nft_set_ext_types[]; 725 726/** 727 * struct nft_set_ext_tmpl - set extension template 728 * 729 * @len: length of extension area 730 * @offset: offsets of individual extension types 731 * @ext_len: length of the expected extension(used to sanity check) 732 */ 733struct nft_set_ext_tmpl { 734 u16 len; 735 u8 offset[NFT_SET_EXT_NUM]; 736 u8 ext_len[NFT_SET_EXT_NUM]; 737}; 738 739/** 740 * struct nft_set_ext - set extensions 741 * 742 * @genmask: generation mask, but also flags (see NFT_SET_ELEM_DEAD_BIT) 743 * @offset: offsets of individual extension types 744 * @data: beginning of extension data 745 * 746 * This structure must be aligned to word size, otherwise atomic bitops 747 * on genmask field can cause alignment failure on some archs. 748 */ 749struct nft_set_ext { 750 u8 genmask; 751 u8 offset[NFT_SET_EXT_NUM]; 752 char data[]; 753} __aligned(BITS_PER_LONG / 8); 754 755static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) 756{ 757 memset(tmpl, 0, sizeof(*tmpl)); 758 tmpl->len = sizeof(struct nft_set_ext); 759} 760 761static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, 762 unsigned int len) 763{ 764 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); 765 if (tmpl->len > U8_MAX) 766 return -EINVAL; 767 768 tmpl->offset[id] = tmpl->len; 769 tmpl->ext_len[id] = nft_set_ext_types[id].len + len; 770 tmpl->len += tmpl->ext_len[id]; 771 772 return 0; 773} 774 775static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) 776{ 777 return nft_set_ext_add_length(tmpl, id, 0); 778} 779 780static inline void nft_set_ext_init(struct nft_set_ext *ext, 781 const struct nft_set_ext_tmpl *tmpl) 782{ 783 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); 784} 785 786static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) 787{ 788 return !!ext->offset[id]; 789} 790 791static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) 792{ 793 return ext && __nft_set_ext_exists(ext, id); 794} 795 796static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) 797{ 798 return (void *)ext + ext->offset[id]; 799} 800 801static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) 802{ 803 return nft_set_ext(ext, NFT_SET_EXT_KEY); 804} 805 806static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext) 807{ 808 return nft_set_ext(ext, NFT_SET_EXT_KEY_END); 809} 810 811static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) 812{ 813 return nft_set_ext(ext, NFT_SET_EXT_DATA); 814} 815 816static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) 817{ 818 return nft_set_ext(ext, NFT_SET_EXT_FLAGS); 819} 820 821struct nft_timeout { 822 u64 timeout; 823 u64 expiration; 824}; 825 826static inline struct nft_timeout *nft_set_ext_timeout(const struct nft_set_ext *ext) 827{ 828 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); 829} 830 831static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) 832{ 833 return nft_set_ext(ext, NFT_SET_EXT_USERDATA); 834} 835 836static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext) 837{ 838 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS); 839} 840 841static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, 842 u64 tstamp) 843{ 844 if (!nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) || 845 READ_ONCE(nft_set_ext_timeout(ext)->timeout) == 0) 846 return false; 847 848 return time_after_eq64(tstamp, READ_ONCE(nft_set_ext_timeout(ext)->expiration)); 849} 850 851static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) 852{ 853 return __nft_set_elem_expired(ext, get_jiffies_64()); 854} 855 856static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, 857 const struct nft_elem_priv *elem_priv) 858{ 859 return (void *)elem_priv + set->ops->elemsize; 860} 861 862static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext) 863{ 864 return nft_set_ext(ext, NFT_SET_EXT_OBJREF); 865} 866 867struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, 868 const struct nft_set *set, 869 const struct nlattr *attr); 870 871struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set, 872 const struct nft_set_ext_tmpl *tmpl, 873 const u32 *key, const u32 *key_end, 874 const u32 *data, 875 u64 timeout, u64 expiration, gfp_t gfp); 876int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, 877 struct nft_expr *expr_array[]); 878void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, 879 struct nft_set_elem_expr *elem_expr); 880void nft_set_elem_destroy(const struct nft_set *set, 881 const struct nft_elem_priv *elem_priv, 882 bool destroy_expr); 883void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, 884 const struct nft_set *set, 885 const struct nft_elem_priv *elem_priv); 886 887struct nft_expr_ops; 888/** 889 * struct nft_expr_type - nf_tables expression type 890 * 891 * @select_ops: function to select nft_expr_ops 892 * @release_ops: release nft_expr_ops 893 * @ops: default ops, used when no select_ops functions is present 894 * @inner_ops: inner ops, used for inner packet operation 895 * @list: used internally 896 * @name: Identifier 897 * @owner: module reference 898 * @policy: netlink attribute policy 899 * @maxattr: highest netlink attribute number 900 * @family: address family for AF-specific types 901 * @flags: expression type flags 902 */ 903struct nft_expr_type { 904 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, 905 const struct nlattr * const tb[]); 906 void (*release_ops)(const struct nft_expr_ops *ops); 907 const struct nft_expr_ops *ops; 908 const struct nft_expr_ops *inner_ops; 909 struct list_head list; 910 const char *name; 911 struct module *owner; 912 const struct nla_policy *policy; 913 unsigned int maxattr; 914 u8 family; 915 u8 flags; 916}; 917 918#define NFT_EXPR_STATEFUL 0x1 919#define NFT_EXPR_GC 0x2 920 921enum nft_trans_phase { 922 NFT_TRANS_PREPARE, 923 NFT_TRANS_PREPARE_ERROR, 924 NFT_TRANS_ABORT, 925 NFT_TRANS_COMMIT, 926 NFT_TRANS_RELEASE 927}; 928 929struct nft_flow_rule; 930struct nft_offload_ctx; 931 932/** 933 * struct nft_expr_ops - nf_tables expression operations 934 * 935 * @eval: Expression evaluation function 936 * @clone: Expression clone function 937 * @size: full expression size, including private data size 938 * @init: initialization function 939 * @activate: activate expression in the next generation 940 * @deactivate: deactivate expression in next generation 941 * @destroy: destruction function, called after synchronize_rcu 942 * @destroy_clone: destruction clone function 943 * @dump: function to dump parameters 944 * @validate: validate expression, called during loop detection 945 * @reduce: reduce expression 946 * @gc: garbage collection expression 947 * @offload: hardware offload expression 948 * @offload_action: function to report true/false to allocate one slot or not in the flow 949 * offload array 950 * @offload_stats: function to synchronize hardware stats via updating the counter expression 951 * @type: expression type 952 * @data: extra data to attach to this expression operation 953 */ 954struct nft_expr_ops { 955 void (*eval)(const struct nft_expr *expr, 956 struct nft_regs *regs, 957 const struct nft_pktinfo *pkt); 958 int (*clone)(struct nft_expr *dst, 959 const struct nft_expr *src, gfp_t gfp); 960 unsigned int size; 961 962 int (*init)(const struct nft_ctx *ctx, 963 const struct nft_expr *expr, 964 const struct nlattr * const tb[]); 965 void (*activate)(const struct nft_ctx *ctx, 966 const struct nft_expr *expr); 967 void (*deactivate)(const struct nft_ctx *ctx, 968 const struct nft_expr *expr, 969 enum nft_trans_phase phase); 970 void (*destroy)(const struct nft_ctx *ctx, 971 const struct nft_expr *expr); 972 void (*destroy_clone)(const struct nft_ctx *ctx, 973 const struct nft_expr *expr); 974 int (*dump)(struct sk_buff *skb, 975 const struct nft_expr *expr, 976 bool reset); 977 int (*validate)(const struct nft_ctx *ctx, 978 const struct nft_expr *expr); 979 bool (*reduce)(struct nft_regs_track *track, 980 const struct nft_expr *expr); 981 bool (*gc)(struct net *net, 982 const struct nft_expr *expr); 983 int (*offload)(struct nft_offload_ctx *ctx, 984 struct nft_flow_rule *flow, 985 const struct nft_expr *expr); 986 bool (*offload_action)(const struct nft_expr *expr); 987 void (*offload_stats)(struct nft_expr *expr, 988 const struct flow_stats *stats); 989 const struct nft_expr_type *type; 990 void *data; 991}; 992 993/** 994 * struct nft_rule - nf_tables rule 995 * 996 * @list: used internally 997 * @handle: rule handle 998 * @genmask: generation mask 999 * @dlen: length of expression data 1000 * @udata: user data is appended to the rule 1001 * @data: expression data 1002 */ 1003struct nft_rule { 1004 struct list_head list; 1005 u64 handle:42, 1006 genmask:2, 1007 dlen:12, 1008 udata:1; 1009 unsigned char data[] 1010 __attribute__((aligned(__alignof__(struct nft_expr)))); 1011}; 1012 1013static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule) 1014{ 1015 return (struct nft_expr *)&rule->data[0]; 1016} 1017 1018static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr) 1019{ 1020 return ((void *)expr) + expr->ops->size; 1021} 1022 1023static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule) 1024{ 1025 return (struct nft_expr *)&rule->data[rule->dlen]; 1026} 1027 1028static inline bool nft_expr_more(const struct nft_rule *rule, 1029 const struct nft_expr *expr) 1030{ 1031 return expr != nft_expr_last(rule) && expr->ops; 1032} 1033 1034static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule) 1035{ 1036 return (void *)&rule->data[rule->dlen]; 1037} 1038 1039void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule); 1040void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule, 1041 enum nft_trans_phase phase); 1042void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule); 1043 1044static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext, 1045 struct nft_regs *regs, 1046 const struct nft_pktinfo *pkt) 1047{ 1048 struct nft_set_elem_expr *elem_expr; 1049 struct nft_expr *expr; 1050 u32 size; 1051 1052 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) { 1053 elem_expr = nft_set_ext_expr(ext); 1054 nft_setelem_expr_foreach(expr, elem_expr, size) { 1055 expr->ops->eval(expr, regs, pkt); 1056 if (regs->verdict.code == NFT_BREAK) 1057 return; 1058 } 1059 } 1060} 1061 1062/* 1063 * The last pointer isn't really necessary, but the compiler isn't able to 1064 * determine that the result of nft_expr_last() is always the same since it 1065 * can't assume that the dlen value wasn't changed within calls in the loop. 1066 */ 1067#define nft_rule_for_each_expr(expr, last, rule) \ 1068 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \ 1069 (expr) != (last); \ 1070 (expr) = nft_expr_next(expr)) 1071 1072#define NFT_CHAIN_POLICY_UNSET U8_MAX 1073 1074struct nft_rule_dp { 1075 u64 is_last:1, 1076 dlen:12, 1077 handle:42; /* for tracing */ 1078 unsigned char data[] 1079 __attribute__((aligned(__alignof__(struct nft_expr)))); 1080}; 1081 1082struct nft_rule_dp_last { 1083 struct nft_rule_dp end; /* end of nft_rule_blob marker */ 1084 struct rcu_head h; /* call_rcu head */ 1085 struct nft_rule_blob *blob; /* ptr to free via call_rcu */ 1086 const struct nft_chain *chain; /* for nftables tracing */ 1087}; 1088 1089static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule) 1090{ 1091 return (void *)rule + sizeof(*rule) + rule->dlen; 1092} 1093 1094struct nft_rule_blob { 1095 unsigned long size; 1096 unsigned char data[] 1097 __attribute__((aligned(__alignof__(struct nft_rule_dp)))); 1098}; 1099 1100enum nft_chain_types { 1101 NFT_CHAIN_T_DEFAULT = 0, 1102 NFT_CHAIN_T_ROUTE, 1103 NFT_CHAIN_T_NAT, 1104 NFT_CHAIN_T_MAX 1105}; 1106 1107/** 1108 * struct nft_chain_validate_state - validation state 1109 * 1110 * If a chain is encountered again during table validation it is 1111 * possible to avoid revalidation provided the calling context is 1112 * compatible. This structure stores relevant calling context of 1113 * previous validations. 1114 * 1115 * @hook_mask: the hook numbers and locations the chain is linked to 1116 * @depth: the deepest call chain level the chain is linked to 1117 */ 1118struct nft_chain_validate_state { 1119 u8 hook_mask[NFT_CHAIN_T_MAX]; 1120 u8 depth; 1121}; 1122 1123/** 1124 * struct nft_chain - nf_tables chain 1125 * 1126 * @blob_gen_0: rule blob pointer to the current generation 1127 * @blob_gen_1: rule blob pointer to the future generation 1128 * @rules: list of rules in the chain 1129 * @list: used internally 1130 * @rhlhead: used internally 1131 * @table: table that this chain belongs to 1132 * @handle: chain handle 1133 * @use: number of jump references to this chain 1134 * @flags: bitmask of enum NFTA_CHAIN_FLAGS 1135 * @bound: bind or not 1136 * @genmask: generation mask 1137 * @name: name of the chain 1138 * @udlen: user data length 1139 * @udata: user data in the chain 1140 * @blob_next: rule blob pointer to the next in the chain 1141 * @vstate: validation state 1142 */ 1143struct nft_chain { 1144 struct nft_rule_blob __rcu *blob_gen_0; 1145 struct nft_rule_blob __rcu *blob_gen_1; 1146 struct list_head rules; 1147 struct list_head list; 1148 struct rhlist_head rhlhead; 1149 struct nft_table *table; 1150 u64 handle; 1151 u32 use; 1152 u8 flags:5, 1153 bound:1, 1154 genmask:2; 1155 char *name; 1156 u16 udlen; 1157 u8 *udata; 1158 1159 /* Only used during control plane commit phase: */ 1160 struct nft_rule_blob *blob_next; 1161 struct nft_chain_validate_state vstate; 1162}; 1163 1164int nft_chain_validate(const struct nft_ctx *ctx, struct nft_chain *chain); 1165int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set, 1166 const struct nft_set_iter *iter, 1167 struct nft_elem_priv *elem_priv); 1168int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set); 1169int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain); 1170void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain); 1171 1172/** 1173 * struct nft_chain_type - nf_tables chain type info 1174 * 1175 * @name: name of the type 1176 * @type: numeric identifier 1177 * @family: address family 1178 * @owner: module owner 1179 * @hook_mask: mask of valid hooks 1180 * @hooks: array of hook functions 1181 * @ops_register: base chain register function 1182 * @ops_unregister: base chain unregister function 1183 */ 1184struct nft_chain_type { 1185 const char *name; 1186 enum nft_chain_types type; 1187 int family; 1188 struct module *owner; 1189 unsigned int hook_mask; 1190 nf_hookfn *hooks[NFT_MAX_HOOKS]; 1191 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops); 1192 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops); 1193}; 1194 1195int nft_chain_validate_dependency(const struct nft_chain *chain, 1196 enum nft_chain_types type); 1197int nft_chain_validate_hooks(const struct nft_chain *chain, 1198 unsigned int hook_flags); 1199 1200static inline bool nft_chain_binding(const struct nft_chain *chain) 1201{ 1202 return chain->flags & NFT_CHAIN_BINDING; 1203} 1204 1205static inline bool nft_chain_is_bound(struct nft_chain *chain) 1206{ 1207 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound; 1208} 1209 1210int nft_chain_add(struct nft_table *table, struct nft_chain *chain); 1211void nft_chain_del(struct nft_chain *chain); 1212void nf_tables_chain_destroy(struct nft_chain *chain); 1213 1214struct nft_stats { 1215 u64 bytes; 1216 u64 pkts; 1217 struct u64_stats_sync syncp; 1218}; 1219 1220struct nft_hook { 1221 struct list_head list; 1222 struct list_head ops_list; 1223 struct rcu_head rcu; 1224 char ifname[IFNAMSIZ]; 1225 u8 ifnamelen; 1226}; 1227 1228struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook, 1229 const struct net_device *dev); 1230struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook, 1231 const struct net_device *dev); 1232 1233/** 1234 * struct nft_base_chain - nf_tables base chain 1235 * 1236 * @ops: netfilter hook ops 1237 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family) 1238 * @type: chain type 1239 * @policy: default policy 1240 * @flags: indicate the base chain disabled or not 1241 * @stats: per-cpu chain stats 1242 * @chain: the chain 1243 * @flow_block: flow block (for hardware offload) 1244 */ 1245struct nft_base_chain { 1246 struct nf_hook_ops ops; 1247 struct list_head hook_list; 1248 const struct nft_chain_type *type; 1249 u8 policy; 1250 u8 flags; 1251 struct nft_stats __percpu *stats; 1252 struct nft_chain chain; 1253 struct flow_block flow_block; 1254}; 1255 1256static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) 1257{ 1258 return container_of(chain, struct nft_base_chain, chain); 1259} 1260 1261static inline bool nft_is_base_chain(const struct nft_chain *chain) 1262{ 1263 return chain->flags & NFT_CHAIN_BASE; 1264} 1265 1266unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv); 1267 1268static inline bool nft_use_inc(u32 *use) 1269{ 1270 if (*use == UINT_MAX) 1271 return false; 1272 1273 (*use)++; 1274 1275 return true; 1276} 1277 1278static inline void nft_use_dec(u32 *use) 1279{ 1280 WARN_ON_ONCE((*use)-- == 0); 1281} 1282 1283/* For error and abort path: restore use counter to previous state. */ 1284static inline void nft_use_inc_restore(u32 *use) 1285{ 1286 WARN_ON_ONCE(!nft_use_inc(use)); 1287} 1288 1289#define nft_use_dec_restore nft_use_dec 1290 1291/** 1292 * struct nft_table - nf_tables table 1293 * 1294 * @list: used internally 1295 * @chains_ht: chains in the table 1296 * @chains: same, for stable walks 1297 * @sets: sets in the table 1298 * @objects: stateful objects in the table 1299 * @flowtables: flow tables in the table 1300 * @hgenerator: handle generator state 1301 * @handle: table handle 1302 * @use: number of chain references to this table 1303 * @family:address family 1304 * @flags: table flag (see enum nft_table_flags) 1305 * @genmask: generation mask 1306 * @nlpid: netlink port ID 1307 * @name: name of the table 1308 * @udlen: length of the user data 1309 * @udata: user data 1310 * @validate_state: internal, set when transaction adds jumps 1311 */ 1312struct nft_table { 1313 struct list_head list; 1314 struct rhltable chains_ht; 1315 struct list_head chains; 1316 struct list_head sets; 1317 struct list_head objects; 1318 struct list_head flowtables; 1319 u64 hgenerator; 1320 u64 handle; 1321 u32 use; 1322 u16 family:6, 1323 flags:8, 1324 genmask:2; 1325 u32 nlpid; 1326 char *name; 1327 u16 udlen; 1328 u8 *udata; 1329 u8 validate_state; 1330}; 1331 1332static inline bool nft_table_has_owner(const struct nft_table *table) 1333{ 1334 return table->flags & NFT_TABLE_F_OWNER; 1335} 1336 1337static inline bool nft_table_is_orphan(const struct nft_table *table) 1338{ 1339 return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) == 1340 NFT_TABLE_F_PERSIST; 1341} 1342 1343static inline bool nft_base_chain_netdev(int family, u32 hooknum) 1344{ 1345 return family == NFPROTO_NETDEV || 1346 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS); 1347} 1348 1349void nft_register_chain_type(const struct nft_chain_type *); 1350void nft_unregister_chain_type(const struct nft_chain_type *); 1351 1352int nft_register_expr(struct nft_expr_type *); 1353void nft_unregister_expr(struct nft_expr_type *); 1354 1355int nft_verdict_dump(struct sk_buff *skb, int type, 1356 const struct nft_verdict *v); 1357 1358/** 1359 * struct nft_object_hash_key - key to lookup nft_object 1360 * 1361 * @name: name of the stateful object to look up 1362 * @table: table the object belongs to 1363 */ 1364struct nft_object_hash_key { 1365 const char *name; 1366 const struct nft_table *table; 1367}; 1368 1369/** 1370 * struct nft_object - nf_tables stateful object 1371 * 1372 * @list: table stateful object list node 1373 * @rhlhead: nft_objname_ht node 1374 * @key: keys that identify this object 1375 * @genmask: generation mask 1376 * @use: number of references to this stateful object 1377 * @handle: unique object handle 1378 * @udlen: length of user data 1379 * @udata: user data 1380 * @ops: object operations 1381 * @data: object data, layout depends on type 1382 */ 1383struct nft_object { 1384 struct list_head list; 1385 struct rhlist_head rhlhead; 1386 struct nft_object_hash_key key; 1387 u32 genmask:2; 1388 u32 use; 1389 u64 handle; 1390 u16 udlen; 1391 u8 *udata; 1392 /* runtime data below here */ 1393 const struct nft_object_ops *ops ____cacheline_aligned; 1394 unsigned char data[] 1395 __attribute__((aligned(__alignof__(u64)))); 1396}; 1397 1398static inline void *nft_obj_data(const struct nft_object *obj) 1399{ 1400 return (void *)obj->data; 1401} 1402 1403#define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr)) 1404 1405struct nft_object *nft_obj_lookup(const struct net *net, 1406 const struct nft_table *table, 1407 const struct nlattr *nla, u32 objtype, 1408 u8 genmask); 1409 1410void nft_obj_notify(struct net *net, const struct nft_table *table, 1411 struct nft_object *obj, u32 portid, u32 seq, 1412 int event, u16 flags, int family, int report, gfp_t gfp); 1413 1414/** 1415 * struct nft_object_type - stateful object type 1416 * 1417 * @select_ops: function to select nft_object_ops 1418 * @ops: default ops, used when no select_ops functions is present 1419 * @list: list node in list of object types 1420 * @type: stateful object numeric type 1421 * @owner: module owner 1422 * @maxattr: maximum netlink attribute 1423 * @family: address family for AF-specific object types 1424 * @policy: netlink attribute policy 1425 */ 1426struct nft_object_type { 1427 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *, 1428 const struct nlattr * const tb[]); 1429 const struct nft_object_ops *ops; 1430 struct list_head list; 1431 u32 type; 1432 unsigned int maxattr; 1433 u8 family; 1434 struct module *owner; 1435 const struct nla_policy *policy; 1436}; 1437 1438/** 1439 * struct nft_object_ops - stateful object operations 1440 * 1441 * @eval: stateful object evaluation function 1442 * @size: stateful object size 1443 * @init: initialize object from netlink attributes 1444 * @destroy: release existing stateful object 1445 * @dump: netlink dump stateful object 1446 * @update: update stateful object 1447 * @type: pointer to object type 1448 */ 1449struct nft_object_ops { 1450 void (*eval)(struct nft_object *obj, 1451 struct nft_regs *regs, 1452 const struct nft_pktinfo *pkt); 1453 unsigned int size; 1454 int (*init)(const struct nft_ctx *ctx, 1455 const struct nlattr *const tb[], 1456 struct nft_object *obj); 1457 void (*destroy)(const struct nft_ctx *ctx, 1458 struct nft_object *obj); 1459 int (*dump)(struct sk_buff *skb, 1460 struct nft_object *obj, 1461 bool reset); 1462 void (*update)(struct nft_object *obj, 1463 struct nft_object *newobj); 1464 const struct nft_object_type *type; 1465}; 1466 1467int nft_register_obj(struct nft_object_type *obj_type); 1468void nft_unregister_obj(struct nft_object_type *obj_type); 1469 1470#define NFT_NETDEVICE_MAX 256 1471 1472/** 1473 * struct nft_flowtable - nf_tables flow table 1474 * 1475 * @list: flow table list node in table list 1476 * @table: the table the flow table is contained in 1477 * @name: name of this flow table 1478 * @hooknum: hook number 1479 * @ops_len: number of hooks in array 1480 * @genmask: generation mask 1481 * @use: number of references to this flow table 1482 * @handle: unique object handle 1483 * @hook_list: hook list for hooks per net_device in flowtables 1484 * @data: rhashtable and garbage collector 1485 */ 1486struct nft_flowtable { 1487 struct list_head list; 1488 struct nft_table *table; 1489 char *name; 1490 int hooknum; 1491 int ops_len; 1492 u32 genmask:2; 1493 u32 use; 1494 u64 handle; 1495 /* runtime data below here */ 1496 struct list_head hook_list ____cacheline_aligned; 1497 struct nf_flowtable data; 1498}; 1499 1500struct nft_flowtable *nft_flowtable_lookup(const struct net *net, 1501 const struct nft_table *table, 1502 const struct nlattr *nla, 1503 u8 genmask); 1504 1505void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx, 1506 struct nft_flowtable *flowtable, 1507 enum nft_trans_phase phase); 1508 1509void nft_register_flowtable_type(struct nf_flowtable_type *type); 1510void nft_unregister_flowtable_type(struct nf_flowtable_type *type); 1511 1512/** 1513 * struct nft_traceinfo - nft tracing information and state 1514 * 1515 * @trace: other struct members are initialised 1516 * @nf_trace: copy of skb->nf_trace before rule evaluation 1517 * @type: event type (enum nft_trace_types) 1518 * @skbid: hash of skb to be used as trace id 1519 * @packet_dumped: packet headers sent in a previous traceinfo message 1520 * @basechain: base chain currently processed 1521 */ 1522struct nft_traceinfo { 1523 bool trace; 1524 bool nf_trace; 1525 bool packet_dumped; 1526 enum nft_trace_types type:8; 1527 u32 skbid; 1528 const struct nft_base_chain *basechain; 1529}; 1530 1531void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, 1532 const struct nft_chain *basechain); 1533 1534void nft_trace_notify(const struct nft_pktinfo *pkt, 1535 const struct nft_verdict *verdict, 1536 const struct nft_rule_dp *rule, 1537 struct nft_traceinfo *info); 1538 1539#define MODULE_ALIAS_NFT_CHAIN(family, name) \ 1540 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name) 1541 1542#define MODULE_ALIAS_NFT_AF_EXPR(family, name) \ 1543 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name) 1544 1545#define MODULE_ALIAS_NFT_EXPR(name) \ 1546 MODULE_ALIAS("nft-expr-" name) 1547 1548#define MODULE_ALIAS_NFT_OBJ(type) \ 1549 MODULE_ALIAS("nft-obj-" __stringify(type)) 1550 1551#if IS_ENABLED(CONFIG_NF_TABLES) 1552 1553/* 1554 * The gencursor defines two generations, the currently active and the 1555 * next one. Objects contain a bitmask of 2 bits specifying the generations 1556 * they're active in. A set bit means they're inactive in the generation 1557 * represented by that bit. 1558 * 1559 * New objects start out as inactive in the current and active in the 1560 * next generation. When committing the ruleset the bitmask is cleared, 1561 * meaning they're active in all generations. When removing an object, 1562 * it is set inactive in the next generation. After committing the ruleset, 1563 * the objects are removed. 1564 */ 1565static inline unsigned int nft_gencursor_next(const struct net *net) 1566{ 1567 return net->nft.gencursor + 1 == 1 ? 1 : 0; 1568} 1569 1570static inline u8 nft_genmask_next(const struct net *net) 1571{ 1572 return 1 << nft_gencursor_next(net); 1573} 1574 1575static inline u8 nft_genmask_cur(const struct net *net) 1576{ 1577 /* Use READ_ONCE() to prevent refetching the value for atomicity */ 1578 return 1 << READ_ONCE(net->nft.gencursor); 1579} 1580 1581#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) 1582 1583/* 1584 * Generic transaction helpers 1585 */ 1586 1587/* Check if this object is currently active. */ 1588#define nft_is_active(__net, __obj) \ 1589 (((__obj)->genmask & nft_genmask_cur(__net)) == 0) 1590 1591/* Check if this object is active in the next generation. */ 1592#define nft_is_active_next(__net, __obj) \ 1593 (((__obj)->genmask & nft_genmask_next(__net)) == 0) 1594 1595/* This object becomes active in the next generation. */ 1596#define nft_activate_next(__net, __obj) \ 1597 (__obj)->genmask = nft_genmask_cur(__net) 1598 1599/* This object becomes inactive in the next generation. */ 1600#define nft_deactivate_next(__net, __obj) \ 1601 (__obj)->genmask = nft_genmask_next(__net) 1602 1603/* After committing the ruleset, clear the stale generation bit. */ 1604#define nft_clear(__net, __obj) \ 1605 (__obj)->genmask &= ~nft_genmask_next(__net) 1606#define nft_active_genmask(__obj, __genmask) \ 1607 !((__obj)->genmask & __genmask) 1608 1609/* 1610 * Set element transaction helpers 1611 */ 1612 1613static inline bool nft_set_elem_active(const struct nft_set_ext *ext, 1614 u8 genmask) 1615{ 1616 return !(ext->genmask & genmask); 1617} 1618 1619static inline void nft_set_elem_change_active(const struct net *net, 1620 const struct nft_set *set, 1621 struct nft_set_ext *ext) 1622{ 1623 ext->genmask ^= nft_genmask_next(net); 1624} 1625 1626#endif /* IS_ENABLED(CONFIG_NF_TABLES) */ 1627 1628#define NFT_SET_ELEM_DEAD_MASK (1 << 2) 1629 1630#if defined(__LITTLE_ENDIAN_BITFIELD) 1631#define NFT_SET_ELEM_DEAD_BIT 2 1632#elif defined(__BIG_ENDIAN_BITFIELD) 1633#define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2) 1634#else 1635#error 1636#endif 1637 1638static inline void nft_set_elem_dead(struct nft_set_ext *ext) 1639{ 1640 unsigned long *word = (unsigned long *)ext; 1641 1642 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); 1643 set_bit(NFT_SET_ELEM_DEAD_BIT, word); 1644} 1645 1646static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext) 1647{ 1648 unsigned long *word = (unsigned long *)ext; 1649 1650 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0); 1651 return test_bit(NFT_SET_ELEM_DEAD_BIT, word); 1652} 1653 1654/** 1655 * struct nft_trans - nf_tables object update in transaction 1656 * 1657 * @list: used internally 1658 * @net: struct net 1659 * @table: struct nft_table the object resides in 1660 * @msg_type: message type 1661 * @seq: netlink sequence number 1662 * @flags: modifiers to new request 1663 * @report: notify via unicast netlink message 1664 * @put_net: net needs to be put 1665 * 1666 * This is the information common to all objects in the transaction, 1667 * this must always be the first member of derived sub-types. 1668 */ 1669struct nft_trans { 1670 struct list_head list; 1671 struct net *net; 1672 struct nft_table *table; 1673 int msg_type; 1674 u32 seq; 1675 u16 flags; 1676 u8 report:1; 1677 u8 put_net:1; 1678}; 1679 1680/** 1681 * struct nft_trans_binding - nf_tables object with binding support in transaction 1682 * @nft_trans: base structure, MUST be first member 1683 * @binding_list: list of objects with possible bindings 1684 * 1685 * This is the base type used by objects that can be bound to a chain. 1686 */ 1687struct nft_trans_binding { 1688 struct nft_trans nft_trans; 1689 struct list_head binding_list; 1690}; 1691 1692struct nft_trans_rule { 1693 struct nft_trans nft_trans; 1694 struct nft_rule *rule; 1695 struct nft_chain *chain; 1696 struct nft_flow_rule *flow; 1697 u32 rule_id; 1698 bool bound; 1699}; 1700 1701#define nft_trans_container_rule(trans) \ 1702 container_of(trans, struct nft_trans_rule, nft_trans) 1703#define nft_trans_rule(trans) \ 1704 nft_trans_container_rule(trans)->rule 1705#define nft_trans_flow_rule(trans) \ 1706 nft_trans_container_rule(trans)->flow 1707#define nft_trans_rule_id(trans) \ 1708 nft_trans_container_rule(trans)->rule_id 1709#define nft_trans_rule_bound(trans) \ 1710 nft_trans_container_rule(trans)->bound 1711#define nft_trans_rule_chain(trans) \ 1712 nft_trans_container_rule(trans)->chain 1713 1714struct nft_trans_set { 1715 struct nft_trans_binding nft_trans_binding; 1716 struct list_head list_trans_newset; 1717 struct nft_set *set; 1718 u32 set_id; 1719 u32 gc_int; 1720 u64 timeout; 1721 bool update; 1722 bool bound; 1723 u32 size; 1724}; 1725 1726#define nft_trans_container_set(t) \ 1727 container_of(t, struct nft_trans_set, nft_trans_binding.nft_trans) 1728#define nft_trans_set(trans) \ 1729 nft_trans_container_set(trans)->set 1730#define nft_trans_set_id(trans) \ 1731 nft_trans_container_set(trans)->set_id 1732#define nft_trans_set_bound(trans) \ 1733 nft_trans_container_set(trans)->bound 1734#define nft_trans_set_update(trans) \ 1735 nft_trans_container_set(trans)->update 1736#define nft_trans_set_timeout(trans) \ 1737 nft_trans_container_set(trans)->timeout 1738#define nft_trans_set_gc_int(trans) \ 1739 nft_trans_container_set(trans)->gc_int 1740#define nft_trans_set_size(trans) \ 1741 nft_trans_container_set(trans)->size 1742 1743struct nft_trans_chain { 1744 struct nft_trans_binding nft_trans_binding; 1745 struct nft_chain *chain; 1746 char *name; 1747 struct nft_stats __percpu *stats; 1748 u8 policy; 1749 bool update; 1750 bool bound; 1751 u32 chain_id; 1752 struct nft_base_chain *basechain; 1753 struct list_head hook_list; 1754}; 1755 1756#define nft_trans_container_chain(t) \ 1757 container_of(t, struct nft_trans_chain, nft_trans_binding.nft_trans) 1758#define nft_trans_chain(trans) \ 1759 nft_trans_container_chain(trans)->chain 1760#define nft_trans_chain_update(trans) \ 1761 nft_trans_container_chain(trans)->update 1762#define nft_trans_chain_name(trans) \ 1763 nft_trans_container_chain(trans)->name 1764#define nft_trans_chain_stats(trans) \ 1765 nft_trans_container_chain(trans)->stats 1766#define nft_trans_chain_policy(trans) \ 1767 nft_trans_container_chain(trans)->policy 1768#define nft_trans_chain_bound(trans) \ 1769 nft_trans_container_chain(trans)->bound 1770#define nft_trans_chain_id(trans) \ 1771 nft_trans_container_chain(trans)->chain_id 1772#define nft_trans_basechain(trans) \ 1773 nft_trans_container_chain(trans)->basechain 1774#define nft_trans_chain_hooks(trans) \ 1775 nft_trans_container_chain(trans)->hook_list 1776 1777struct nft_trans_table { 1778 struct nft_trans nft_trans; 1779 bool update; 1780}; 1781 1782#define nft_trans_container_table(trans) \ 1783 container_of(trans, struct nft_trans_table, nft_trans) 1784#define nft_trans_table_update(trans) \ 1785 nft_trans_container_table(trans)->update 1786 1787enum nft_trans_elem_flags { 1788 NFT_TRANS_UPD_TIMEOUT = (1 << 0), 1789 NFT_TRANS_UPD_EXPIRATION = (1 << 1), 1790}; 1791 1792struct nft_elem_update { 1793 u64 timeout; 1794 u64 expiration; 1795 u8 flags; 1796}; 1797 1798struct nft_trans_one_elem { 1799 struct nft_elem_priv *priv; 1800 struct nft_elem_update *update; 1801}; 1802 1803struct nft_trans_elem { 1804 struct nft_trans nft_trans; 1805 struct nft_set *set; 1806 bool bound; 1807 unsigned int nelems; 1808 struct nft_trans_one_elem elems[] __counted_by(nelems); 1809}; 1810 1811#define nft_trans_container_elem(t) \ 1812 container_of(t, struct nft_trans_elem, nft_trans) 1813#define nft_trans_elem_set(trans) \ 1814 nft_trans_container_elem(trans)->set 1815#define nft_trans_elem_set_bound(trans) \ 1816 nft_trans_container_elem(trans)->bound 1817 1818struct nft_trans_obj { 1819 struct nft_trans nft_trans; 1820 struct nft_object *obj; 1821 struct nft_object *newobj; 1822 bool update; 1823}; 1824 1825#define nft_trans_container_obj(t) \ 1826 container_of(t, struct nft_trans_obj, nft_trans) 1827#define nft_trans_obj(trans) \ 1828 nft_trans_container_obj(trans)->obj 1829#define nft_trans_obj_newobj(trans) \ 1830 nft_trans_container_obj(trans)->newobj 1831#define nft_trans_obj_update(trans) \ 1832 nft_trans_container_obj(trans)->update 1833 1834struct nft_trans_flowtable { 1835 struct nft_trans nft_trans; 1836 struct nft_flowtable *flowtable; 1837 struct list_head hook_list; 1838 u32 flags; 1839 bool update; 1840}; 1841 1842#define nft_trans_container_flowtable(t) \ 1843 container_of(t, struct nft_trans_flowtable, nft_trans) 1844#define nft_trans_flowtable(trans) \ 1845 nft_trans_container_flowtable(trans)->flowtable 1846#define nft_trans_flowtable_update(trans) \ 1847 nft_trans_container_flowtable(trans)->update 1848#define nft_trans_flowtable_hooks(trans) \ 1849 nft_trans_container_flowtable(trans)->hook_list 1850#define nft_trans_flowtable_flags(trans) \ 1851 nft_trans_container_flowtable(trans)->flags 1852 1853#define NFT_TRANS_GC_BATCHCOUNT 256 1854 1855struct nft_trans_gc { 1856 struct list_head list; 1857 struct net *net; 1858 struct nft_set *set; 1859 u32 seq; 1860 u16 count; 1861 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT]; 1862 struct rcu_head rcu; 1863}; 1864 1865static inline int nft_trans_gc_space(const struct nft_trans_gc *trans) 1866{ 1867 return NFT_TRANS_GC_BATCHCOUNT - trans->count; 1868} 1869 1870static inline void nft_ctx_update(struct nft_ctx *ctx, 1871 const struct nft_trans *trans) 1872{ 1873 switch (trans->msg_type) { 1874 case NFT_MSG_NEWRULE: 1875 case NFT_MSG_DELRULE: 1876 case NFT_MSG_DESTROYRULE: 1877 ctx->chain = nft_trans_rule_chain(trans); 1878 break; 1879 case NFT_MSG_NEWCHAIN: 1880 case NFT_MSG_DELCHAIN: 1881 case NFT_MSG_DESTROYCHAIN: 1882 ctx->chain = nft_trans_chain(trans); 1883 break; 1884 default: 1885 ctx->chain = NULL; 1886 break; 1887 } 1888 1889 ctx->net = trans->net; 1890 ctx->table = trans->table; 1891 ctx->family = trans->table->family; 1892 ctx->report = trans->report; 1893 ctx->flags = trans->flags; 1894 ctx->seq = trans->seq; 1895} 1896 1897struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set, 1898 unsigned int gc_seq, gfp_t gfp); 1899void nft_trans_gc_destroy(struct nft_trans_gc *trans); 1900 1901struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc, 1902 unsigned int gc_seq, gfp_t gfp); 1903void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc); 1904 1905struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp); 1906void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans); 1907 1908void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv); 1909 1910struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc, 1911 unsigned int gc_seq); 1912struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc); 1913 1914void nft_setelem_data_deactivate(const struct net *net, 1915 const struct nft_set *set, 1916 struct nft_elem_priv *elem_priv); 1917 1918int __init nft_chain_filter_init(void); 1919void nft_chain_filter_fini(void); 1920 1921void __init nft_chain_route_init(void); 1922void nft_chain_route_fini(void); 1923 1924void nf_tables_trans_destroy_flush_work(struct net *net); 1925 1926int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result); 1927__be64 nf_jiffies64_to_msecs(u64 input); 1928 1929#ifdef CONFIG_MODULES 1930__printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...); 1931#else 1932static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; } 1933#endif 1934 1935struct nftables_pernet { 1936 struct list_head tables; 1937 struct list_head commit_list; 1938 struct list_head destroy_list; 1939 struct list_head commit_set_list; 1940 struct list_head binding_list; 1941 struct list_head module_list; 1942 struct list_head notify_list; 1943 struct mutex commit_mutex; 1944 u64 table_handle; 1945 u64 tstamp; 1946 unsigned int gc_seq; 1947 u8 validate_state; 1948 struct work_struct destroy_work; 1949}; 1950 1951extern unsigned int nf_tables_net_id; 1952 1953static inline struct nftables_pernet *nft_pernet(const struct net *net) 1954{ 1955 return net_generic(net, nf_tables_net_id); 1956} 1957 1958static inline u64 nft_net_tstamp(const struct net *net) 1959{ 1960 return nft_pernet(net)->tstamp; 1961} 1962 1963#define __NFT_REDUCE_READONLY 1UL 1964#define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY 1965 1966void nft_reg_track_update(struct nft_regs_track *track, 1967 const struct nft_expr *expr, u8 dreg, u8 len); 1968void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len); 1969void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg); 1970 1971static inline bool nft_reg_track_cmp(struct nft_regs_track *track, 1972 const struct nft_expr *expr, u8 dreg) 1973{ 1974 return track->regs[dreg].selector && 1975 track->regs[dreg].selector->ops == expr->ops && 1976 track->regs[dreg].num_reg == 0; 1977} 1978 1979#endif /* _NET_NF_TABLES_H */
~/
Read-only file preview.