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