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