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)
312 * enum nft_iter_type - nftables set iterator type
314 * @NFT_ITER_READ: read-only iteration over set elements
315 * @NFT_ITER_UPDATE: iteration under mutex to update set element state
324 struct nft_set_iter {
326 enum nft_iter_type type:8;
330 int (*fn)(const struct nft_ctx *ctx,
332 const struct nft_set_iter *iter,
333 struct nft_elem_priv *elem_priv);
337 * struct nft_set_desc - description of set elements
343 * @objtype: object type
344 * @size: number of set elements
345 * @policy: set policy
346 * @gc_int: garbage collector interval
347 * @timeout: element timeout
348 * @field_len: length of each field in concatenation, bytes
349 * @field_count: number of concatenated fields in element
350 * @expr: set must support for expressions
352 struct nft_set_desc {
362 u8 field_len[NFT_REG32_COUNT];
368 * enum nft_set_class - performance class
370 * @NFT_SET_CLASS_O_1: constant, O(1)
371 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N)
372 * @NFT_SET_CLASS_O_N: linear, O(N)
376 NFT_SET_CLASS_O_LOG_N,
381 * struct nft_set_estimate - estimation of memory and performance
384 * @size: required memory
385 * @lookup: lookup performance class
386 * @space: memory class
388 struct nft_set_estimate {
390 enum nft_set_class lookup;
391 enum nft_set_class space;
394 #define NFT_EXPR_MAXATTR 16
395 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
396 ALIGN(size, __alignof__(struct nft_expr)))
399 * struct nft_expr - nf_tables expression
401 * @ops: expression ops
402 * @data: expression private data
405 const struct nft_expr_ops *ops;
407 __attribute__((aligned(__alignof__(u64))));
410 static inline void *nft_expr_priv(const struct nft_expr *expr)
412 return (void *)expr->data;
415 struct nft_expr_info;
417 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
418 struct nft_expr_info *info);
419 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp);
420 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
421 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
422 const struct nft_expr *expr, bool reset);
423 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
424 const struct nft_expr *expr);
429 * struct nft_set_ops - nf_tables set operations
431 * @lookup: look up an element within the set
432 * @update: update an element if exists, add it if doesn't exist
433 * @delete: delete an element
434 * @insert: insert new element into set
435 * @activate: activate new element in the next generation
436 * @deactivate: lookup for element and deactivate it in the next generation
437 * @flush: deactivate element in the next generation
438 * @remove: remove element from set
439 * @walk: iterate over all set elements
440 * @get: get set elements
441 * @commit: commit set elements
442 * @abort: abort set elements
443 * @privsize: function to return size of set private data
444 * @estimate: estimate the required memory size and the lookup complexity class
445 * @init: initialize private data of new set instance
446 * @destroy: destroy private data of set instance
447 * @gc_init: initialize garbage collection
448 * @elemsize: element private size
450 * Operations lookup, update and delete have simpler interfaces, are faster
451 * and currently only used in the packet path. All the rest are slower,
452 * control plane functions.
455 bool (*lookup)(const struct net *net,
456 const struct nft_set *set,
458 const struct nft_set_ext **ext);
459 bool (*update)(struct nft_set *set,
461 struct nft_elem_priv *
462 (*new)(struct nft_set *,
463 const struct nft_expr *,
465 const struct nft_expr *expr,
466 struct nft_regs *regs,
467 const struct nft_set_ext **ext);
468 bool (*delete)(const struct nft_set *set,
471 int (*insert)(const struct net *net,
472 const struct nft_set *set,
473 const struct nft_set_elem *elem,
474 struct nft_elem_priv **priv);
475 void (*activate)(const struct net *net,
476 const struct nft_set *set,
477 struct nft_elem_priv *elem_priv);
478 struct nft_elem_priv * (*deactivate)(const struct net *net,
479 const struct nft_set *set,
480 const struct nft_set_elem *elem);
481 void (*flush)(const struct net *net,
482 const struct nft_set *set,
483 struct nft_elem_priv *priv);
484 void (*remove)(const struct net *net,
485 const struct nft_set *set,
486 struct nft_elem_priv *elem_priv);
487 void (*walk)(const struct nft_ctx *ctx,
489 struct nft_set_iter *iter);
490 struct nft_elem_priv * (*get)(const struct net *net,
491 const struct nft_set *set,
492 const struct nft_set_elem *elem,
494 void (*commit)(struct nft_set *set);
495 void (*abort)(const struct nft_set *set);
496 u64 (*privsize)(const struct nlattr * const nla[],
497 const struct nft_set_desc *desc);
498 bool (*estimate)(const struct nft_set_desc *desc,
500 struct nft_set_estimate *est);
501 int (*init)(const struct nft_set *set,
502 const struct nft_set_desc *desc,
503 const struct nlattr * const nla[]);
504 void (*destroy)(const struct nft_ctx *ctx,
505 const struct nft_set *set);
506 void (*gc_init)(const struct nft_set *set);
508 unsigned int elemsize;
512 * struct nft_set_type - nf_tables set type
514 * @ops: set ops for this type
515 * @features: features supported by the implementation
517 struct nft_set_type {
518 const struct nft_set_ops ops;
521 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
523 struct nft_set_elem_expr {
526 __attribute__((aligned(__alignof__(struct nft_expr))));
529 #define nft_setelem_expr_at(__elem_expr, __offset) \
530 ((struct nft_expr *)&__elem_expr->data[__offset])
532 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
533 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
534 __size < (__elem_expr)->size; \
535 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
537 #define NFT_SET_EXPR_MAX 2
540 * struct nft_set - nf_tables set instance
542 * @list: table set list node
543 * @bindings: list of set bindings
544 * @refs: internal refcounting for async set destruction
545 * @table: table this set belongs to
546 * @net: netnamespace this set belongs to
547 * @name: name of the set
548 * @handle: unique handle of the set
549 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
550 * @dtype: data type (verdict or numeric type defined by userspace)
551 * @objtype: object type (see NFT_OBJECT_* definitions)
552 * @size: maximum set size
553 * @field_len: length of each field in concatenation, bytes
554 * @field_count: number of concatenated fields in element
555 * @use: number of rules references to this set
556 * @nelems: number of elements
557 * @ndeact: number of deactivated elements queued for removal
558 * @timeout: default timeout value in jiffies
559 * @gc_int: garbage collection interval in msecs
560 * @policy: set parameterization (see enum nft_set_policies)
561 * @udlen: user data length
563 * @pending_update: list of pending update set element
566 * @dead: set will be freed, never cleared
567 * @genmask: generation mask
570 * @num_exprs: numbers of exprs
571 * @exprs: stateful expression
572 * @catchall_list: list of catch-all set element
573 * @data: private set data
576 struct list_head list;
577 struct list_head bindings;
579 struct nft_table *table;
587 u8 field_len[NFT_REG32_COUNT];
596 unsigned char *udata;
597 struct list_head pending_update;
598 /* runtime data below here */
599 const struct nft_set_ops *ops ____cacheline_aligned;
606 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
607 struct list_head catchall_list;
609 __attribute__((aligned(__alignof__(u64))));
612 static inline bool nft_set_is_anonymous(const struct nft_set *set)
614 return set->flags & NFT_SET_ANONYMOUS;
617 static inline void *nft_set_priv(const struct nft_set *set)
619 return (void *)set->data;
622 static inline enum nft_data_types nft_set_datatype(const struct nft_set *set)
624 return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
627 static inline bool nft_set_gc_is_pending(const struct nft_set *s)
629 return refcount_read(&s->refs) != 1;
632 static inline struct nft_set *nft_set_container_of(const void *priv)
634 return (void *)priv - offsetof(struct nft_set, data);
637 struct nft_set *nft_set_lookup_global(const struct net *net,
638 const struct nft_table *table,
639 const struct nlattr *nla_set_name,
640 const struct nlattr *nla_set_id,
643 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
644 const struct nft_set *set);
646 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
648 u32 gc_int = READ_ONCE(set->gc_int);
650 return gc_int ? msecs_to_jiffies(gc_int) : HZ;
654 * struct nft_set_binding - nf_tables set binding
656 * @list: set bindings list node
657 * @chain: chain containing the rule bound to the set
658 * @flags: set action flags
660 * A set binding contains all information necessary for validation
661 * of new elements added to a bound set.
663 struct nft_set_binding {
664 struct list_head list;
665 const struct nft_chain *chain;
669 enum nft_trans_phase;
670 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
671 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
672 struct nft_set_binding *binding,
673 enum nft_trans_phase phase);
674 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
675 struct nft_set_binding *binding);
676 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
679 * enum nft_set_extensions - set extension type IDs
681 * @NFT_SET_EXT_KEY: element key
682 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
683 * @NFT_SET_EXT_DATA: mapping data
684 * @NFT_SET_EXT_FLAGS: element flags
685 * @NFT_SET_EXT_TIMEOUT: element timeout
686 * @NFT_SET_EXT_EXPIRATION: element expiration time
687 * @NFT_SET_EXT_USERDATA: user data associated with the element
688 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
689 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
690 * @NFT_SET_EXT_NUM: number of extension types
692 enum nft_set_extensions {
698 NFT_SET_EXT_EXPIRATION,
699 NFT_SET_EXT_USERDATA,
700 NFT_SET_EXT_EXPRESSIONS,
706 * struct nft_set_ext_type - set extension type
708 * @len: fixed part length of the extension
709 * @align: alignment requirements of the extension
711 struct nft_set_ext_type {
716 extern const struct nft_set_ext_type nft_set_ext_types[];
719 * struct nft_set_ext_tmpl - set extension template
721 * @len: length of extension area
722 * @offset: offsets of individual extension types
723 * @ext_len: length of the expected extension(used to sanity check)
725 struct nft_set_ext_tmpl {
727 u8 offset[NFT_SET_EXT_NUM];
728 u8 ext_len[NFT_SET_EXT_NUM];
732 * struct nft_set_ext - set extensions
734 * @genmask: generation mask
735 * @offset: offsets of individual extension types
736 * @data: beginning of extension data
740 u8 offset[NFT_SET_EXT_NUM];
744 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
746 memset(tmpl, 0, sizeof(*tmpl));
747 tmpl->len = sizeof(struct nft_set_ext);
750 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
753 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
754 if (tmpl->len > U8_MAX)
757 tmpl->offset[id] = tmpl->len;
758 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
759 tmpl->len += tmpl->ext_len[id];
764 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
766 return nft_set_ext_add_length(tmpl, id, 0);
769 static inline void nft_set_ext_init(struct nft_set_ext *ext,
770 const struct nft_set_ext_tmpl *tmpl)
772 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
775 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
777 return !!ext->offset[id];
780 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
782 return ext && __nft_set_ext_exists(ext, id);
785 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
787 return (void *)ext + ext->offset[id];
790 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
792 return nft_set_ext(ext, NFT_SET_EXT_KEY);
795 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
797 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
800 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
802 return nft_set_ext(ext, NFT_SET_EXT_DATA);
805 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
807 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
810 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
812 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
815 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
817 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
820 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
822 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
825 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
827 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
830 static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext,
833 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
834 time_after_eq64(tstamp, *nft_set_ext_expiration(ext));
837 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
839 return __nft_set_elem_expired(ext, get_jiffies_64());
842 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
843 const struct nft_elem_priv *elem_priv)
845 return (void *)elem_priv + set->ops->elemsize;
848 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
850 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
853 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
854 const struct nft_set *set,
855 const struct nlattr *attr);
857 struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
858 const struct nft_set_ext_tmpl *tmpl,
859 const u32 *key, const u32 *key_end,
861 u64 timeout, u64 expiration, gfp_t gfp);
862 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
863 struct nft_expr *expr_array[]);
864 void nft_set_elem_destroy(const struct nft_set *set,
865 const struct nft_elem_priv *elem_priv,
867 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
868 const struct nft_set *set,
869 const struct nft_elem_priv *elem_priv);
873 * struct nft_expr_type - nf_tables expression type
875 * @select_ops: function to select nft_expr_ops
876 * @release_ops: release nft_expr_ops
877 * @ops: default ops, used when no select_ops functions is present
878 * @inner_ops: inner ops, used for inner packet operation
879 * @list: used internally
881 * @owner: module reference
882 * @policy: netlink attribute policy
883 * @maxattr: highest netlink attribute number
884 * @family: address family for AF-specific types
885 * @flags: expression type flags
887 struct nft_expr_type {
888 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
889 const struct nlattr * const tb[]);
890 void (*release_ops)(const struct nft_expr_ops *ops);
891 const struct nft_expr_ops *ops;
892 const struct nft_expr_ops *inner_ops;
893 struct list_head list;
895 struct module *owner;
896 const struct nla_policy *policy;
897 unsigned int maxattr;
902 #define NFT_EXPR_STATEFUL 0x1
903 #define NFT_EXPR_GC 0x2
905 enum nft_trans_phase {
907 NFT_TRANS_PREPARE_ERROR,
913 struct nft_flow_rule;
914 struct nft_offload_ctx;
917 * struct nft_expr_ops - nf_tables expression operations
919 * @eval: Expression evaluation function
920 * @clone: Expression clone function
921 * @size: full expression size, including private data size
922 * @init: initialization function
923 * @activate: activate expression in the next generation
924 * @deactivate: deactivate expression in next generation
925 * @destroy: destruction function, called after synchronize_rcu
926 * @destroy_clone: destruction clone function
927 * @dump: function to dump parameters
928 * @validate: validate expression, called during loop detection
929 * @reduce: reduce expression
930 * @gc: garbage collection expression
931 * @offload: hardware offload expression
932 * @offload_action: function to report true/false to allocate one slot or not in the flow
934 * @offload_stats: function to synchronize hardware stats via updating the counter expression
935 * @type: expression type
936 * @data: extra data to attach to this expression operation
938 struct nft_expr_ops {
939 void (*eval)(const struct nft_expr *expr,
940 struct nft_regs *regs,
941 const struct nft_pktinfo *pkt);
942 int (*clone)(struct nft_expr *dst,
943 const struct nft_expr *src, gfp_t gfp);
946 int (*init)(const struct nft_ctx *ctx,
947 const struct nft_expr *expr,
948 const struct nlattr * const tb[]);
949 void (*activate)(const struct nft_ctx *ctx,
950 const struct nft_expr *expr);
951 void (*deactivate)(const struct nft_ctx *ctx,
952 const struct nft_expr *expr,
953 enum nft_trans_phase phase);
954 void (*destroy)(const struct nft_ctx *ctx,
955 const struct nft_expr *expr);
956 void (*destroy_clone)(const struct nft_ctx *ctx,
957 const struct nft_expr *expr);
958 int (*dump)(struct sk_buff *skb,
959 const struct nft_expr *expr,
961 int (*validate)(const struct nft_ctx *ctx,
962 const struct nft_expr *expr,
963 const struct nft_data **data);
964 bool (*reduce)(struct nft_regs_track *track,
965 const struct nft_expr *expr);
966 bool (*gc)(struct net *net,
967 const struct nft_expr *expr);
968 int (*offload)(struct nft_offload_ctx *ctx,
969 struct nft_flow_rule *flow,
970 const struct nft_expr *expr);
971 bool (*offload_action)(const struct nft_expr *expr);
972 void (*offload_stats)(struct nft_expr *expr,
973 const struct flow_stats *stats);
974 const struct nft_expr_type *type;
979 * struct nft_rule - nf_tables rule
981 * @list: used internally
982 * @handle: rule handle
983 * @genmask: generation mask
984 * @dlen: length of expression data
985 * @udata: user data is appended to the rule
986 * @data: expression data
989 struct list_head list;
995 __attribute__((aligned(__alignof__(struct nft_expr))));
998 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
1000 return (struct nft_expr *)&rule->data[0];
1003 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
1005 return ((void *)expr) + expr->ops->size;
1008 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
1010 return (struct nft_expr *)&rule->data[rule->dlen];
1013 static inline bool nft_expr_more(const struct nft_rule *rule,
1014 const struct nft_expr *expr)
1016 return expr != nft_expr_last(rule) && expr->ops;
1019 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
1021 return (void *)&rule->data[rule->dlen];
1024 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule);
1025 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
1026 enum nft_trans_phase phase);
1027 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule);
1029 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
1030 struct nft_regs *regs,
1031 const struct nft_pktinfo *pkt)
1033 struct nft_set_elem_expr *elem_expr;
1034 struct nft_expr *expr;
1037 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
1038 elem_expr = nft_set_ext_expr(ext);
1039 nft_setelem_expr_foreach(expr, elem_expr, size) {
1040 expr->ops->eval(expr, regs, pkt);
1041 if (regs->verdict.code == NFT_BREAK)
1048 * The last pointer isn't really necessary, but the compiler isn't able to
1049 * determine that the result of nft_expr_last() is always the same since it
1050 * can't assume that the dlen value wasn't changed within calls in the loop.
1052 #define nft_rule_for_each_expr(expr, last, rule) \
1053 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1055 (expr) = nft_expr_next(expr))
1057 #define NFT_CHAIN_POLICY_UNSET U8_MAX
1059 struct nft_rule_dp {
1062 handle:42; /* for tracing */
1063 unsigned char data[]
1064 __attribute__((aligned(__alignof__(struct nft_expr))));
1067 struct nft_rule_dp_last {
1068 struct nft_rule_dp end; /* end of nft_rule_blob marker */
1069 struct rcu_head h; /* call_rcu head */
1070 struct nft_rule_blob *blob; /* ptr to free via call_rcu */
1071 const struct nft_chain *chain; /* for nftables tracing */
1074 static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule)
1076 return (void *)rule + sizeof(*rule) + rule->dlen;
1079 struct nft_rule_blob {
1081 unsigned char data[]
1082 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1086 * struct nft_chain - nf_tables chain
1088 * @blob_gen_0: rule blob pointer to the current generation
1089 * @blob_gen_1: rule blob pointer to the future generation
1090 * @rules: list of rules in the chain
1091 * @list: used internally
1092 * @rhlhead: used internally
1093 * @table: table that this chain belongs to
1094 * @handle: chain handle
1095 * @use: number of jump references to this chain
1096 * @flags: bitmask of enum NFTA_CHAIN_FLAGS
1097 * @bound: bind or not
1098 * @genmask: generation mask
1099 * @name: name of the chain
1100 * @udlen: user data length
1101 * @udata: user data in the chain
1102 * @blob_next: rule blob pointer to the next in the chain
1105 struct nft_rule_blob __rcu *blob_gen_0;
1106 struct nft_rule_blob __rcu *blob_gen_1;
1107 struct list_head rules;
1108 struct list_head list;
1109 struct rhlist_head rhlhead;
1110 struct nft_table *table;
1120 /* Only used during control plane commit phase: */
1121 struct nft_rule_blob *blob_next;
1124 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1125 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1126 const struct nft_set_iter *iter,
1127 struct nft_elem_priv *elem_priv);
1128 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
1129 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1130 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1132 enum nft_chain_types {
1133 NFT_CHAIN_T_DEFAULT = 0,
1140 * struct nft_chain_type - nf_tables chain type info
1142 * @name: name of the type
1143 * @type: numeric identifier
1144 * @family: address family
1145 * @owner: module owner
1146 * @hook_mask: mask of valid hooks
1147 * @hooks: array of hook functions
1148 * @ops_register: base chain register function
1149 * @ops_unregister: base chain unregister function
1151 struct nft_chain_type {
1153 enum nft_chain_types type;
1155 struct module *owner;
1156 unsigned int hook_mask;
1157 nf_hookfn *hooks[NFT_MAX_HOOKS];
1158 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1159 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1162 int nft_chain_validate_dependency(const struct nft_chain *chain,
1163 enum nft_chain_types type);
1164 int nft_chain_validate_hooks(const struct nft_chain *chain,
1165 unsigned int hook_flags);
1167 static inline bool nft_chain_binding(const struct nft_chain *chain)
1169 return chain->flags & NFT_CHAIN_BINDING;
1172 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1174 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1177 int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
1178 void nft_chain_del(struct nft_chain *chain);
1179 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1184 struct u64_stats_sync syncp;
1188 struct list_head list;
1189 struct nf_hook_ops ops;
1190 struct rcu_head rcu;
1194 * struct nft_base_chain - nf_tables base chain
1196 * @ops: netfilter hook ops
1197 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1199 * @policy: default policy
1200 * @flags: indicate the base chain disabled or not
1201 * @stats: per-cpu chain stats
1203 * @flow_block: flow block (for hardware offload)
1205 struct nft_base_chain {
1206 struct nf_hook_ops ops;
1207 struct list_head hook_list;
1208 const struct nft_chain_type *type;
1211 struct nft_stats __percpu *stats;
1212 struct nft_chain chain;
1213 struct flow_block flow_block;
1216 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1218 return container_of(chain, struct nft_base_chain, chain);
1221 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1223 return chain->flags & NFT_CHAIN_BASE;
1226 int __nft_release_basechain(struct nft_ctx *ctx);
1228 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1230 static inline bool nft_use_inc(u32 *use)
1232 if (*use == UINT_MAX)
1240 static inline void nft_use_dec(u32 *use)
1242 WARN_ON_ONCE((*use)-- == 0);
1245 /* For error and abort path: restore use counter to previous state. */
1246 static inline void nft_use_inc_restore(u32 *use)
1248 WARN_ON_ONCE(!nft_use_inc(use));
1251 #define nft_use_dec_restore nft_use_dec
1254 * struct nft_table - nf_tables table
1256 * @list: used internally
1257 * @chains_ht: chains in the table
1258 * @chains: same, for stable walks
1259 * @sets: sets in the table
1260 * @objects: stateful objects in the table
1261 * @flowtables: flow tables in the table
1262 * @hgenerator: handle generator state
1263 * @handle: table handle
1264 * @use: number of chain references to this table
1265 * @family:address family
1266 * @flags: table flag (see enum nft_table_flags)
1267 * @genmask: generation mask
1268 * @nlpid: netlink port ID
1269 * @name: name of the table
1270 * @udlen: length of the user data
1272 * @validate_state: internal, set when transaction adds jumps
1275 struct list_head list;
1276 struct rhltable chains_ht;
1277 struct list_head chains;
1278 struct list_head sets;
1279 struct list_head objects;
1280 struct list_head flowtables;
1294 static inline bool nft_table_has_owner(const struct nft_table *table)
1296 return table->flags & NFT_TABLE_F_OWNER;
1299 static inline bool nft_table_is_orphan(const struct nft_table *table)
1301 return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) ==
1302 NFT_TABLE_F_PERSIST;
1305 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1307 return family == NFPROTO_NETDEV ||
1308 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1311 void nft_register_chain_type(const struct nft_chain_type *);
1312 void nft_unregister_chain_type(const struct nft_chain_type *);
1314 int nft_register_expr(struct nft_expr_type *);
1315 void nft_unregister_expr(struct nft_expr_type *);
1317 int nft_verdict_dump(struct sk_buff *skb, int type,
1318 const struct nft_verdict *v);
1321 * struct nft_object_hash_key - key to lookup nft_object
1323 * @name: name of the stateful object to look up
1324 * @table: table the object belongs to
1326 struct nft_object_hash_key {
1328 const struct nft_table *table;
1332 * struct nft_object - nf_tables stateful object
1334 * @list: table stateful object list node
1335 * @rhlhead: nft_objname_ht node
1336 * @key: keys that identify this object
1337 * @genmask: generation mask
1338 * @use: number of references to this stateful object
1339 * @handle: unique object handle
1340 * @udlen: length of user data
1342 * @ops: object operations
1343 * @data: object data, layout depends on type
1346 struct list_head list;
1347 struct rhlist_head rhlhead;
1348 struct nft_object_hash_key key;
1354 /* runtime data below here */
1355 const struct nft_object_ops *ops ____cacheline_aligned;
1356 unsigned char data[]
1357 __attribute__((aligned(__alignof__(u64))));
1360 static inline void *nft_obj_data(const struct nft_object *obj)
1362 return (void *)obj->data;
1365 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1367 struct nft_object *nft_obj_lookup(const struct net *net,
1368 const struct nft_table *table,
1369 const struct nlattr *nla, u32 objtype,
1372 void nft_obj_notify(struct net *net, const struct nft_table *table,
1373 struct nft_object *obj, u32 portid, u32 seq,
1374 int event, u16 flags, int family, int report, gfp_t gfp);
1377 * struct nft_object_type - stateful object type
1379 * @select_ops: function to select nft_object_ops
1380 * @ops: default ops, used when no select_ops functions is present
1381 * @list: list node in list of object types
1382 * @type: stateful object numeric type
1383 * @owner: module owner
1384 * @maxattr: maximum netlink attribute
1385 * @family: address family for AF-specific object types
1386 * @policy: netlink attribute policy
1388 struct nft_object_type {
1389 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1390 const struct nlattr * const tb[]);
1391 const struct nft_object_ops *ops;
1392 struct list_head list;
1394 unsigned int maxattr;
1396 struct module *owner;
1397 const struct nla_policy *policy;
1401 * struct nft_object_ops - stateful object operations
1403 * @eval: stateful object evaluation function
1404 * @size: stateful object size
1405 * @init: initialize object from netlink attributes
1406 * @destroy: release existing stateful object
1407 * @dump: netlink dump stateful object
1408 * @update: update stateful object
1409 * @type: pointer to object type
1411 struct nft_object_ops {
1412 void (*eval)(struct nft_object *obj,
1413 struct nft_regs *regs,
1414 const struct nft_pktinfo *pkt);
1416 int (*init)(const struct nft_ctx *ctx,
1417 const struct nlattr *const tb[],
1418 struct nft_object *obj);
1419 void (*destroy)(const struct nft_ctx *ctx,
1420 struct nft_object *obj);
1421 int (*dump)(struct sk_buff *skb,
1422 struct nft_object *obj,
1424 void (*update)(struct nft_object *obj,
1425 struct nft_object *newobj);
1426 const struct nft_object_type *type;
1429 int nft_register_obj(struct nft_object_type *obj_type);
1430 void nft_unregister_obj(struct nft_object_type *obj_type);
1432 #define NFT_NETDEVICE_MAX 256
1435 * struct nft_flowtable - nf_tables flow table
1437 * @list: flow table list node in table list
1438 * @table: the table the flow table is contained in
1439 * @name: name of this flow table
1440 * @hooknum: hook number
1441 * @ops_len: number of hooks in array
1442 * @genmask: generation mask
1443 * @use: number of references to this flow table
1444 * @handle: unique object handle
1445 * @hook_list: hook list for hooks per net_device in flowtables
1446 * @data: rhashtable and garbage collector
1448 struct nft_flowtable {
1449 struct list_head list;
1450 struct nft_table *table;
1457 /* runtime data below here */
1458 struct list_head hook_list ____cacheline_aligned;
1459 struct nf_flowtable data;
1462 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1463 const struct nlattr *nla,
1466 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1467 struct nft_flowtable *flowtable,
1468 enum nft_trans_phase phase);
1470 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1471 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1474 * struct nft_traceinfo - nft tracing information and state
1476 * @trace: other struct members are initialised
1477 * @nf_trace: copy of skb->nf_trace before rule evaluation
1478 * @type: event type (enum nft_trace_types)
1479 * @skbid: hash of skb to be used as trace id
1480 * @packet_dumped: packet headers sent in a previous traceinfo message
1481 * @basechain: base chain currently processed
1483 struct nft_traceinfo {
1487 enum nft_trace_types type:8;
1489 const struct nft_base_chain *basechain;
1492 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1493 const struct nft_chain *basechain);
1495 void nft_trace_notify(const struct nft_pktinfo *pkt,
1496 const struct nft_verdict *verdict,
1497 const struct nft_rule_dp *rule,
1498 struct nft_traceinfo *info);
1500 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1501 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1503 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1504 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1506 #define MODULE_ALIAS_NFT_EXPR(name) \
1507 MODULE_ALIAS("nft-expr-" name)
1509 #define MODULE_ALIAS_NFT_OBJ(type) \
1510 MODULE_ALIAS("nft-obj-" __stringify(type))
1512 #if IS_ENABLED(CONFIG_NF_TABLES)
1515 * The gencursor defines two generations, the currently active and the
1516 * next one. Objects contain a bitmask of 2 bits specifying the generations
1517 * they're active in. A set bit means they're inactive in the generation
1518 * represented by that bit.
1520 * New objects start out as inactive in the current and active in the
1521 * next generation. When committing the ruleset the bitmask is cleared,
1522 * meaning they're active in all generations. When removing an object,
1523 * it is set inactive in the next generation. After committing the ruleset,
1524 * the objects are removed.
1526 static inline unsigned int nft_gencursor_next(const struct net *net)
1528 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1531 static inline u8 nft_genmask_next(const struct net *net)
1533 return 1 << nft_gencursor_next(net);
1536 static inline u8 nft_genmask_cur(const struct net *net)
1538 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1539 return 1 << READ_ONCE(net->nft.gencursor);
1542 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1545 * Generic transaction helpers
1548 /* Check if this object is currently active. */
1549 #define nft_is_active(__net, __obj) \
1550 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1552 /* Check if this object is active in the next generation. */
1553 #define nft_is_active_next(__net, __obj) \
1554 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1556 /* This object becomes active in the next generation. */
1557 #define nft_activate_next(__net, __obj) \
1558 (__obj)->genmask = nft_genmask_cur(__net)
1560 /* This object becomes inactive in the next generation. */
1561 #define nft_deactivate_next(__net, __obj) \
1562 (__obj)->genmask = nft_genmask_next(__net)
1564 /* After committing the ruleset, clear the stale generation bit. */
1565 #define nft_clear(__net, __obj) \
1566 (__obj)->genmask &= ~nft_genmask_next(__net)
1567 #define nft_active_genmask(__obj, __genmask) \
1568 !((__obj)->genmask & __genmask)
1571 * Set element transaction helpers
1574 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1577 return !(ext->genmask & genmask);
1580 static inline void nft_set_elem_change_active(const struct net *net,
1581 const struct nft_set *set,
1582 struct nft_set_ext *ext)
1584 ext->genmask ^= nft_genmask_next(net);
1587 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1589 #define NFT_SET_ELEM_DEAD_MASK (1 << 2)
1591 #if defined(__LITTLE_ENDIAN_BITFIELD)
1592 #define NFT_SET_ELEM_DEAD_BIT 2
1593 #elif defined(__BIG_ENDIAN_BITFIELD)
1594 #define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1599 static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1601 unsigned long *word = (unsigned long *)ext;
1603 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1604 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1607 static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1609 unsigned long *word = (unsigned long *)ext;
1611 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1612 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1616 * struct nft_trans - nf_tables object update in transaction
1618 * @list: used internally
1619 * @binding_list: list of objects with possible bindings
1620 * @msg_type: message type
1621 * @put_net: ctx->net needs to be put
1622 * @ctx: transaction context
1623 * @data: internal information related to the transaction
1626 struct list_head list;
1627 struct list_head binding_list;
1634 struct nft_trans_rule {
1635 struct nft_rule *rule;
1636 struct nft_flow_rule *flow;
1641 #define nft_trans_rule(trans) \
1642 (((struct nft_trans_rule *)trans->data)->rule)
1643 #define nft_trans_flow_rule(trans) \
1644 (((struct nft_trans_rule *)trans->data)->flow)
1645 #define nft_trans_rule_id(trans) \
1646 (((struct nft_trans_rule *)trans->data)->rule_id)
1647 #define nft_trans_rule_bound(trans) \
1648 (((struct nft_trans_rule *)trans->data)->bound)
1650 struct nft_trans_set {
1651 struct nft_set *set;
1660 #define nft_trans_set(trans) \
1661 (((struct nft_trans_set *)trans->data)->set)
1662 #define nft_trans_set_id(trans) \
1663 (((struct nft_trans_set *)trans->data)->set_id)
1664 #define nft_trans_set_bound(trans) \
1665 (((struct nft_trans_set *)trans->data)->bound)
1666 #define nft_trans_set_update(trans) \
1667 (((struct nft_trans_set *)trans->data)->update)
1668 #define nft_trans_set_timeout(trans) \
1669 (((struct nft_trans_set *)trans->data)->timeout)
1670 #define nft_trans_set_gc_int(trans) \
1671 (((struct nft_trans_set *)trans->data)->gc_int)
1672 #define nft_trans_set_size(trans) \
1673 (((struct nft_trans_set *)trans->data)->size)
1675 struct nft_trans_chain {
1676 struct nft_chain *chain;
1679 struct nft_stats __percpu *stats;
1683 struct nft_base_chain *basechain;
1684 struct list_head hook_list;
1687 #define nft_trans_chain(trans) \
1688 (((struct nft_trans_chain *)trans->data)->chain)
1689 #define nft_trans_chain_update(trans) \
1690 (((struct nft_trans_chain *)trans->data)->update)
1691 #define nft_trans_chain_name(trans) \
1692 (((struct nft_trans_chain *)trans->data)->name)
1693 #define nft_trans_chain_stats(trans) \
1694 (((struct nft_trans_chain *)trans->data)->stats)
1695 #define nft_trans_chain_policy(trans) \
1696 (((struct nft_trans_chain *)trans->data)->policy)
1697 #define nft_trans_chain_bound(trans) \
1698 (((struct nft_trans_chain *)trans->data)->bound)
1699 #define nft_trans_chain_id(trans) \
1700 (((struct nft_trans_chain *)trans->data)->chain_id)
1701 #define nft_trans_basechain(trans) \
1702 (((struct nft_trans_chain *)trans->data)->basechain)
1703 #define nft_trans_chain_hooks(trans) \
1704 (((struct nft_trans_chain *)trans->data)->hook_list)
1706 struct nft_trans_table {
1710 #define nft_trans_table_update(trans) \
1711 (((struct nft_trans_table *)trans->data)->update)
1713 struct nft_trans_elem {
1714 struct nft_set *set;
1715 struct nft_elem_priv *elem_priv;
1719 #define nft_trans_elem_set(trans) \
1720 (((struct nft_trans_elem *)trans->data)->set)
1721 #define nft_trans_elem_priv(trans) \
1722 (((struct nft_trans_elem *)trans->data)->elem_priv)
1723 #define nft_trans_elem_set_bound(trans) \
1724 (((struct nft_trans_elem *)trans->data)->bound)
1726 struct nft_trans_obj {
1727 struct nft_object *obj;
1728 struct nft_object *newobj;
1732 #define nft_trans_obj(trans) \
1733 (((struct nft_trans_obj *)trans->data)->obj)
1734 #define nft_trans_obj_newobj(trans) \
1735 (((struct nft_trans_obj *)trans->data)->newobj)
1736 #define nft_trans_obj_update(trans) \
1737 (((struct nft_trans_obj *)trans->data)->update)
1739 struct nft_trans_flowtable {
1740 struct nft_flowtable *flowtable;
1742 struct list_head hook_list;
1746 #define nft_trans_flowtable(trans) \
1747 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1748 #define nft_trans_flowtable_update(trans) \
1749 (((struct nft_trans_flowtable *)trans->data)->update)
1750 #define nft_trans_flowtable_hooks(trans) \
1751 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1752 #define nft_trans_flowtable_flags(trans) \
1753 (((struct nft_trans_flowtable *)trans->data)->flags)
1755 #define NFT_TRANS_GC_BATCHCOUNT 256
1757 struct nft_trans_gc {
1758 struct list_head list;
1760 struct nft_set *set;
1763 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT];
1764 struct rcu_head rcu;
1767 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1768 unsigned int gc_seq, gfp_t gfp);
1769 void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1771 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1772 unsigned int gc_seq, gfp_t gfp);
1773 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1775 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1776 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1778 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1780 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1781 unsigned int gc_seq);
1782 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
1784 void nft_setelem_data_deactivate(const struct net *net,
1785 const struct nft_set *set,
1786 struct nft_elem_priv *elem_priv);
1788 int __init nft_chain_filter_init(void);
1789 void nft_chain_filter_fini(void);
1791 void __init nft_chain_route_init(void);
1792 void nft_chain_route_fini(void);
1794 void nf_tables_trans_destroy_flush_work(void);
1796 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1797 __be64 nf_jiffies64_to_msecs(u64 input);
1799 #ifdef CONFIG_MODULES
1800 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1802 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1805 struct nftables_pernet {
1806 struct list_head tables;
1807 struct list_head commit_list;
1808 struct list_head binding_list;
1809 struct list_head module_list;
1810 struct list_head notify_list;
1811 struct mutex commit_mutex;
1814 unsigned int base_seq;
1815 unsigned int gc_seq;
1819 extern unsigned int nf_tables_net_id;
1821 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1823 return net_generic(net, nf_tables_net_id);
1826 static inline u64 nft_net_tstamp(const struct net *net)
1828 return nft_pernet(net)->tstamp;
1831 #define __NFT_REDUCE_READONLY 1UL
1832 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1834 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1836 return expr->ops->reduce == NFT_REDUCE_READONLY;
1839 void nft_reg_track_update(struct nft_regs_track *track,
1840 const struct nft_expr *expr, u8 dreg, u8 len);
1841 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1842 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1844 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1845 const struct nft_expr *expr, u8 dreg)
1847 return track->regs[dreg].selector &&
1848 track->regs[dreg].selector->ops == expr->ops &&
1849 track->regs[dreg].num_reg == 0;
1852 #endif /* _NET_NF_TABLES_H */