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