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 __be16 ethertype; 35 u16 fragoff; 36 u16 nhoff; 37 u16 thoff; 38 u16 inneroff; 39 }; 40 41 static inline struct sock *nft_sk(const struct nft_pktinfo *pkt) 42 { 43 return pkt->state->sk; 44 } 45 46 static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt) 47 { 48 return pkt->thoff; 49 } 50 51 static inline struct net *nft_net(const struct nft_pktinfo *pkt) 52 { 53 return pkt->state->net; 54 } 55 56 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt) 57 { 58 return pkt->state->hook; 59 } 60 61 static inline u8 nft_pf(const struct nft_pktinfo *pkt) 62 { 63 return pkt->state->pf; 64 } 65 66 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt) 67 { 68 return pkt->state->in; 69 } 70 71 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt) 72 { 73 return pkt->state->out; 74 } 75 76 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt, 77 struct sk_buff *skb, 78 const struct nf_hook_state *state) 79 { 80 pkt->skb = skb; 81 pkt->state = state; 82 } 83 84 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt) 85 { 86 pkt->flags = 0; 87 pkt->tprot = 0; 88 pkt->ethertype = pkt->skb->protocol; 89 pkt->nhoff = 0; 90 pkt->thoff = 0; 91 pkt->fragoff = 0; 92 } 93 94 /** 95 * struct nft_verdict - nf_tables verdict 96 * 97 * @code: nf_tables/netfilter verdict code 98 * @chain: destination chain for NFT_JUMP/NFT_GOTO 99 */ 100 struct nft_verdict { 101 u32 code; 102 struct nft_chain *chain; 103 }; 104 105 struct nft_data { 106 union { 107 u32 data[4]; 108 struct nft_verdict verdict; 109 }; 110 } __attribute__((aligned(__alignof__(u64)))); 111 112 #define NFT_REG32_NUM 20 113 114 /** 115 * struct nft_regs - nf_tables register set 116 * 117 * @data: data registers 118 * @verdict: verdict register 119 * 120 * The first four data registers alias to the verdict register. 121 */ 122 struct nft_regs { 123 union { 124 u32 data[NFT_REG32_NUM]; 125 struct nft_verdict verdict; 126 }; 127 }; 128 129 /* Store/load an u8, u16 or u64 integer to/from the u32 data register. 130 * 131 * Note, when using concatenations, register allocation happens at 32-bit 132 * level. So for store instruction, pad the rest part with zero to avoid 133 * garbage values. 134 */ 135 136 static inline void nft_reg_store8(u32 *dreg, u8 val) 137 { 138 *dreg = 0; 139 *(u8 *)dreg = val; 140 } 141 142 static inline u8 nft_reg_load8(const u32 *sreg) 143 { 144 return *(u8 *)sreg; 145 } 146 147 static inline void nft_reg_store16(u32 *dreg, u16 val) 148 { 149 *dreg = 0; 150 *(u16 *)dreg = val; 151 } 152 153 static inline void nft_reg_store_be16(u32 *dreg, __be16 val) 154 { 155 nft_reg_store16(dreg, (__force __u16)val); 156 } 157 158 static inline u16 nft_reg_load16(const u32 *sreg) 159 { 160 return *(u16 *)sreg; 161 } 162 163 static inline __be16 nft_reg_load_be16(const u32 *sreg) 164 { 165 return (__force __be16)nft_reg_load16(sreg); 166 } 167 168 static inline __be32 nft_reg_load_be32(const u32 *sreg) 169 { 170 return *(__force __be32 *)sreg; 171 } 172 173 static inline void nft_reg_store64(u64 *dreg, u64 val) 174 { 175 put_unaligned(val, dreg); 176 } 177 178 static inline u64 nft_reg_load64(const u32 *sreg) 179 { 180 return get_unaligned((u64 *)sreg); 181 } 182 183 static inline bool nft_reg_overlap(u8 src, u8 dst, u32 len) 184 { 185 unsigned int n = DIV_ROUND_UP(len, sizeof(u32)); 186 187 return src != dst && src < dst + n && dst < src + n; 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 /* placeholder structure for opaque set element backend representation. */ 281 struct nft_elem_priv { }; 282 283 /** 284 * struct nft_set_elem - generic representation of set elements 285 * 286 * @key: element key 287 * @key_end: closing element key 288 * @data: element data 289 * @priv: element private data and extensions 290 */ 291 struct nft_set_elem { 292 union { 293 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 294 struct nft_data val; 295 } key; 296 union { 297 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 298 struct nft_data val; 299 } key_end; 300 union { 301 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)]; 302 struct nft_data val; 303 } data; 304 struct nft_elem_priv *priv; 305 }; 306 307 static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv) 308 { 309 return (void *)priv; 310 } 311 312 313 /** 314 * enum nft_iter_type - nftables set iterator type 315 * 316 * @NFT_ITER_UNSPEC: unspecified, to catch errors 317 * @NFT_ITER_READ: read-only iteration over set elements 318 * @NFT_ITER_UPDATE: iteration under mutex to update set element state 319 * @NFT_ITER_UPDATE_CLONE: clone set before iteration under mutex to update element 320 */ 321 enum nft_iter_type { 322 NFT_ITER_UNSPEC, 323 NFT_ITER_READ, 324 NFT_ITER_UPDATE, 325 NFT_ITER_UPDATE_CLONE, 326 }; 327 328 struct nft_set; 329 struct nft_set_iter { 330 u8 genmask; 331 enum nft_iter_type type:8; 332 unsigned int count; 333 unsigned int skip; 334 int err; 335 int (*fn)(const struct nft_ctx *ctx, 336 struct nft_set *set, 337 const struct nft_set_iter *iter, 338 struct nft_elem_priv *elem_priv); 339 }; 340 341 /** 342 * struct nft_set_desc - description of set elements 343 * 344 * @ktype: key type 345 * @klen: key length 346 * @dtype: data type 347 * @dlen: data length 348 * @objtype: object type 349 * @size: number of set elements 350 * @policy: set policy 351 * @gc_int: garbage collector interval 352 * @timeout: element timeout 353 * @field_len: length of each field in concatenation, bytes 354 * @field_count: number of concatenated fields in element 355 * @expr: set must support for expressions 356 */ 357 struct nft_set_desc { 358 u32 ktype; 359 unsigned int klen; 360 u32 dtype; 361 unsigned int dlen; 362 u32 objtype; 363 unsigned int size; 364 u32 policy; 365 u32 gc_int; 366 u64 timeout; 367 u8 field_len[NFT_REG32_COUNT]; 368 u8 field_count; 369 bool expr; 370 }; 371 372 /** 373 * enum nft_set_class - performance class 374 * 375 * @NFT_SET_CLASS_O_1: constant, O(1) 376 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N) 377 * @NFT_SET_CLASS_O_N: linear, O(N) 378 */ 379 enum nft_set_class { 380 NFT_SET_CLASS_O_1, 381 NFT_SET_CLASS_O_LOG_N, 382 NFT_SET_CLASS_O_N, 383 }; 384 385 /** 386 * struct nft_set_estimate - estimation of memory and performance 387 * characteristics 388 * 389 * @size: required memory 390 * @lookup: lookup performance class 391 * @space: memory class 392 */ 393 struct nft_set_estimate { 394 u64 size; 395 enum nft_set_class lookup; 396 enum nft_set_class space; 397 }; 398 399 #define NFT_EXPR_MAXATTR 16 400 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \ 401 ALIGN(size, __alignof__(struct nft_expr))) 402 403 /** 404 * struct nft_expr - nf_tables expression 405 * 406 * @ops: expression ops 407 * @data: expression private data 408 */ 409 struct nft_expr { 410 const struct nft_expr_ops *ops; 411 unsigned char data[] 412 __attribute__((aligned(__alignof__(u64)))); 413 }; 414 415 static inline void *nft_expr_priv(const struct nft_expr *expr) 416 { 417 return (void *)expr->data; 418 } 419 420 struct nft_expr_info; 421 422 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla, 423 struct nft_expr_info *info); 424 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp); 425 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr); 426 int nft_expr_dump(struct sk_buff *skb, unsigned int attr, 427 const struct nft_expr *expr, bool reset); 428 429 struct nft_set_ext; 430 431 /** 432 * struct nft_set_ops - nf_tables set operations 433 * 434 * @lookup: look up an element within the set 435 * @update: update an element if exists, add it if doesn't exist 436 * @delete: delete an element 437 * @insert: insert new element into set 438 * @activate: activate new element in the next generation 439 * @deactivate: lookup for element and deactivate it in the next generation 440 * @flush: deactivate element in the next generation 441 * @remove: remove element from set 442 * @walk: iterate over all set elements 443 * @get: get set elements 444 * @ksize: kernel set size 445 * @usize: userspace set size 446 * @adjust_maxsize: delta to adjust maximum set size 447 * @commit: commit set elements 448 * @abort: abort set elements 449 * @privsize: function to return size of set private data 450 * @estimate: estimate the required memory size and the lookup complexity class 451 * @init: initialize private data of new set instance 452 * @destroy: destroy private data of set instance 453 * @gc_init: initialize garbage collection 454 * @abort_skip_removal: skip removal of elements from abort path 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 bool abort_skip_removal; 513 unsigned int elemsize; 514 }; 515 516 /** 517 * struct nft_set_type - nf_tables set type 518 * 519 * @ops: set ops for this type 520 * @features: features supported by the implementation 521 */ 522 struct nft_set_type { 523 const struct nft_set_ops ops; 524 u32 features; 525 }; 526 #define to_set_type(o) container_of(o, struct nft_set_type, ops) 527 528 struct nft_set_elem_expr { 529 u8 size; 530 unsigned char data[] 531 __attribute__((aligned(__alignof__(struct nft_expr)))); 532 }; 533 534 #define nft_setelem_expr_at(__elem_expr, __offset) \ 535 ((struct nft_expr *)&__elem_expr->data[__offset]) 536 537 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \ 538 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \ 539 __size < (__elem_expr)->size; \ 540 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size) 541 542 #define NFT_SET_EXPR_MAX 2 543 544 /** 545 * struct nft_set - nf_tables set instance 546 * 547 * @list: table set list node 548 * @bindings: list of set bindings 549 * @refs: internal refcounting for async set destruction 550 * @table: table this set belongs to 551 * @net: netnamespace this set belongs to 552 * @name: name of the set 553 * @handle: unique handle of the set 554 * @ktype: key type (numeric type defined by userspace, not used in the kernel) 555 * @dtype: data type (verdict or numeric type defined by userspace) 556 * @objtype: object type (see NFT_OBJECT_* definitions) 557 * @size: maximum set size 558 * @field_len: length of each field in concatenation, bytes 559 * @field_count: number of concatenated fields in element 560 * @in_update_walk: true during ->walk() in transaction phase 561 * @use: number of rules references to this set 562 * @nelems: number of elements 563 * @ndeact: number of deactivated elements queued for removal 564 * @timeout: default timeout value in jiffies 565 * @gc_int: garbage collection interval in msecs 566 * @policy: set parameterization (see enum nft_set_policies) 567 * @udlen: user data length 568 * @udata: user data 569 * @pending_update: list of pending update set element 570 * @ops: set ops 571 * @flags: set flags 572 * @dead: set will be freed, never cleared 573 * @genmask: generation mask 574 * @klen: key length 575 * @dlen: data length 576 * @num_exprs: numbers of exprs 577 * @exprs: stateful expression 578 * @catchall_list: list of catch-all set element 579 * @data: private set data 580 */ 581 struct nft_set { 582 struct list_head list; 583 struct list_head bindings; 584 refcount_t refs; 585 struct nft_table *table; 586 possible_net_t net; 587 char *name; 588 u64 handle; 589 u32 ktype; 590 u32 dtype; 591 u32 objtype; 592 u32 size; 593 u8 field_len[NFT_REG32_COUNT]; 594 u8 field_count; 595 bool in_update_walk; 596 u32 use; 597 atomic_t nelems; 598 u32 ndeact; 599 u64 timeout; 600 u32 gc_int; 601 u16 policy; 602 u16 udlen; 603 unsigned char *udata; 604 struct list_head pending_update; 605 /* runtime data below here */ 606 const struct nft_set_ops *ops ____cacheline_aligned; 607 u16 flags:13, 608 dead:1, 609 genmask:2; 610 u8 klen; 611 u8 dlen; 612 u8 num_exprs; 613 struct nft_expr *exprs[NFT_SET_EXPR_MAX]; 614 struct list_head catchall_list; 615 unsigned char data[] 616 __attribute__((aligned(__alignof__(u64)))); 617 }; 618 619 static inline bool nft_set_is_anonymous(const struct nft_set *set) 620 { 621 return set->flags & NFT_SET_ANONYMOUS; 622 } 623 624 static inline void *nft_set_priv(const struct nft_set *set) 625 { 626 return (void *)set->data; 627 } 628 629 static inline enum nft_data_types nft_set_datatype(const struct nft_set *set) 630 { 631 return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE; 632 } 633 634 static inline bool nft_set_gc_is_pending(const struct nft_set *s) 635 { 636 return refcount_read(&s->refs) != 1; 637 } 638 639 static inline struct nft_set *nft_set_container_of(const void *priv) 640 { 641 return (void *)priv - offsetof(struct nft_set, data); 642 } 643 644 struct nft_set *nft_set_lookup_global(const struct net *net, 645 const struct nft_table *table, 646 const struct nlattr *nla_set_name, 647 const struct nlattr *nla_set_id, 648 u8 genmask); 649 650 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net, 651 const struct nft_set *set); 652 653 static inline unsigned long nft_set_gc_interval(const struct nft_set *set) 654 { 655 u32 gc_int = READ_ONCE(set->gc_int); 656 657 return gc_int ? msecs_to_jiffies(gc_int) : HZ; 658 } 659 660 /** 661 * struct nft_set_binding - nf_tables set binding 662 * 663 * @list: set bindings list node 664 * @chain: chain containing the rule bound to the set 665 * @flags: set action flags 666 * 667 * A set binding contains all information necessary for validation 668 * of new elements added to a bound set. 669 */ 670 struct nft_set_binding { 671 struct list_head list; 672 const struct nft_chain *chain; 673 u32 flags; 674 }; 675 676 enum nft_trans_phase; 677 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set); 678 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set, 679 struct nft_set_binding *binding, 680 enum nft_trans_phase phase); 681 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, 682 struct nft_set_binding *binding); 683 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set); 684 685 /** 686 * enum nft_set_extensions - set extension type IDs 687 * 688 * @NFT_SET_EXT_KEY: element key 689 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges 690 * @NFT_SET_EXT_DATA: mapping data 691 * @NFT_SET_EXT_FLAGS: element flags 692 * @NFT_SET_EXT_TIMEOUT: element timeout 693 * @NFT_SET_EXT_USERDATA: user data associated with the element 694 * @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element 695 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element 696 * @NFT_SET_EXT_NUM: number of extension types 697 */ 698 enum nft_set_extensions { 699 NFT_SET_EXT_KEY, 700 NFT_SET_EXT_KEY_END, 701 NFT_SET_EXT_DATA, 702 NFT_SET_EXT_FLAGS, 703 NFT_SET_EXT_TIMEOUT, 704 NFT_SET_EXT_USERDATA, 705 NFT_SET_EXT_EXPRESSIONS, 706 NFT_SET_EXT_OBJREF, 707 NFT_SET_EXT_NUM 708 }; 709 710 /** 711 * struct nft_set_ext_type - set extension type 712 * 713 * @len: fixed part length of the extension 714 * @align: alignment requirements of the extension 715 */ 716 struct nft_set_ext_type { 717 u8 len; 718 u8 align; 719 }; 720 721 extern const struct nft_set_ext_type nft_set_ext_types[]; 722 723 /** 724 * struct nft_set_ext_tmpl - set extension template 725 * 726 * @len: length of extension area 727 * @offset: offsets of individual extension types 728 * @ext_len: length of the expected extension(used to sanity check) 729 */ 730 struct nft_set_ext_tmpl { 731 u16 len; 732 u8 offset[NFT_SET_EXT_NUM]; 733 u8 ext_len[NFT_SET_EXT_NUM]; 734 }; 735 736 /** 737 * struct nft_set_ext - set extensions 738 * 739 * @genmask: generation mask, but also flags (see NFT_SET_ELEM_DEAD_BIT) 740 * @offset: offsets of individual extension types 741 * @data: beginning of extension data 742 * 743 * This structure must be aligned to word size, otherwise atomic bitops 744 * on genmask field can cause alignment failure on some archs. 745 */ 746 struct nft_set_ext { 747 u8 genmask; 748 u8 offset[NFT_SET_EXT_NUM]; 749 char data[]; 750 } __aligned(BITS_PER_LONG / 8); 751 752 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl) 753 { 754 memset(tmpl, 0, sizeof(*tmpl)); 755 tmpl->len = sizeof(struct nft_set_ext); 756 } 757 758 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id, 759 unsigned int len) 760 { 761 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align); 762 if (tmpl->len > U8_MAX) 763 return -EINVAL; 764 765 tmpl->offset[id] = tmpl->len; 766 tmpl->ext_len[id] = nft_set_ext_types[id].len + len; 767 tmpl->len += tmpl->ext_len[id]; 768 769 return 0; 770 } 771 772 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id) 773 { 774 return nft_set_ext_add_length(tmpl, id, 0); 775 } 776 777 static inline void nft_set_ext_init(struct nft_set_ext *ext, 778 const struct nft_set_ext_tmpl *tmpl) 779 { 780 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset)); 781 } 782 783 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) 784 { 785 return !!ext->offset[id]; 786 } 787 788 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id) 789 { 790 return ext && __nft_set_ext_exists(ext, id); 791 } 792 793 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id) 794 { 795 return (void *)ext + ext->offset[id]; 796 } 797 798 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext) 799 { 800 return nft_set_ext(ext, NFT_SET_EXT_KEY); 801 } 802 803 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext) 804 { 805 return nft_set_ext(ext, NFT_SET_EXT_KEY_END); 806 } 807 808 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext) 809 { 810 return nft_set_ext(ext, NFT_SET_EXT_DATA); 811 } 812 813 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext) 814 { 815 return nft_set_ext(ext, NFT_SET_EXT_FLAGS); 816 } 817 818 struct nft_timeout { 819 u64 timeout; 820 u64 expiration; 821 }; 822 823 static inline struct nft_timeout *nft_set_ext_timeout(const struct nft_set_ext *ext) 824 { 825 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT); 826 } 827 828 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext) 829 { 830 return nft_set_ext(ext, NFT_SET_EXT_USERDATA); 831 } 832 833 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext) 834 { 835 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS); 836 } 837 838 static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext, 839 u64 tstamp) 840 { 841 if (!nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) || 842 READ_ONCE(nft_set_ext_timeout(ext)->timeout) == 0) 843 return false; 844 845 return time_after_eq64(tstamp, READ_ONCE(nft_set_ext_timeout(ext)->expiration)); 846 } 847 848 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext) 849 { 850 return __nft_set_elem_expired(ext, get_jiffies_64()); 851 } 852 853 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set, 854 const struct nft_elem_priv *elem_priv) 855 { 856 return (void *)elem_priv + set->ops->elemsize; 857 } 858 859 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext) 860 { 861 return nft_set_ext(ext, NFT_SET_EXT_OBJREF); 862 } 863 864 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx, 865 const struct nft_set *set, 866 const struct nlattr *attr); 867 868 struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set, 869 const struct nft_set_ext_tmpl *tmpl, 870 const u32 *key, const u32 *key_end, 871 const u32 *data, 872 u64 timeout, u64 expiration, gfp_t gfp); 873 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set, 874 struct nft_expr *expr_array[]); 875 void nft_set_elem_expr_destroy(const struct nft_ctx *ctx, 876 struct nft_set_elem_expr *elem_expr); 877 void nft_set_elem_destroy(const struct nft_set *set, 878 const struct nft_elem_priv *elem_priv, 879 bool destroy_expr); 880 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx, 881 const struct nft_set *set, 882 const struct nft_elem_priv *elem_priv); 883 884 struct nft_expr_ops; 885 /** 886 * struct nft_expr_type - nf_tables expression type 887 * 888 * @select_ops: function to select nft_expr_ops 889 * @release_ops: release nft_expr_ops 890 * @ops: default ops, used when no select_ops functions is present 891 * @inner_ops: inner ops, used for inner packet operation 892 * @list: used internally 893 * @name: Identifier 894 * @owner: module reference 895 * @policy: netlink attribute policy 896 * @maxattr: highest netlink attribute number 897 * @family: address family for AF-specific types 898 * @flags: expression type flags 899 */ 900 struct nft_expr_type { 901 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *, 902 const struct nlattr * const tb[]); 903 void (*release_ops)(const struct nft_expr_ops *ops); 904 const struct nft_expr_ops *ops; 905 const struct nft_expr_ops *inner_ops; 906 struct list_head list; 907 const char *name; 908 struct module *owner; 909 const struct nla_policy *policy; 910 unsigned int maxattr; 911 u8 family; 912 u8 flags; 913 }; 914 915 #define NFT_EXPR_STATEFUL 0x1 916 #define NFT_EXPR_GC 0x2 917 918 enum nft_trans_phase { 919 NFT_TRANS_PREPARE, 920 NFT_TRANS_PREPARE_ERROR, 921 NFT_TRANS_ABORT, 922 NFT_TRANS_COMMIT, 923 NFT_TRANS_RELEASE 924 }; 925 926 struct nft_flow_rule; 927 struct nft_offload_ctx; 928 929 /** 930 * struct nft_expr_ops - nf_tables expression operations 931 * 932 * @eval: Expression evaluation function 933 * @clone: Expression clone function 934 * @size: full expression size, including private data size 935 * @init: initialization function 936 * @activate: activate expression in the next generation 937 * @deactivate: deactivate expression in next generation 938 * @destroy: destruction function, called after synchronize_rcu 939 * @destroy_clone: destruction clone function 940 * @dump: function to dump parameters 941 * @validate: validate expression, called during loop detection 942 * @gc: garbage collection expression 943 * @offload: hardware offload expression 944 * @offload_action: function to report true/false to allocate one slot or not in the flow 945 * offload array 946 * @offload_stats: function to synchronize hardware stats via updating the counter expression 947 * @type: expression type 948 * @data: extra data to attach to this expression operation 949 */ 950 struct nft_expr_ops { 951 void (*eval)(const struct nft_expr *expr, 952 struct nft_regs *regs, 953 const struct nft_pktinfo *pkt); 954 int (*clone)(struct nft_expr *dst, 955 const struct nft_expr *src, gfp_t gfp); 956 unsigned int size; 957 958 int (*init)(const struct nft_ctx *ctx, 959 const struct nft_expr *expr, 960 const struct nlattr * const tb[]); 961 void (*activate)(const struct nft_ctx *ctx, 962 const struct nft_expr *expr); 963 void (*deactivate)(const struct nft_ctx *ctx, 964 const struct nft_expr *expr, 965 enum nft_trans_phase phase); 966 void (*destroy)(const struct nft_ctx *ctx, 967 const struct nft_expr *expr); 968 void (*destroy_clone)(const struct nft_ctx *ctx, 969 const struct nft_expr *expr); 970 int (*dump)(struct sk_buff *skb, 971 const struct nft_expr *expr, 972 bool reset); 973 int (*validate)(const struct nft_ctx *ctx, 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 #define NFT_HOOK_REMOVE (1 << 0) 1215 1216 struct nft_hook { 1217 struct list_head list; 1218 struct list_head ops_list; 1219 struct rcu_head rcu; 1220 char ifname[IFNAMSIZ]; 1221 u8 ifnamelen; 1222 u8 flags; 1223 }; 1224 1225 struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook, 1226 const struct net_device *dev); 1227 struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook, 1228 const struct net_device *dev); 1229 1230 /** 1231 * struct nft_base_chain - nf_tables base chain 1232 * 1233 * @ops: netfilter hook ops 1234 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family) 1235 * @type: chain type 1236 * @policy: default policy 1237 * @flags: indicate the base chain disabled or not 1238 * @stats: per-cpu chain stats 1239 * @chain: the chain 1240 * @flow_block: flow block (for hardware offload) 1241 */ 1242 struct nft_base_chain { 1243 struct nf_hook_ops ops; 1244 struct list_head hook_list; 1245 const struct nft_chain_type *type; 1246 u8 policy; 1247 u8 flags; 1248 struct nft_stats __percpu *stats; 1249 struct nft_chain chain; 1250 struct flow_block flow_block; 1251 }; 1252 1253 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain) 1254 { 1255 return container_of(chain, struct nft_base_chain, chain); 1256 } 1257 1258 static inline bool nft_is_base_chain(const struct nft_chain *chain) 1259 { 1260 return chain->flags & NFT_CHAIN_BASE; 1261 } 1262 1263 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv); 1264 1265 static inline bool nft_use_inc(u32 *use) 1266 { 1267 if (*use == UINT_MAX) 1268 return false; 1269 1270 (*use)++; 1271 1272 return true; 1273 } 1274 1275 static inline void nft_use_dec(u32 *use) 1276 { 1277 WARN_ON_ONCE((*use)-- == 0); 1278 } 1279 1280 /* For error and abort path: restore use counter to previous state. */ 1281 static inline void nft_use_inc_restore(u32 *use) 1282 { 1283 WARN_ON_ONCE(!nft_use_inc(use)); 1284 } 1285 1286 #define nft_use_dec_restore nft_use_dec 1287 1288 /** 1289 * struct nft_table - nf_tables table 1290 * 1291 * @list: used internally 1292 * @chains_ht: chains in the table 1293 * @chains: same, for stable walks 1294 * @sets: sets in the table 1295 * @objects: stateful objects in the table 1296 * @flowtables: flow tables in the table 1297 * @objname_ht: hashtable for objects lookup by name 1298 * @hgenerator: handle generator state 1299 * @handle: table handle 1300 * @use: number of chain references to this table 1301 * @family:address family 1302 * @flags: table flag (see enum nft_table_flags) 1303 * @genmask: generation mask 1304 * @nlpid: netlink port ID 1305 * @name: name of the table 1306 * @udlen: length of the user data 1307 * @udata: user data 1308 * @validate_state: internal, set when transaction adds jumps 1309 */ 1310 struct nft_table { 1311 struct list_head list; 1312 struct rhltable chains_ht; 1313 struct list_head chains; 1314 struct list_head sets; 1315 struct list_head objects; 1316 struct list_head flowtables; 1317 struct rhltable objname_ht; 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 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_hook - nf_tables hook update in transaction 1681 * @list: used internally 1682 * @hook: struct nft_hook with the device hook 1683 */ 1684 struct nft_trans_hook { 1685 struct list_head list; 1686 struct nft_hook *hook; 1687 }; 1688 1689 /** 1690 * struct nft_trans_binding - nf_tables object with binding support in transaction 1691 * @nft_trans: base structure, MUST be first member 1692 * @binding_list: list of objects with possible bindings 1693 * 1694 * This is the base type used by objects that can be bound to a chain. 1695 */ 1696 struct nft_trans_binding { 1697 struct nft_trans nft_trans; 1698 struct list_head binding_list; 1699 }; 1700 1701 struct nft_trans_rule { 1702 struct nft_trans nft_trans; 1703 struct nft_rule *rule; 1704 struct nft_chain *chain; 1705 struct nft_flow_rule *flow; 1706 u32 rule_id; 1707 bool bound; 1708 }; 1709 1710 #define nft_trans_container_rule(trans) \ 1711 container_of(trans, struct nft_trans_rule, nft_trans) 1712 #define nft_trans_rule(trans) \ 1713 nft_trans_container_rule(trans)->rule 1714 #define nft_trans_flow_rule(trans) \ 1715 nft_trans_container_rule(trans)->flow 1716 #define nft_trans_rule_id(trans) \ 1717 nft_trans_container_rule(trans)->rule_id 1718 #define nft_trans_rule_bound(trans) \ 1719 nft_trans_container_rule(trans)->bound 1720 #define nft_trans_rule_chain(trans) \ 1721 nft_trans_container_rule(trans)->chain 1722 1723 struct nft_trans_set { 1724 struct nft_trans_binding nft_trans_binding; 1725 struct list_head list_trans_newset; 1726 struct nft_set *set; 1727 u32 set_id; 1728 u32 gc_int; 1729 u64 timeout; 1730 bool update; 1731 bool bound; 1732 u32 size; 1733 }; 1734 1735 #define nft_trans_container_set(t) \ 1736 container_of(t, struct nft_trans_set, nft_trans_binding.nft_trans) 1737 #define nft_trans_set(trans) \ 1738 nft_trans_container_set(trans)->set 1739 #define nft_trans_set_id(trans) \ 1740 nft_trans_container_set(trans)->set_id 1741 #define nft_trans_set_bound(trans) \ 1742 nft_trans_container_set(trans)->bound 1743 #define nft_trans_set_update(trans) \ 1744 nft_trans_container_set(trans)->update 1745 #define nft_trans_set_timeout(trans) \ 1746 nft_trans_container_set(trans)->timeout 1747 #define nft_trans_set_gc_int(trans) \ 1748 nft_trans_container_set(trans)->gc_int 1749 #define nft_trans_set_size(trans) \ 1750 nft_trans_container_set(trans)->size 1751 1752 struct nft_trans_chain { 1753 struct nft_trans_binding nft_trans_binding; 1754 struct nft_chain *chain; 1755 char *name; 1756 struct nft_stats __percpu *stats; 1757 u8 policy; 1758 bool update; 1759 bool bound; 1760 u32 chain_id; 1761 struct nft_base_chain *basechain; 1762 struct list_head hook_list; 1763 }; 1764 1765 #define nft_trans_container_chain(t) \ 1766 container_of(t, struct nft_trans_chain, nft_trans_binding.nft_trans) 1767 #define nft_trans_chain(trans) \ 1768 nft_trans_container_chain(trans)->chain 1769 #define nft_trans_chain_update(trans) \ 1770 nft_trans_container_chain(trans)->update 1771 #define nft_trans_chain_name(trans) \ 1772 nft_trans_container_chain(trans)->name 1773 #define nft_trans_chain_stats(trans) \ 1774 nft_trans_container_chain(trans)->stats 1775 #define nft_trans_chain_policy(trans) \ 1776 nft_trans_container_chain(trans)->policy 1777 #define nft_trans_chain_bound(trans) \ 1778 nft_trans_container_chain(trans)->bound 1779 #define nft_trans_chain_id(trans) \ 1780 nft_trans_container_chain(trans)->chain_id 1781 #define nft_trans_basechain(trans) \ 1782 nft_trans_container_chain(trans)->basechain 1783 #define nft_trans_chain_hooks(trans) \ 1784 nft_trans_container_chain(trans)->hook_list 1785 1786 struct nft_trans_table { 1787 struct nft_trans nft_trans; 1788 bool update; 1789 }; 1790 1791 #define nft_trans_container_table(trans) \ 1792 container_of(trans, struct nft_trans_table, nft_trans) 1793 #define nft_trans_table_update(trans) \ 1794 nft_trans_container_table(trans)->update 1795 1796 enum nft_trans_elem_flags { 1797 NFT_TRANS_UPD_TIMEOUT = (1 << 0), 1798 NFT_TRANS_UPD_EXPIRATION = (1 << 1), 1799 }; 1800 1801 struct nft_elem_update { 1802 u64 timeout; 1803 u64 expiration; 1804 u8 flags; 1805 }; 1806 1807 struct nft_trans_one_elem { 1808 struct nft_elem_priv *priv; 1809 struct nft_elem_update *update; 1810 }; 1811 1812 struct nft_trans_elem { 1813 struct nft_trans nft_trans; 1814 struct nft_set *set; 1815 bool bound; 1816 unsigned int nelems; 1817 struct nft_trans_one_elem elems[] __counted_by(nelems); 1818 }; 1819 1820 #define nft_trans_container_elem(t) \ 1821 container_of(t, struct nft_trans_elem, nft_trans) 1822 #define nft_trans_elem_set(trans) \ 1823 nft_trans_container_elem(trans)->set 1824 #define nft_trans_elem_set_bound(trans) \ 1825 nft_trans_container_elem(trans)->bound 1826 1827 struct nft_trans_obj { 1828 struct nft_trans nft_trans; 1829 struct nft_object *obj; 1830 struct nft_object *newobj; 1831 bool update; 1832 }; 1833 1834 #define nft_trans_container_obj(t) \ 1835 container_of(t, struct nft_trans_obj, nft_trans) 1836 #define nft_trans_obj(trans) \ 1837 nft_trans_container_obj(trans)->obj 1838 #define nft_trans_obj_newobj(trans) \ 1839 nft_trans_container_obj(trans)->newobj 1840 #define nft_trans_obj_update(trans) \ 1841 nft_trans_container_obj(trans)->update 1842 1843 struct nft_trans_flowtable { 1844 struct nft_trans nft_trans; 1845 struct nft_flowtable *flowtable; 1846 struct list_head hook_list; 1847 u32 flags; 1848 bool update; 1849 }; 1850 1851 #define nft_trans_container_flowtable(t) \ 1852 container_of(t, struct nft_trans_flowtable, nft_trans) 1853 #define nft_trans_flowtable(trans) \ 1854 nft_trans_container_flowtable(trans)->flowtable 1855 #define nft_trans_flowtable_update(trans) \ 1856 nft_trans_container_flowtable(trans)->update 1857 #define nft_trans_flowtable_hooks(trans) \ 1858 nft_trans_container_flowtable(trans)->hook_list 1859 #define nft_trans_flowtable_flags(trans) \ 1860 nft_trans_container_flowtable(trans)->flags 1861 1862 #define NFT_TRANS_GC_BATCHCOUNT 256 1863 1864 struct nft_trans_gc { 1865 struct list_head list; 1866 struct net *net; 1867 struct nft_set *set; 1868 u32 seq; 1869 u16 count; 1870 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT]; 1871 struct rcu_head rcu; 1872 }; 1873 1874 static inline int nft_trans_gc_space(const struct nft_trans_gc *trans) 1875 { 1876 return NFT_TRANS_GC_BATCHCOUNT - trans->count; 1877 } 1878 1879 static inline void nft_ctx_update(struct nft_ctx *ctx, 1880 const struct nft_trans *trans) 1881 { 1882 switch (trans->msg_type) { 1883 case NFT_MSG_NEWRULE: 1884 case NFT_MSG_DELRULE: 1885 case NFT_MSG_DESTROYRULE: 1886 ctx->chain = nft_trans_rule_chain(trans); 1887 break; 1888 case NFT_MSG_NEWCHAIN: 1889 case NFT_MSG_DELCHAIN: 1890 case NFT_MSG_DESTROYCHAIN: 1891 ctx->chain = nft_trans_chain(trans); 1892 break; 1893 default: 1894 ctx->chain = NULL; 1895 break; 1896 } 1897 1898 ctx->net = trans->net; 1899 ctx->table = trans->table; 1900 ctx->family = trans->table->family; 1901 ctx->report = trans->report; 1902 ctx->flags = trans->flags; 1903 ctx->seq = trans->seq; 1904 } 1905 1906 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set, 1907 unsigned int gc_seq, gfp_t gfp); 1908 void nft_trans_gc_destroy(struct nft_trans_gc *trans); 1909 1910 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc, 1911 unsigned int gc_seq, gfp_t gfp); 1912 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc); 1913 1914 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp); 1915 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans); 1916 1917 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv); 1918 1919 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc, 1920 unsigned int gc_seq); 1921 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc); 1922 1923 void nft_setelem_data_deactivate(const struct net *net, 1924 const struct nft_set *set, 1925 struct nft_elem_priv *elem_priv); 1926 1927 int __init nft_chain_filter_init(void); 1928 void nft_chain_filter_fini(void); 1929 1930 void __init nft_chain_route_init(void); 1931 void nft_chain_route_fini(void); 1932 1933 void nf_tables_trans_destroy_flush_work(struct net *net); 1934 1935 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result); 1936 __be64 nf_jiffies64_to_msecs(u64 input); 1937 1938 #ifdef CONFIG_MODULES 1939 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...); 1940 #else 1941 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; } 1942 #endif 1943 1944 struct nftables_pernet { 1945 struct list_head tables; 1946 struct list_head commit_list; 1947 struct list_head destroy_list; 1948 struct list_head commit_set_list; 1949 struct list_head binding_list; 1950 struct list_head module_list; 1951 struct list_head notify_list; 1952 struct mutex commit_mutex; 1953 u64 table_handle; 1954 u64 tstamp; 1955 unsigned int gc_seq; 1956 u8 validate_state; 1957 struct work_struct destroy_work; 1958 }; 1959 1960 extern unsigned int nf_tables_net_id; 1961 1962 static inline struct nftables_pernet *nft_pernet(const struct net *net) 1963 { 1964 return net_generic(net, nf_tables_net_id); 1965 } 1966 1967 static inline u64 nft_net_tstamp(const struct net *net) 1968 { 1969 return nft_pernet(net)->tstamp; 1970 } 1971 1972 #endif /* _NET_NF_TABLES_H */ 1973