1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <asm/unaligned.h>
6 #include <linux/list.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nfnetlink.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <linux/u64_stats_sync.h>
12 #include <linux/rhashtable.h>
13 #include <net/netfilter/nf_flow_table.h>
14 #include <net/netlink.h>
15 #include <net/flow_offload.h>
16 #include <net/netns/generic.h>
18 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
22 #define NFT_JUMP_STACK_SIZE 16
25 NFT_PKTINFO_L4PROTO = (1 << 0),
26 NFT_PKTINFO_INNER = (1 << 1),
27 NFT_PKTINFO_INNER_FULL = (1 << 2),
32 const struct nf_hook_state *state;
40 static inline struct sock *nft_sk(const struct nft_pktinfo *pkt)
42 return pkt->state->sk;
45 static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt)
50 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
52 return pkt->state->net;
55 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
57 return pkt->state->hook;
60 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
62 return pkt->state->pf;
65 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
67 return pkt->state->in;
70 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
72 return pkt->state->out;
75 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
77 const struct nf_hook_state *state)
83 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt)
92 * struct nft_verdict - nf_tables verdict
94 * @code: nf_tables/netfilter verdict code
95 * @chain: destination chain for NFT_JUMP/NFT_GOTO
99 struct nft_chain *chain;
105 struct nft_verdict verdict;
107 } __attribute__((aligned(__alignof__(u64))));
109 #define NFT_REG32_NUM 20
112 * struct nft_regs - nf_tables register set
114 * @data: data registers
115 * @verdict: verdict register
117 * The first four data registers alias to the verdict register.
121 u32 data[NFT_REG32_NUM];
122 struct nft_verdict verdict;
126 struct nft_regs_track {
128 const struct nft_expr *selector;
129 const struct nft_expr *bitwise;
131 } regs[NFT_REG32_NUM];
133 const struct nft_expr *cur;
134 const struct nft_expr *last;
137 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
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
144 static inline void nft_reg_store8(u32 *dreg, u8 val)
150 static inline u8 nft_reg_load8(const u32 *sreg)
155 static inline void nft_reg_store16(u32 *dreg, u16 val)
161 static inline void nft_reg_store_be16(u32 *dreg, __be16 val)
163 nft_reg_store16(dreg, (__force __u16)val);
166 static inline u16 nft_reg_load16(const u32 *sreg)
171 static inline __be16 nft_reg_load_be16(const u32 *sreg)
173 return (__force __be16)nft_reg_load16(sreg);
176 static inline __be32 nft_reg_load_be32(const u32 *sreg)
178 return *(__force __be32 *)sreg;
181 static inline void nft_reg_store64(u64 *dreg, u64 val)
183 put_unaligned(val, dreg);
186 static inline u64 nft_reg_load64(const u32 *sreg)
188 return get_unaligned((u64 *)sreg);
191 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
194 if (len % NFT_REG32_SIZE)
195 dst[len / NFT_REG32_SIZE] = 0;
196 memcpy(dst, src, len);
200 * struct nft_ctx - nf_tables rule/set context
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
215 struct nft_table *table;
216 struct nft_chain *chain;
217 const struct nlattr * const *nla;
226 enum nft_data_desc_flags {
227 NFT_DATA_DESC_SETELEM = (1 << 0),
230 struct nft_data_desc {
231 enum nft_data_types type;
237 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
238 struct nft_data_desc *desc, const struct nlattr *nla);
239 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
240 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
241 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
242 enum nft_data_types type, unsigned int len);
244 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
246 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
249 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
251 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
254 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
255 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
257 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
258 int nft_parse_register_store(const struct nft_ctx *ctx,
259 const struct nlattr *attr, u8 *dreg,
260 const struct nft_data *data,
261 enum nft_data_types type, unsigned int len);
264 * struct nft_userdata - user defined data associated with an object
266 * @len: length of the data
269 * The presence of user data is indicated in an object specific fashion,
270 * so a length of zero can't occur and the value "len" indicates data
273 struct nft_userdata {
275 unsigned char data[];
278 /* placeholder structure for opaque set element backend representation. */
279 struct nft_elem_priv { };
282 * struct nft_set_elem - generic representation of set elements
285 * @key_end: closing element key
286 * @data: element data
287 * @priv: element private data and extensions
289 struct nft_set_elem {
291 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
295 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
299 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
302 struct nft_elem_priv *priv;
305 static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)
311 struct nft_set_iter {
316 int (*fn)(const struct nft_ctx *ctx,
318 const struct nft_set_iter *iter,
319 struct nft_elem_priv *elem_priv);
323 * struct nft_set_desc - description of set elements
329 * @objtype: object type
330 * @size: number of set elements
331 * @policy: set policy
332 * @gc_int: garbage collector interval
333 * @timeout: element timeout
334 * @field_len: length of each field in concatenation, bytes
335 * @field_count: number of concatenated fields in element
336 * @expr: set must support for expressions
338 struct nft_set_desc {
348 u8 field_len[NFT_REG32_COUNT];
354 * enum nft_set_class - performance class
356 * @NFT_SET_CLASS_O_1: constant, O(1)
357 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N)
358 * @NFT_SET_CLASS_O_N: linear, O(N)
362 NFT_SET_CLASS_O_LOG_N,
367 * struct nft_set_estimate - estimation of memory and performance
370 * @size: required memory
371 * @lookup: lookup performance class
372 * @space: memory class
374 struct nft_set_estimate {
376 enum nft_set_class lookup;
377 enum nft_set_class space;
380 #define NFT_EXPR_MAXATTR 16
381 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
382 ALIGN(size, __alignof__(struct nft_expr)))
385 * struct nft_expr - nf_tables expression
387 * @ops: expression ops
388 * @data: expression private data
391 const struct nft_expr_ops *ops;
393 __attribute__((aligned(__alignof__(u64))));
396 static inline void *nft_expr_priv(const struct nft_expr *expr)
398 return (void *)expr->data;
401 struct nft_expr_info;
403 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
404 struct nft_expr_info *info);
405 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
406 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
407 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
408 const struct nft_expr *expr, bool reset);
409 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
410 const struct nft_expr *expr);
415 * struct nft_set_ops - nf_tables set operations
417 * @lookup: look up an element within the set
418 * @update: update an element if exists, add it if doesn't exist
419 * @delete: delete an element
420 * @insert: insert new element into set
421 * @activate: activate new element in the next generation
422 * @deactivate: lookup for element and deactivate it in the next generation
423 * @flush: deactivate element in the next generation
424 * @remove: remove element from set
425 * @walk: iterate over all set elements
426 * @get: get set elements
427 * @commit: commit set elements
428 * @abort: abort set elements
429 * @privsize: function to return size of set private data
430 * @estimate: estimate the required memory size and the lookup complexity class
431 * @init: initialize private data of new set instance
432 * @destroy: destroy private data of set instance
433 * @gc_init: initialize garbage collection
434 * @elemsize: element private size
436 * Operations lookup, update and delete have simpler interfaces, are faster
437 * and currently only used in the packet path. All the rest are slower,
438 * control plane functions.
441 bool (*lookup)(const struct net *net,
442 const struct nft_set *set,
444 const struct nft_set_ext **ext);
445 bool (*update)(struct nft_set *set,
447 struct nft_elem_priv *
448 (*new)(struct nft_set *,
449 const struct nft_expr *,
451 const struct nft_expr *expr,
452 struct nft_regs *regs,
453 const struct nft_set_ext **ext);
454 bool (*delete)(const struct nft_set *set,
457 int (*insert)(const struct net *net,
458 const struct nft_set *set,
459 const struct nft_set_elem *elem,
460 struct nft_elem_priv **priv);
461 void (*activate)(const struct net *net,
462 const struct nft_set *set,
463 struct nft_elem_priv *elem_priv);
464 struct nft_elem_priv * (*deactivate)(const struct net *net,
465 const struct nft_set *set,
466 const struct nft_set_elem *elem);
467 void (*flush)(const struct net *net,
468 const struct nft_set *set,
469 struct nft_elem_priv *priv);
470 void (*remove)(const struct net *net,
471 const struct nft_set *set,
472 struct nft_elem_priv *elem_priv);
473 void (*walk)(const struct nft_ctx *ctx,
475 struct nft_set_iter *iter);
476 struct nft_elem_priv * (*get)(const struct net *net,
477 const struct nft_set *set,
478 const struct nft_set_elem *elem,
480 void (*commit)(struct nft_set *set);
481 void (*abort)(const struct nft_set *set);
482 u64 (*privsize)(const struct nlattr * const nla[],
483 const struct nft_set_desc *desc);
484 bool (*estimate)(const struct nft_set_desc *desc,
486 struct nft_set_estimate *est);
487 int (*init)(const struct nft_set *set,
488 const struct nft_set_desc *desc,
489 const struct nlattr * const nla[]);
490 void (*destroy)(const struct nft_ctx *ctx,
491 const struct nft_set *set);
492 void (*gc_init)(const struct nft_set *set);
494 unsigned int elemsize;
498 * struct nft_set_type - nf_tables set type
500 * @ops: set ops for this type
501 * @features: features supported by the implementation
503 struct nft_set_type {
504 const struct nft_set_ops ops;
507 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
509 struct nft_set_elem_expr {
512 __attribute__((aligned(__alignof__(struct nft_expr))));
515 #define nft_setelem_expr_at(__elem_expr, __offset) \
516 ((struct nft_expr *)&__elem_expr->data[__offset])
518 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
519 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
520 __size < (__elem_expr)->size; \
521 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
523 #define NFT_SET_EXPR_MAX 2
526 * struct nft_set - nf_tables set instance
528 * @list: table set list node
529 * @bindings: list of set bindings
530 * @refs: internal refcounting for async set destruction
531 * @table: table this set belongs to
532 * @net: netnamespace this set belongs to
533 * @name: name of the set
534 * @handle: unique handle of the set
535 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
536 * @dtype: data type (verdict or numeric type defined by userspace)
537 * @objtype: object type (see NFT_OBJECT_* definitions)
538 * @size: maximum set size
539 * @field_len: length of each field in concatenation, bytes
540 * @field_count: number of concatenated fields in element
541 * @use: number of rules references to this set
542 * @nelems: number of elements
543 * @ndeact: number of deactivated elements queued for removal
544 * @timeout: default timeout value in jiffies
545 * @gc_int: garbage collection interval in msecs
546 * @policy: set parameterization (see enum nft_set_policies)
547 * @udlen: user data length
549 * @pending_update: list of pending update set element
552 * @dead: set will be freed, never cleared
553 * @genmask: generation mask
556 * @num_exprs: numbers of exprs
557 * @exprs: stateful expression
558 * @catchall_list: list of catch-all set element
559 * @data: private set data
562 struct list_head list;
563 struct list_head bindings;
565 struct nft_table *table;
573 u8 field_len[NFT_REG32_COUNT];
582 unsigned char *udata;
583 struct list_head pending_update;
584 /* runtime data below here */
585 const struct nft_set_ops *ops ____cacheline_aligned;
592 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
593 struct list_head catchall_list;
595 __attribute__((aligned(__alignof__(u64))));
598 static inline bool nft_set_is_anonymous(const struct nft_set *set)
600 return set->flags & NFT_SET_ANONYMOUS;
603 static inline void *nft_set_priv(const struct nft_set *set)
605 return (void *)set->data;
608 static inline bool nft_set_gc_is_pending(const struct nft_set *s)
610 return refcount_read(&s->refs) != 1;
613 static inline struct nft_set *nft_set_container_of(const void *priv)
615 return (void *)priv - offsetof(struct nft_set, data);
618 struct nft_set *nft_set_lookup_global(const struct net *net,
619 const struct nft_table *table,
620 const struct nlattr *nla_set_name,
621 const struct nlattr *nla_set_id,
624 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
625 const struct nft_set *set);
627 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
629 u32 gc_int = READ_ONCE(set->gc_int);
631 return gc_int ? msecs_to_jiffies(gc_int) : HZ;
635 * struct nft_set_binding - nf_tables set binding
637 * @list: set bindings list node
638 * @chain: chain containing the rule bound to the set
639 * @flags: set action flags
641 * A set binding contains all information necessary for validation
642 * of new elements added to a bound set.
644 struct nft_set_binding {
645 struct list_head list;
646 const struct nft_chain *chain;
650 enum nft_trans_phase;
651 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
652 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
653 struct nft_set_binding *binding,
654 enum nft_trans_phase phase);
655 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
656 struct nft_set_binding *binding);
657 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
660 * enum nft_set_extensions - set extension type IDs
662 * @NFT_SET_EXT_KEY: element key
663 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
664 * @NFT_SET_EXT_DATA: mapping data
665 * @NFT_SET_EXT_FLAGS: element flags
666 * @NFT_SET_EXT_TIMEOUT: element timeout
667 * @NFT_SET_EXT_EXPIRATION: element expiration time
668 * @NFT_SET_EXT_USERDATA: user data associated with the element
669 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
670 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
671 * @NFT_SET_EXT_NUM: number of extension types
673 enum nft_set_extensions {
679 NFT_SET_EXT_EXPIRATION,
680 NFT_SET_EXT_USERDATA,
681 NFT_SET_EXT_EXPRESSIONS,
687 * struct nft_set_ext_type - set extension type
689 * @len: fixed part length of the extension
690 * @align: alignment requirements of the extension
692 struct nft_set_ext_type {
697 extern const struct nft_set_ext_type nft_set_ext_types[];
700 * struct nft_set_ext_tmpl - set extension template
702 * @len: length of extension area
703 * @offset: offsets of individual extension types
704 * @ext_len: length of the expected extension(used to sanity check)
706 struct nft_set_ext_tmpl {
708 u8 offset[NFT_SET_EXT_NUM];
709 u8 ext_len[NFT_SET_EXT_NUM];
713 * struct nft_set_ext - set extensions
715 * @genmask: generation mask
716 * @offset: offsets of individual extension types
717 * @data: beginning of extension data
721 u8 offset[NFT_SET_EXT_NUM];
725 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
727 memset(tmpl, 0, sizeof(*tmpl));
728 tmpl->len = sizeof(struct nft_set_ext);
731 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
734 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
735 if (tmpl->len > U8_MAX)
738 tmpl->offset[id] = tmpl->len;
739 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
740 tmpl->len += tmpl->ext_len[id];
745 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
747 return nft_set_ext_add_length(tmpl, id, 0);
750 static inline void nft_set_ext_init(struct nft_set_ext *ext,
751 const struct nft_set_ext_tmpl *tmpl)
753 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
756 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
758 return !!ext->offset[id];
761 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
763 return ext && __nft_set_ext_exists(ext, id);
766 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
768 return (void *)ext + ext->offset[id];
771 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
773 return nft_set_ext(ext, NFT_SET_EXT_KEY);
776 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
778 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
781 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
783 return nft_set_ext(ext, NFT_SET_EXT_DATA);
786 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
788 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
791 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
793 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
796 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
798 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
801 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
803 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
806 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
808 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
811 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
813 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
814 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
817 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
818 const struct nft_elem_priv *elem_priv)
820 return (void *)elem_priv + set->ops->elemsize;
823 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
825 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
828 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
829 const struct nft_set *set,
830 const struct nlattr *attr);
832 struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
833 const struct nft_set_ext_tmpl *tmpl,
834 const u32 *key, const u32 *key_end,
836 u64 timeout, u64 expiration, gfp_t gfp);
837 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
838 struct nft_expr *expr_array[]);
839 void nft_set_elem_destroy(const struct nft_set *set,
840 const struct nft_elem_priv *elem_priv,
842 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
843 const struct nft_set *set,
844 const struct nft_elem_priv *elem_priv);
848 * struct nft_expr_type - nf_tables expression type
850 * @select_ops: function to select nft_expr_ops
851 * @release_ops: release nft_expr_ops
852 * @ops: default ops, used when no select_ops functions is present
853 * @inner_ops: inner ops, used for inner packet operation
854 * @list: used internally
856 * @owner: module reference
857 * @policy: netlink attribute policy
858 * @maxattr: highest netlink attribute number
859 * @family: address family for AF-specific types
860 * @flags: expression type flags
862 struct nft_expr_type {
863 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
864 const struct nlattr * const tb[]);
865 void (*release_ops)(const struct nft_expr_ops *ops);
866 const struct nft_expr_ops *ops;
867 const struct nft_expr_ops *inner_ops;
868 struct list_head list;
870 struct module *owner;
871 const struct nla_policy *policy;
872 unsigned int maxattr;
877 #define NFT_EXPR_STATEFUL 0x1
878 #define NFT_EXPR_GC 0x2
880 enum nft_trans_phase {
882 NFT_TRANS_PREPARE_ERROR,
888 struct nft_flow_rule;
889 struct nft_offload_ctx;
892 * struct nft_expr_ops - nf_tables expression operations
894 * @eval: Expression evaluation function
895 * @clone: Expression clone function
896 * @size: full expression size, including private data size
897 * @init: initialization function
898 * @activate: activate expression in the next generation
899 * @deactivate: deactivate expression in next generation
900 * @destroy: destruction function, called after synchronize_rcu
901 * @destroy_clone: destruction clone function
902 * @dump: function to dump parameters
903 * @validate: validate expression, called during loop detection
904 * @reduce: reduce expression
905 * @gc: garbage collection expression
906 * @offload: hardware offload expression
907 * @offload_action: function to report true/false to allocate one slot or not in the flow
909 * @offload_stats: function to synchronize hardware stats via updating the counter expression
910 * @type: expression type
911 * @data: extra data to attach to this expression operation
913 struct nft_expr_ops {
914 void (*eval)(const struct nft_expr *expr,
915 struct nft_regs *regs,
916 const struct nft_pktinfo *pkt);
917 int (*clone)(struct nft_expr *dst,
918 const struct nft_expr *src);
921 int (*init)(const struct nft_ctx *ctx,
922 const struct nft_expr *expr,
923 const struct nlattr * const tb[]);
924 void (*activate)(const struct nft_ctx *ctx,
925 const struct nft_expr *expr);
926 void (*deactivate)(const struct nft_ctx *ctx,
927 const struct nft_expr *expr,
928 enum nft_trans_phase phase);
929 void (*destroy)(const struct nft_ctx *ctx,
930 const struct nft_expr *expr);
931 void (*destroy_clone)(const struct nft_ctx *ctx,
932 const struct nft_expr *expr);
933 int (*dump)(struct sk_buff *skb,
934 const struct nft_expr *expr,
936 int (*validate)(const struct nft_ctx *ctx,
937 const struct nft_expr *expr,
938 const struct nft_data **data);
939 bool (*reduce)(struct nft_regs_track *track,
940 const struct nft_expr *expr);
941 bool (*gc)(struct net *net,
942 const struct nft_expr *expr);
943 int (*offload)(struct nft_offload_ctx *ctx,
944 struct nft_flow_rule *flow,
945 const struct nft_expr *expr);
946 bool (*offload_action)(const struct nft_expr *expr);
947 void (*offload_stats)(struct nft_expr *expr,
948 const struct flow_stats *stats);
949 const struct nft_expr_type *type;
954 * struct nft_rule - nf_tables rule
956 * @list: used internally
957 * @handle: rule handle
958 * @genmask: generation mask
959 * @dlen: length of expression data
960 * @udata: user data is appended to the rule
961 * @data: expression data
964 struct list_head list;
970 __attribute__((aligned(__alignof__(struct nft_expr))));
973 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
975 return (struct nft_expr *)&rule->data[0];
978 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
980 return ((void *)expr) + expr->ops->size;
983 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
985 return (struct nft_expr *)&rule->data[rule->dlen];
988 static inline bool nft_expr_more(const struct nft_rule *rule,
989 const struct nft_expr *expr)
991 return expr != nft_expr_last(rule) && expr->ops;
994 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
996 return (void *)&rule->data[rule->dlen];
999 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule);
1000 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
1001 enum nft_trans_phase phase);
1002 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule);
1004 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
1005 struct nft_regs *regs,
1006 const struct nft_pktinfo *pkt)
1008 struct nft_set_elem_expr *elem_expr;
1009 struct nft_expr *expr;
1012 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
1013 elem_expr = nft_set_ext_expr(ext);
1014 nft_setelem_expr_foreach(expr, elem_expr, size) {
1015 expr->ops->eval(expr, regs, pkt);
1016 if (regs->verdict.code == NFT_BREAK)
1023 * The last pointer isn't really necessary, but the compiler isn't able to
1024 * determine that the result of nft_expr_last() is always the same since it
1025 * can't assume that the dlen value wasn't changed within calls in the loop.
1027 #define nft_rule_for_each_expr(expr, last, rule) \
1028 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1030 (expr) = nft_expr_next(expr))
1032 #define NFT_CHAIN_POLICY_UNSET U8_MAX
1034 struct nft_rule_dp {
1037 handle:42; /* for tracing */
1038 unsigned char data[]
1039 __attribute__((aligned(__alignof__(struct nft_expr))));
1042 struct nft_rule_dp_last {
1043 struct nft_rule_dp end; /* end of nft_rule_blob marker */
1044 struct rcu_head h; /* call_rcu head */
1045 struct nft_rule_blob *blob; /* ptr to free via call_rcu */
1046 const struct nft_chain *chain; /* for nftables tracing */
1049 static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule)
1051 return (void *)rule + sizeof(*rule) + rule->dlen;
1054 struct nft_rule_blob {
1056 unsigned char data[]
1057 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1061 * struct nft_chain - nf_tables chain
1063 * @blob_gen_0: rule blob pointer to the current generation
1064 * @blob_gen_1: rule blob pointer to the future generation
1065 * @rules: list of rules in the chain
1066 * @list: used internally
1067 * @rhlhead: used internally
1068 * @table: table that this chain belongs to
1069 * @handle: chain handle
1070 * @use: number of jump references to this chain
1071 * @flags: bitmask of enum NFTA_CHAIN_FLAGS
1072 * @bound: bind or not
1073 * @genmask: generation mask
1074 * @name: name of the chain
1075 * @udlen: user data length
1076 * @udata: user data in the chain
1077 * @blob_next: rule blob pointer to the next in the chain
1080 struct nft_rule_blob __rcu *blob_gen_0;
1081 struct nft_rule_blob __rcu *blob_gen_1;
1082 struct list_head rules;
1083 struct list_head list;
1084 struct rhlist_head rhlhead;
1085 struct nft_table *table;
1095 /* Only used during control plane commit phase: */
1096 struct nft_rule_blob *blob_next;
1099 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1100 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1101 const struct nft_set_iter *iter,
1102 struct nft_elem_priv *elem_priv);
1103 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
1104 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1105 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1107 enum nft_chain_types {
1108 NFT_CHAIN_T_DEFAULT = 0,
1115 * struct nft_chain_type - nf_tables chain type info
1117 * @name: name of the type
1118 * @type: numeric identifier
1119 * @family: address family
1120 * @owner: module owner
1121 * @hook_mask: mask of valid hooks
1122 * @hooks: array of hook functions
1123 * @ops_register: base chain register function
1124 * @ops_unregister: base chain unregister function
1126 struct nft_chain_type {
1128 enum nft_chain_types type;
1130 struct module *owner;
1131 unsigned int hook_mask;
1132 nf_hookfn *hooks[NFT_MAX_HOOKS];
1133 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1134 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1137 int nft_chain_validate_dependency(const struct nft_chain *chain,
1138 enum nft_chain_types type);
1139 int nft_chain_validate_hooks(const struct nft_chain *chain,
1140 unsigned int hook_flags);
1142 static inline bool nft_chain_binding(const struct nft_chain *chain)
1144 return chain->flags & NFT_CHAIN_BINDING;
1147 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1149 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1152 int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
1153 void nft_chain_del(struct nft_chain *chain);
1154 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1159 struct u64_stats_sync syncp;
1163 struct list_head list;
1164 struct nf_hook_ops ops;
1165 struct rcu_head rcu;
1169 * struct nft_base_chain - nf_tables base chain
1171 * @ops: netfilter hook ops
1172 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1174 * @policy: default policy
1175 * @flags: indicate the base chain disabled or not
1176 * @stats: per-cpu chain stats
1178 * @flow_block: flow block (for hardware offload)
1180 struct nft_base_chain {
1181 struct nf_hook_ops ops;
1182 struct list_head hook_list;
1183 const struct nft_chain_type *type;
1186 struct nft_stats __percpu *stats;
1187 struct nft_chain chain;
1188 struct flow_block flow_block;
1191 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1193 return container_of(chain, struct nft_base_chain, chain);
1196 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1198 return chain->flags & NFT_CHAIN_BASE;
1201 int __nft_release_basechain(struct nft_ctx *ctx);
1203 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1205 static inline bool nft_use_inc(u32 *use)
1207 if (*use == UINT_MAX)
1215 static inline void nft_use_dec(u32 *use)
1217 WARN_ON_ONCE((*use)-- == 0);
1220 /* For error and abort path: restore use counter to previous state. */
1221 static inline void nft_use_inc_restore(u32 *use)
1223 WARN_ON_ONCE(!nft_use_inc(use));
1226 #define nft_use_dec_restore nft_use_dec
1229 * struct nft_table - nf_tables table
1231 * @list: used internally
1232 * @chains_ht: chains in the table
1233 * @chains: same, for stable walks
1234 * @sets: sets in the table
1235 * @objects: stateful objects in the table
1236 * @flowtables: flow tables in the table
1237 * @hgenerator: handle generator state
1238 * @handle: table handle
1239 * @use: number of chain references to this table
1240 * @family:address family
1241 * @flags: table flag (see enum nft_table_flags)
1242 * @genmask: generation mask
1243 * @nlpid: netlink port ID
1244 * @name: name of the table
1245 * @udlen: length of the user data
1247 * @validate_state: internal, set when transaction adds jumps
1250 struct list_head list;
1251 struct rhltable chains_ht;
1252 struct list_head chains;
1253 struct list_head sets;
1254 struct list_head objects;
1255 struct list_head flowtables;
1269 static inline bool nft_table_has_owner(const struct nft_table *table)
1271 return table->flags & NFT_TABLE_F_OWNER;
1274 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1276 return family == NFPROTO_NETDEV ||
1277 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1280 void nft_register_chain_type(const struct nft_chain_type *);
1281 void nft_unregister_chain_type(const struct nft_chain_type *);
1283 int nft_register_expr(struct nft_expr_type *);
1284 void nft_unregister_expr(struct nft_expr_type *);
1286 int nft_verdict_dump(struct sk_buff *skb, int type,
1287 const struct nft_verdict *v);
1290 * struct nft_object_hash_key - key to lookup nft_object
1292 * @name: name of the stateful object to look up
1293 * @table: table the object belongs to
1295 struct nft_object_hash_key {
1297 const struct nft_table *table;
1301 * struct nft_object - nf_tables stateful object
1303 * @list: table stateful object list node
1304 * @rhlhead: nft_objname_ht node
1305 * @key: keys that identify this object
1306 * @genmask: generation mask
1307 * @use: number of references to this stateful object
1308 * @handle: unique object handle
1309 * @udlen: length of user data
1311 * @ops: object operations
1312 * @data: object data, layout depends on type
1315 struct list_head list;
1316 struct rhlist_head rhlhead;
1317 struct nft_object_hash_key key;
1323 /* runtime data below here */
1324 const struct nft_object_ops *ops ____cacheline_aligned;
1325 unsigned char data[]
1326 __attribute__((aligned(__alignof__(u64))));
1329 static inline void *nft_obj_data(const struct nft_object *obj)
1331 return (void *)obj->data;
1334 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1336 struct nft_object *nft_obj_lookup(const struct net *net,
1337 const struct nft_table *table,
1338 const struct nlattr *nla, u32 objtype,
1341 void nft_obj_notify(struct net *net, const struct nft_table *table,
1342 struct nft_object *obj, u32 portid, u32 seq,
1343 int event, u16 flags, int family, int report, gfp_t gfp);
1346 * struct nft_object_type - stateful object type
1348 * @select_ops: function to select nft_object_ops
1349 * @ops: default ops, used when no select_ops functions is present
1350 * @list: list node in list of object types
1351 * @type: stateful object numeric type
1352 * @owner: module owner
1353 * @maxattr: maximum netlink attribute
1354 * @policy: netlink attribute policy
1356 struct nft_object_type {
1357 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1358 const struct nlattr * const tb[]);
1359 const struct nft_object_ops *ops;
1360 struct list_head list;
1362 unsigned int maxattr;
1363 struct module *owner;
1364 const struct nla_policy *policy;
1368 * struct nft_object_ops - stateful object operations
1370 * @eval: stateful object evaluation function
1371 * @size: stateful object size
1372 * @init: initialize object from netlink attributes
1373 * @destroy: release existing stateful object
1374 * @dump: netlink dump stateful object
1375 * @update: update stateful object
1376 * @type: pointer to object type
1378 struct nft_object_ops {
1379 void (*eval)(struct nft_object *obj,
1380 struct nft_regs *regs,
1381 const struct nft_pktinfo *pkt);
1383 int (*init)(const struct nft_ctx *ctx,
1384 const struct nlattr *const tb[],
1385 struct nft_object *obj);
1386 void (*destroy)(const struct nft_ctx *ctx,
1387 struct nft_object *obj);
1388 int (*dump)(struct sk_buff *skb,
1389 struct nft_object *obj,
1391 void (*update)(struct nft_object *obj,
1392 struct nft_object *newobj);
1393 const struct nft_object_type *type;
1396 int nft_register_obj(struct nft_object_type *obj_type);
1397 void nft_unregister_obj(struct nft_object_type *obj_type);
1399 #define NFT_NETDEVICE_MAX 256
1402 * struct nft_flowtable - nf_tables flow table
1404 * @list: flow table list node in table list
1405 * @table: the table the flow table is contained in
1406 * @name: name of this flow table
1407 * @hooknum: hook number
1408 * @ops_len: number of hooks in array
1409 * @genmask: generation mask
1410 * @use: number of references to this flow table
1411 * @handle: unique object handle
1412 * @hook_list: hook list for hooks per net_device in flowtables
1413 * @data: rhashtable and garbage collector
1415 struct nft_flowtable {
1416 struct list_head list;
1417 struct nft_table *table;
1424 /* runtime data below here */
1425 struct list_head hook_list ____cacheline_aligned;
1426 struct nf_flowtable data;
1429 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1430 const struct nlattr *nla,
1433 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1434 struct nft_flowtable *flowtable,
1435 enum nft_trans_phase phase);
1437 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1438 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1441 * struct nft_traceinfo - nft tracing information and state
1443 * @trace: other struct members are initialised
1444 * @nf_trace: copy of skb->nf_trace before rule evaluation
1445 * @type: event type (enum nft_trace_types)
1446 * @skbid: hash of skb to be used as trace id
1447 * @packet_dumped: packet headers sent in a previous traceinfo message
1448 * @basechain: base chain currently processed
1450 struct nft_traceinfo {
1454 enum nft_trace_types type:8;
1456 const struct nft_base_chain *basechain;
1459 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1460 const struct nft_chain *basechain);
1462 void nft_trace_notify(const struct nft_pktinfo *pkt,
1463 const struct nft_verdict *verdict,
1464 const struct nft_rule_dp *rule,
1465 struct nft_traceinfo *info);
1467 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1468 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1470 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1471 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1473 #define MODULE_ALIAS_NFT_EXPR(name) \
1474 MODULE_ALIAS("nft-expr-" name)
1476 #define MODULE_ALIAS_NFT_OBJ(type) \
1477 MODULE_ALIAS("nft-obj-" __stringify(type))
1479 #if IS_ENABLED(CONFIG_NF_TABLES)
1482 * The gencursor defines two generations, the currently active and the
1483 * next one. Objects contain a bitmask of 2 bits specifying the generations
1484 * they're active in. A set bit means they're inactive in the generation
1485 * represented by that bit.
1487 * New objects start out as inactive in the current and active in the
1488 * next generation. When committing the ruleset the bitmask is cleared,
1489 * meaning they're active in all generations. When removing an object,
1490 * it is set inactive in the next generation. After committing the ruleset,
1491 * the objects are removed.
1493 static inline unsigned int nft_gencursor_next(const struct net *net)
1495 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1498 static inline u8 nft_genmask_next(const struct net *net)
1500 return 1 << nft_gencursor_next(net);
1503 static inline u8 nft_genmask_cur(const struct net *net)
1505 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1506 return 1 << READ_ONCE(net->nft.gencursor);
1509 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1512 * Generic transaction helpers
1515 /* Check if this object is currently active. */
1516 #define nft_is_active(__net, __obj) \
1517 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1519 /* Check if this object is active in the next generation. */
1520 #define nft_is_active_next(__net, __obj) \
1521 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1523 /* This object becomes active in the next generation. */
1524 #define nft_activate_next(__net, __obj) \
1525 (__obj)->genmask = nft_genmask_cur(__net)
1527 /* This object becomes inactive in the next generation. */
1528 #define nft_deactivate_next(__net, __obj) \
1529 (__obj)->genmask = nft_genmask_next(__net)
1531 /* After committing the ruleset, clear the stale generation bit. */
1532 #define nft_clear(__net, __obj) \
1533 (__obj)->genmask &= ~nft_genmask_next(__net)
1534 #define nft_active_genmask(__obj, __genmask) \
1535 !((__obj)->genmask & __genmask)
1538 * Set element transaction helpers
1541 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1544 return !(ext->genmask & genmask);
1547 static inline void nft_set_elem_change_active(const struct net *net,
1548 const struct nft_set *set,
1549 struct nft_set_ext *ext)
1551 ext->genmask ^= nft_genmask_next(net);
1554 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1556 #define NFT_SET_ELEM_DEAD_MASK (1 << 2)
1558 #if defined(__LITTLE_ENDIAN_BITFIELD)
1559 #define NFT_SET_ELEM_DEAD_BIT 2
1560 #elif defined(__BIG_ENDIAN_BITFIELD)
1561 #define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1566 static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1568 unsigned long *word = (unsigned long *)ext;
1570 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1571 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1574 static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1576 unsigned long *word = (unsigned long *)ext;
1578 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1579 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1583 * struct nft_trans - nf_tables object update in transaction
1585 * @list: used internally
1586 * @binding_list: list of objects with possible bindings
1587 * @msg_type: message type
1588 * @put_net: ctx->net needs to be put
1589 * @ctx: transaction context
1590 * @data: internal information related to the transaction
1593 struct list_head list;
1594 struct list_head binding_list;
1601 struct nft_trans_rule {
1602 struct nft_rule *rule;
1603 struct nft_flow_rule *flow;
1608 #define nft_trans_rule(trans) \
1609 (((struct nft_trans_rule *)trans->data)->rule)
1610 #define nft_trans_flow_rule(trans) \
1611 (((struct nft_trans_rule *)trans->data)->flow)
1612 #define nft_trans_rule_id(trans) \
1613 (((struct nft_trans_rule *)trans->data)->rule_id)
1614 #define nft_trans_rule_bound(trans) \
1615 (((struct nft_trans_rule *)trans->data)->bound)
1617 struct nft_trans_set {
1618 struct nft_set *set;
1627 #define nft_trans_set(trans) \
1628 (((struct nft_trans_set *)trans->data)->set)
1629 #define nft_trans_set_id(trans) \
1630 (((struct nft_trans_set *)trans->data)->set_id)
1631 #define nft_trans_set_bound(trans) \
1632 (((struct nft_trans_set *)trans->data)->bound)
1633 #define nft_trans_set_update(trans) \
1634 (((struct nft_trans_set *)trans->data)->update)
1635 #define nft_trans_set_timeout(trans) \
1636 (((struct nft_trans_set *)trans->data)->timeout)
1637 #define nft_trans_set_gc_int(trans) \
1638 (((struct nft_trans_set *)trans->data)->gc_int)
1639 #define nft_trans_set_size(trans) \
1640 (((struct nft_trans_set *)trans->data)->size)
1642 struct nft_trans_chain {
1643 struct nft_chain *chain;
1646 struct nft_stats __percpu *stats;
1650 struct nft_base_chain *basechain;
1651 struct list_head hook_list;
1654 #define nft_trans_chain(trans) \
1655 (((struct nft_trans_chain *)trans->data)->chain)
1656 #define nft_trans_chain_update(trans) \
1657 (((struct nft_trans_chain *)trans->data)->update)
1658 #define nft_trans_chain_name(trans) \
1659 (((struct nft_trans_chain *)trans->data)->name)
1660 #define nft_trans_chain_stats(trans) \
1661 (((struct nft_trans_chain *)trans->data)->stats)
1662 #define nft_trans_chain_policy(trans) \
1663 (((struct nft_trans_chain *)trans->data)->policy)
1664 #define nft_trans_chain_bound(trans) \
1665 (((struct nft_trans_chain *)trans->data)->bound)
1666 #define nft_trans_chain_id(trans) \
1667 (((struct nft_trans_chain *)trans->data)->chain_id)
1668 #define nft_trans_basechain(trans) \
1669 (((struct nft_trans_chain *)trans->data)->basechain)
1670 #define nft_trans_chain_hooks(trans) \
1671 (((struct nft_trans_chain *)trans->data)->hook_list)
1673 struct nft_trans_table {
1677 #define nft_trans_table_update(trans) \
1678 (((struct nft_trans_table *)trans->data)->update)
1680 struct nft_trans_elem {
1681 struct nft_set *set;
1682 struct nft_elem_priv *elem_priv;
1686 #define nft_trans_elem_set(trans) \
1687 (((struct nft_trans_elem *)trans->data)->set)
1688 #define nft_trans_elem_priv(trans) \
1689 (((struct nft_trans_elem *)trans->data)->elem_priv)
1690 #define nft_trans_elem_set_bound(trans) \
1691 (((struct nft_trans_elem *)trans->data)->bound)
1693 struct nft_trans_obj {
1694 struct nft_object *obj;
1695 struct nft_object *newobj;
1699 #define nft_trans_obj(trans) \
1700 (((struct nft_trans_obj *)trans->data)->obj)
1701 #define nft_trans_obj_newobj(trans) \
1702 (((struct nft_trans_obj *)trans->data)->newobj)
1703 #define nft_trans_obj_update(trans) \
1704 (((struct nft_trans_obj *)trans->data)->update)
1706 struct nft_trans_flowtable {
1707 struct nft_flowtable *flowtable;
1709 struct list_head hook_list;
1713 #define nft_trans_flowtable(trans) \
1714 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1715 #define nft_trans_flowtable_update(trans) \
1716 (((struct nft_trans_flowtable *)trans->data)->update)
1717 #define nft_trans_flowtable_hooks(trans) \
1718 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1719 #define nft_trans_flowtable_flags(trans) \
1720 (((struct nft_trans_flowtable *)trans->data)->flags)
1722 #define NFT_TRANS_GC_BATCHCOUNT 256
1724 struct nft_trans_gc {
1725 struct list_head list;
1727 struct nft_set *set;
1730 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT];
1731 struct rcu_head rcu;
1734 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1735 unsigned int gc_seq, gfp_t gfp);
1736 void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1738 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1739 unsigned int gc_seq, gfp_t gfp);
1740 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1742 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1743 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1745 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1747 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1748 unsigned int gc_seq);
1749 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
1751 void nft_setelem_data_deactivate(const struct net *net,
1752 const struct nft_set *set,
1753 struct nft_elem_priv *elem_priv);
1755 int __init nft_chain_filter_init(void);
1756 void nft_chain_filter_fini(void);
1758 void __init nft_chain_route_init(void);
1759 void nft_chain_route_fini(void);
1761 void nf_tables_trans_destroy_flush_work(void);
1763 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1764 __be64 nf_jiffies64_to_msecs(u64 input);
1766 #ifdef CONFIG_MODULES
1767 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1769 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1772 struct nftables_pernet {
1773 struct list_head tables;
1774 struct list_head commit_list;
1775 struct list_head binding_list;
1776 struct list_head module_list;
1777 struct list_head notify_list;
1778 struct mutex commit_mutex;
1780 unsigned int base_seq;
1781 unsigned int gc_seq;
1785 extern unsigned int nf_tables_net_id;
1787 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1789 return net_generic(net, nf_tables_net_id);
1792 #define __NFT_REDUCE_READONLY 1UL
1793 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1795 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1797 return expr->ops->reduce == NFT_REDUCE_READONLY;
1800 void nft_reg_track_update(struct nft_regs_track *track,
1801 const struct nft_expr *expr, u8 dreg, u8 len);
1802 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1803 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1805 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1806 const struct nft_expr *expr, u8 dreg)
1808 return track->regs[dreg].selector &&
1809 track->regs[dreg].selector->ops == expr->ops &&
1810 track->regs[dreg].num_reg == 0;
1813 #endif /* _NET_NF_TABLES_H */