1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <linux/unaligned.h>
6 #include <linux/list.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nfnetlink.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <linux/u64_stats_sync.h>
12 #include <linux/rhashtable.h>
13 #include <net/netfilter/nf_flow_table.h>
14 #include <net/netlink.h>
15 #include <net/flow_offload.h>
16 #include <net/netns/generic.h>
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
212 * @reg_inited: bitmap of initialised registers
216 struct nft_table *table;
217 struct nft_chain *chain;
218 const struct nlattr * const *nla;
225 DECLARE_BITMAP(reg_inited, NFT_REG32_NUM);
228 enum nft_data_desc_flags {
229 NFT_DATA_DESC_SETELEM = (1 << 0),
232 struct nft_data_desc {
233 enum nft_data_types type;
239 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
240 struct nft_data_desc *desc, const struct nlattr *nla);
241 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
242 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
243 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
244 enum nft_data_types type, unsigned int len);
246 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
248 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
251 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
253 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
256 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
257 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
259 int nft_parse_register_load(const struct nft_ctx *ctx,
260 const struct nlattr *attr, u8 *sreg, u32 len);
261 int nft_parse_register_store(const struct nft_ctx *ctx,
262 const struct nlattr *attr, u8 *dreg,
263 const struct nft_data *data,
264 enum nft_data_types type, unsigned int len);
267 * struct nft_userdata - user defined data associated with an object
269 * @len: length of the data
272 * The presence of user data is indicated in an object specific fashion,
273 * so a length of zero can't occur and the value "len" indicates data
276 struct nft_userdata {
278 unsigned char data[];
281 /* placeholder structure for opaque set element backend representation. */
282 struct nft_elem_priv { };
285 * struct nft_set_elem - generic representation of set elements
288 * @key_end: closing element key
289 * @data: element data
290 * @priv: element private data and extensions
292 struct nft_set_elem {
294 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
298 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
302 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
305 struct nft_elem_priv *priv;
308 static inline void *nft_elem_priv_cast(const struct nft_elem_priv *priv)
315 * enum nft_iter_type - nftables set iterator type
317 * @NFT_ITER_UNSPEC: unspecified, to catch errors
318 * @NFT_ITER_READ: read-only iteration over set elements
319 * @NFT_ITER_UPDATE: iteration under mutex to update set element state
328 struct nft_set_iter {
330 enum nft_iter_type type:8;
334 int (*fn)(const struct nft_ctx *ctx,
336 const struct nft_set_iter *iter,
337 struct nft_elem_priv *elem_priv);
341 * struct nft_set_desc - description of set elements
347 * @objtype: object type
348 * @size: number of set elements
349 * @policy: set policy
350 * @gc_int: garbage collector interval
351 * @timeout: element timeout
352 * @field_len: length of each field in concatenation, bytes
353 * @field_count: number of concatenated fields in element
354 * @expr: set must support for expressions
356 struct nft_set_desc {
366 u8 field_len[NFT_REG32_COUNT];
372 * enum nft_set_class - performance class
374 * @NFT_SET_CLASS_O_1: constant, O(1)
375 * @NFT_SET_CLASS_O_LOG_N: logarithmic, O(log N)
376 * @NFT_SET_CLASS_O_N: linear, O(N)
380 NFT_SET_CLASS_O_LOG_N,
385 * struct nft_set_estimate - estimation of memory and performance
388 * @size: required memory
389 * @lookup: lookup performance class
390 * @space: memory class
392 struct nft_set_estimate {
394 enum nft_set_class lookup;
395 enum nft_set_class space;
398 #define NFT_EXPR_MAXATTR 16
399 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
400 ALIGN(size, __alignof__(struct nft_expr)))
403 * struct nft_expr - nf_tables expression
405 * @ops: expression ops
406 * @data: expression private data
409 const struct nft_expr_ops *ops;
411 __attribute__((aligned(__alignof__(u64))));
414 static inline void *nft_expr_priv(const struct nft_expr *expr)
416 return (void *)expr->data;
419 struct nft_expr_info;
421 int nft_expr_inner_parse(const struct nft_ctx *ctx, const struct nlattr *nla,
422 struct nft_expr_info *info);
423 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src, gfp_t gfp);
424 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
425 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
426 const struct nft_expr *expr, bool reset);
427 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
428 const struct nft_expr *expr);
433 * struct nft_set_ops - nf_tables set operations
435 * @lookup: look up an element within the set
436 * @update: update an element if exists, add it if doesn't exist
437 * @delete: delete an element
438 * @insert: insert new element into set
439 * @activate: activate new element in the next generation
440 * @deactivate: lookup for element and deactivate it in the next generation
441 * @flush: deactivate element in the next generation
442 * @remove: remove element from set
443 * @walk: iterate over all set elements
444 * @get: get set elements
445 * @ksize: kernel set size
446 * @usize: userspace set size
447 * @adjust_maxsize: delta to adjust maximum set size
448 * @commit: commit set elements
449 * @abort: abort set elements
450 * @privsize: function to return size of set private data
451 * @estimate: estimate the required memory size and the lookup complexity class
452 * @init: initialize private data of new set instance
453 * @destroy: destroy private data of set instance
454 * @gc_init: initialize garbage collection
455 * @elemsize: element private size
457 * Operations lookup, update and delete have simpler interfaces, are faster
458 * and currently only used in the packet path. All the rest are slower,
459 * control plane functions.
462 bool (*lookup)(const struct net *net,
463 const struct nft_set *set,
465 const struct nft_set_ext **ext);
466 bool (*update)(struct nft_set *set,
468 struct nft_elem_priv *
469 (*new)(struct nft_set *,
470 const struct nft_expr *,
472 const struct nft_expr *expr,
473 struct nft_regs *regs,
474 const struct nft_set_ext **ext);
475 bool (*delete)(const struct nft_set *set,
478 int (*insert)(const struct net *net,
479 const struct nft_set *set,
480 const struct nft_set_elem *elem,
481 struct nft_elem_priv **priv);
482 void (*activate)(const struct net *net,
483 const struct nft_set *set,
484 struct nft_elem_priv *elem_priv);
485 struct nft_elem_priv * (*deactivate)(const struct net *net,
486 const struct nft_set *set,
487 const struct nft_set_elem *elem);
488 void (*flush)(const struct net *net,
489 const struct nft_set *set,
490 struct nft_elem_priv *priv);
491 void (*remove)(const struct net *net,
492 const struct nft_set *set,
493 struct nft_elem_priv *elem_priv);
494 void (*walk)(const struct nft_ctx *ctx,
496 struct nft_set_iter *iter);
497 struct nft_elem_priv * (*get)(const struct net *net,
498 const struct nft_set *set,
499 const struct nft_set_elem *elem,
501 u32 (*ksize)(u32 size);
502 u32 (*usize)(u32 size);
503 u32 (*adjust_maxsize)(const struct nft_set *set);
504 void (*commit)(struct nft_set *set);
505 void (*abort)(const struct nft_set *set);
506 u64 (*privsize)(const struct nlattr * const nla[],
507 const struct nft_set_desc *desc);
508 bool (*estimate)(const struct nft_set_desc *desc,
510 struct nft_set_estimate *est);
511 int (*init)(const struct nft_set *set,
512 const struct nft_set_desc *desc,
513 const struct nlattr * const nla[]);
514 void (*destroy)(const struct nft_ctx *ctx,
515 const struct nft_set *set);
516 void (*gc_init)(const struct nft_set *set);
518 unsigned int elemsize;
522 * struct nft_set_type - nf_tables set type
524 * @ops: set ops for this type
525 * @features: features supported by the implementation
527 struct nft_set_type {
528 const struct nft_set_ops ops;
531 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
533 struct nft_set_elem_expr {
536 __attribute__((aligned(__alignof__(struct nft_expr))));
539 #define nft_setelem_expr_at(__elem_expr, __offset) \
540 ((struct nft_expr *)&__elem_expr->data[__offset])
542 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
543 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
544 __size < (__elem_expr)->size; \
545 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
547 #define NFT_SET_EXPR_MAX 2
550 * struct nft_set - nf_tables set instance
552 * @list: table set list node
553 * @bindings: list of set bindings
554 * @refs: internal refcounting for async set destruction
555 * @table: table this set belongs to
556 * @net: netnamespace this set belongs to
557 * @name: name of the set
558 * @handle: unique handle of the set
559 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
560 * @dtype: data type (verdict or numeric type defined by userspace)
561 * @objtype: object type (see NFT_OBJECT_* definitions)
562 * @size: maximum set size
563 * @field_len: length of each field in concatenation, bytes
564 * @field_count: number of concatenated fields in element
565 * @use: number of rules references to this set
566 * @nelems: number of elements
567 * @ndeact: number of deactivated elements queued for removal
568 * @timeout: default timeout value in jiffies
569 * @gc_int: garbage collection interval in msecs
570 * @policy: set parameterization (see enum nft_set_policies)
571 * @udlen: user data length
573 * @pending_update: list of pending update set element
576 * @dead: set will be freed, never cleared
577 * @genmask: generation mask
580 * @num_exprs: numbers of exprs
581 * @exprs: stateful expression
582 * @catchall_list: list of catch-all set element
583 * @data: private set data
586 struct list_head list;
587 struct list_head bindings;
589 struct nft_table *table;
597 u8 field_len[NFT_REG32_COUNT];
606 unsigned char *udata;
607 struct list_head pending_update;
608 /* runtime data below here */
609 const struct nft_set_ops *ops ____cacheline_aligned;
616 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
617 struct list_head catchall_list;
619 __attribute__((aligned(__alignof__(u64))));
622 static inline bool nft_set_is_anonymous(const struct nft_set *set)
624 return set->flags & NFT_SET_ANONYMOUS;
627 static inline void *nft_set_priv(const struct nft_set *set)
629 return (void *)set->data;
632 static inline enum nft_data_types nft_set_datatype(const struct nft_set *set)
634 return set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
637 static inline bool nft_set_gc_is_pending(const struct nft_set *s)
639 return refcount_read(&s->refs) != 1;
642 static inline struct nft_set *nft_set_container_of(const void *priv)
644 return (void *)priv - offsetof(struct nft_set, data);
647 struct nft_set *nft_set_lookup_global(const struct net *net,
648 const struct nft_table *table,
649 const struct nlattr *nla_set_name,
650 const struct nlattr *nla_set_id,
653 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
654 const struct nft_set *set);
656 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
658 u32 gc_int = READ_ONCE(set->gc_int);
660 return gc_int ? msecs_to_jiffies(gc_int) : HZ;
664 * struct nft_set_binding - nf_tables set binding
666 * @list: set bindings list node
667 * @chain: chain containing the rule bound to the set
668 * @flags: set action flags
670 * A set binding contains all information necessary for validation
671 * of new elements added to a bound set.
673 struct nft_set_binding {
674 struct list_head list;
675 const struct nft_chain *chain;
679 enum nft_trans_phase;
680 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
681 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
682 struct nft_set_binding *binding,
683 enum nft_trans_phase phase);
684 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
685 struct nft_set_binding *binding);
686 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
689 * enum nft_set_extensions - set extension type IDs
691 * @NFT_SET_EXT_KEY: element key
692 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
693 * @NFT_SET_EXT_DATA: mapping data
694 * @NFT_SET_EXT_FLAGS: element flags
695 * @NFT_SET_EXT_TIMEOUT: element timeout
696 * @NFT_SET_EXT_USERDATA: user data associated with the element
697 * @NFT_SET_EXT_EXPRESSIONS: expressions associated with the element
698 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
699 * @NFT_SET_EXT_NUM: number of extension types
701 enum nft_set_extensions {
707 NFT_SET_EXT_USERDATA,
708 NFT_SET_EXT_EXPRESSIONS,
714 * struct nft_set_ext_type - set extension type
716 * @len: fixed part length of the extension
717 * @align: alignment requirements of the extension
719 struct nft_set_ext_type {
724 extern const struct nft_set_ext_type nft_set_ext_types[];
727 * struct nft_set_ext_tmpl - set extension template
729 * @len: length of extension area
730 * @offset: offsets of individual extension types
731 * @ext_len: length of the expected extension(used to sanity check)
733 struct nft_set_ext_tmpl {
735 u8 offset[NFT_SET_EXT_NUM];
736 u8 ext_len[NFT_SET_EXT_NUM];
740 * struct nft_set_ext - set extensions
742 * @genmask: generation mask, but also flags (see NFT_SET_ELEM_DEAD_BIT)
743 * @offset: offsets of individual extension types
744 * @data: beginning of extension data
746 * This structure must be aligned to word size, otherwise atomic bitops
747 * on genmask field can cause alignment failure on some archs.
751 u8 offset[NFT_SET_EXT_NUM];
753 } __aligned(BITS_PER_LONG / 8);
755 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
757 memset(tmpl, 0, sizeof(*tmpl));
758 tmpl->len = sizeof(struct nft_set_ext);
761 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
764 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
765 if (tmpl->len > U8_MAX)
768 tmpl->offset[id] = tmpl->len;
769 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
770 tmpl->len += tmpl->ext_len[id];
775 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
777 return nft_set_ext_add_length(tmpl, id, 0);
780 static inline void nft_set_ext_init(struct nft_set_ext *ext,
781 const struct nft_set_ext_tmpl *tmpl)
783 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
786 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
788 return !!ext->offset[id];
791 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
793 return ext && __nft_set_ext_exists(ext, id);
796 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
798 return (void *)ext + ext->offset[id];
801 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
803 return nft_set_ext(ext, NFT_SET_EXT_KEY);
806 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
808 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
811 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
813 return nft_set_ext(ext, NFT_SET_EXT_DATA);
816 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
818 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
826 static inline struct nft_timeout *nft_set_ext_timeout(const struct nft_set_ext *ext)
828 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
831 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
833 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
836 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
838 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
841 static inline bool __nft_set_elem_expired(const struct nft_set_ext *ext,
844 if (!nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) ||
845 READ_ONCE(nft_set_ext_timeout(ext)->timeout) == 0)
848 return time_after_eq64(tstamp, READ_ONCE(nft_set_ext_timeout(ext)->expiration));
851 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
853 return __nft_set_elem_expired(ext, get_jiffies_64());
856 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
857 const struct nft_elem_priv *elem_priv)
859 return (void *)elem_priv + set->ops->elemsize;
862 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
864 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
867 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
868 const struct nft_set *set,
869 const struct nlattr *attr);
871 struct nft_elem_priv *nft_set_elem_init(const struct nft_set *set,
872 const struct nft_set_ext_tmpl *tmpl,
873 const u32 *key, const u32 *key_end,
875 u64 timeout, u64 expiration, gfp_t gfp);
876 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
877 struct nft_expr *expr_array[]);
878 void nft_set_elem_destroy(const struct nft_set *set,
879 const struct nft_elem_priv *elem_priv,
881 void nf_tables_set_elem_destroy(const struct nft_ctx *ctx,
882 const struct nft_set *set,
883 const struct nft_elem_priv *elem_priv);
887 * struct nft_expr_type - nf_tables expression type
889 * @select_ops: function to select nft_expr_ops
890 * @release_ops: release nft_expr_ops
891 * @ops: default ops, used when no select_ops functions is present
892 * @inner_ops: inner ops, used for inner packet operation
893 * @list: used internally
895 * @owner: module reference
896 * @policy: netlink attribute policy
897 * @maxattr: highest netlink attribute number
898 * @family: address family for AF-specific types
899 * @flags: expression type flags
901 struct nft_expr_type {
902 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
903 const struct nlattr * const tb[]);
904 void (*release_ops)(const struct nft_expr_ops *ops);
905 const struct nft_expr_ops *ops;
906 const struct nft_expr_ops *inner_ops;
907 struct list_head list;
909 struct module *owner;
910 const struct nla_policy *policy;
911 unsigned int maxattr;
916 #define NFT_EXPR_STATEFUL 0x1
917 #define NFT_EXPR_GC 0x2
919 enum nft_trans_phase {
921 NFT_TRANS_PREPARE_ERROR,
927 struct nft_flow_rule;
928 struct nft_offload_ctx;
931 * struct nft_expr_ops - nf_tables expression operations
933 * @eval: Expression evaluation function
934 * @clone: Expression clone function
935 * @size: full expression size, including private data size
936 * @init: initialization function
937 * @activate: activate expression in the next generation
938 * @deactivate: deactivate expression in next generation
939 * @destroy: destruction function, called after synchronize_rcu
940 * @destroy_clone: destruction clone function
941 * @dump: function to dump parameters
942 * @validate: validate expression, called during loop detection
943 * @reduce: reduce expression
944 * @gc: garbage collection expression
945 * @offload: hardware offload expression
946 * @offload_action: function to report true/false to allocate one slot or not in the flow
948 * @offload_stats: function to synchronize hardware stats via updating the counter expression
949 * @type: expression type
950 * @data: extra data to attach to this expression operation
952 struct nft_expr_ops {
953 void (*eval)(const struct nft_expr *expr,
954 struct nft_regs *regs,
955 const struct nft_pktinfo *pkt);
956 int (*clone)(struct nft_expr *dst,
957 const struct nft_expr *src, gfp_t gfp);
960 int (*init)(const struct nft_ctx *ctx,
961 const struct nft_expr *expr,
962 const struct nlattr * const tb[]);
963 void (*activate)(const struct nft_ctx *ctx,
964 const struct nft_expr *expr);
965 void (*deactivate)(const struct nft_ctx *ctx,
966 const struct nft_expr *expr,
967 enum nft_trans_phase phase);
968 void (*destroy)(const struct nft_ctx *ctx,
969 const struct nft_expr *expr);
970 void (*destroy_clone)(const struct nft_ctx *ctx,
971 const struct nft_expr *expr);
972 int (*dump)(struct sk_buff *skb,
973 const struct nft_expr *expr,
975 int (*validate)(const struct nft_ctx *ctx,
976 const struct nft_expr *expr);
977 bool (*reduce)(struct nft_regs_track *track,
978 const struct nft_expr *expr);
979 bool (*gc)(struct net *net,
980 const struct nft_expr *expr);
981 int (*offload)(struct nft_offload_ctx *ctx,
982 struct nft_flow_rule *flow,
983 const struct nft_expr *expr);
984 bool (*offload_action)(const struct nft_expr *expr);
985 void (*offload_stats)(struct nft_expr *expr,
986 const struct flow_stats *stats);
987 const struct nft_expr_type *type;
992 * struct nft_rule - nf_tables rule
994 * @list: used internally
995 * @handle: rule handle
996 * @genmask: generation mask
997 * @dlen: length of expression data
998 * @udata: user data is appended to the rule
999 * @data: expression data
1002 struct list_head list;
1007 unsigned char data[]
1008 __attribute__((aligned(__alignof__(struct nft_expr))));
1011 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
1013 return (struct nft_expr *)&rule->data[0];
1016 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
1018 return ((void *)expr) + expr->ops->size;
1021 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
1023 return (struct nft_expr *)&rule->data[rule->dlen];
1026 static inline bool nft_expr_more(const struct nft_rule *rule,
1027 const struct nft_expr *expr)
1029 return expr != nft_expr_last(rule) && expr->ops;
1032 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
1034 return (void *)&rule->data[rule->dlen];
1037 void nft_rule_expr_activate(const struct nft_ctx *ctx, struct nft_rule *rule);
1038 void nft_rule_expr_deactivate(const struct nft_ctx *ctx, struct nft_rule *rule,
1039 enum nft_trans_phase phase);
1040 void nf_tables_rule_destroy(const struct nft_ctx *ctx, struct nft_rule *rule);
1042 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
1043 struct nft_regs *regs,
1044 const struct nft_pktinfo *pkt)
1046 struct nft_set_elem_expr *elem_expr;
1047 struct nft_expr *expr;
1050 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
1051 elem_expr = nft_set_ext_expr(ext);
1052 nft_setelem_expr_foreach(expr, elem_expr, size) {
1053 expr->ops->eval(expr, regs, pkt);
1054 if (regs->verdict.code == NFT_BREAK)
1061 * The last pointer isn't really necessary, but the compiler isn't able to
1062 * determine that the result of nft_expr_last() is always the same since it
1063 * can't assume that the dlen value wasn't changed within calls in the loop.
1065 #define nft_rule_for_each_expr(expr, last, rule) \
1066 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1068 (expr) = nft_expr_next(expr))
1070 #define NFT_CHAIN_POLICY_UNSET U8_MAX
1072 struct nft_rule_dp {
1075 handle:42; /* for tracing */
1076 unsigned char data[]
1077 __attribute__((aligned(__alignof__(struct nft_expr))));
1080 struct nft_rule_dp_last {
1081 struct nft_rule_dp end; /* end of nft_rule_blob marker */
1082 struct rcu_head h; /* call_rcu head */
1083 struct nft_rule_blob *blob; /* ptr to free via call_rcu */
1084 const struct nft_chain *chain; /* for nftables tracing */
1087 static inline const struct nft_rule_dp *nft_rule_next(const struct nft_rule_dp *rule)
1089 return (void *)rule + sizeof(*rule) + rule->dlen;
1092 struct nft_rule_blob {
1094 unsigned char data[]
1095 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1099 * struct nft_chain - nf_tables chain
1101 * @blob_gen_0: rule blob pointer to the current generation
1102 * @blob_gen_1: rule blob pointer to the future generation
1103 * @rules: list of rules in the chain
1104 * @list: used internally
1105 * @rhlhead: used internally
1106 * @table: table that this chain belongs to
1107 * @handle: chain handle
1108 * @use: number of jump references to this chain
1109 * @flags: bitmask of enum NFTA_CHAIN_FLAGS
1110 * @bound: bind or not
1111 * @genmask: generation mask
1112 * @name: name of the chain
1113 * @udlen: user data length
1114 * @udata: user data in the chain
1115 * @blob_next: rule blob pointer to the next in the chain
1118 struct nft_rule_blob __rcu *blob_gen_0;
1119 struct nft_rule_blob __rcu *blob_gen_1;
1120 struct list_head rules;
1121 struct list_head list;
1122 struct rhlist_head rhlhead;
1123 struct nft_table *table;
1133 /* Only used during control plane commit phase: */
1134 struct nft_rule_blob *blob_next;
1137 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1138 int nft_setelem_validate(const struct nft_ctx *ctx, struct nft_set *set,
1139 const struct nft_set_iter *iter,
1140 struct nft_elem_priv *elem_priv);
1141 int nft_set_catchall_validate(const struct nft_ctx *ctx, struct nft_set *set);
1142 int nf_tables_bind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1143 void nf_tables_unbind_chain(const struct nft_ctx *ctx, struct nft_chain *chain);
1146 void nf_tables_chain_device_notify(const struct nft_chain *chain,
1147 const struct nft_hook *hook,
1148 const struct net_device *dev, int event);
1150 enum nft_chain_types {
1151 NFT_CHAIN_T_DEFAULT = 0,
1158 * struct nft_chain_type - nf_tables chain type info
1160 * @name: name of the type
1161 * @type: numeric identifier
1162 * @family: address family
1163 * @owner: module owner
1164 * @hook_mask: mask of valid hooks
1165 * @hooks: array of hook functions
1166 * @ops_register: base chain register function
1167 * @ops_unregister: base chain unregister function
1169 struct nft_chain_type {
1171 enum nft_chain_types type;
1173 struct module *owner;
1174 unsigned int hook_mask;
1175 nf_hookfn *hooks[NFT_MAX_HOOKS];
1176 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1177 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1180 int nft_chain_validate_dependency(const struct nft_chain *chain,
1181 enum nft_chain_types type);
1182 int nft_chain_validate_hooks(const struct nft_chain *chain,
1183 unsigned int hook_flags);
1185 static inline bool nft_chain_binding(const struct nft_chain *chain)
1187 return chain->flags & NFT_CHAIN_BINDING;
1190 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1192 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1195 int nft_chain_add(struct nft_table *table, struct nft_chain *chain);
1196 void nft_chain_del(struct nft_chain *chain);
1197 void nf_tables_chain_destroy(struct nft_chain *chain);
1202 struct u64_stats_sync syncp;
1206 struct list_head list;
1207 struct list_head ops_list;
1208 struct rcu_head rcu;
1209 char ifname[IFNAMSIZ];
1213 struct nf_hook_ops *nft_hook_find_ops(const struct nft_hook *hook,
1214 const struct net_device *dev);
1215 struct nf_hook_ops *nft_hook_find_ops_rcu(const struct nft_hook *hook,
1216 const struct net_device *dev);
1219 * struct nft_base_chain - nf_tables base chain
1221 * @ops: netfilter hook ops
1222 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1224 * @policy: default policy
1225 * @flags: indicate the base chain disabled or not
1226 * @stats: per-cpu chain stats
1228 * @flow_block: flow block (for hardware offload)
1230 struct nft_base_chain {
1231 struct nf_hook_ops ops;
1232 struct list_head hook_list;
1233 const struct nft_chain_type *type;
1236 struct nft_stats __percpu *stats;
1237 struct nft_chain chain;
1238 struct flow_block flow_block;
1241 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1243 return container_of(chain, struct nft_base_chain, chain);
1246 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1248 return chain->flags & NFT_CHAIN_BASE;
1251 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1253 static inline bool nft_use_inc(u32 *use)
1255 if (*use == UINT_MAX)
1263 static inline void nft_use_dec(u32 *use)
1265 WARN_ON_ONCE((*use)-- == 0);
1268 /* For error and abort path: restore use counter to previous state. */
1269 static inline void nft_use_inc_restore(u32 *use)
1271 WARN_ON_ONCE(!nft_use_inc(use));
1274 #define nft_use_dec_restore nft_use_dec
1277 * struct nft_table - nf_tables table
1279 * @list: used internally
1280 * @chains_ht: chains in the table
1281 * @chains: same, for stable walks
1282 * @sets: sets in the table
1283 * @objects: stateful objects in the table
1284 * @flowtables: flow tables in the table
1285 * @hgenerator: handle generator state
1286 * @handle: table handle
1287 * @use: number of chain references to this table
1288 * @family:address family
1289 * @flags: table flag (see enum nft_table_flags)
1290 * @genmask: generation mask
1291 * @nlpid: netlink port ID
1292 * @name: name of the table
1293 * @udlen: length of the user data
1295 * @validate_state: internal, set when transaction adds jumps
1298 struct list_head list;
1299 struct rhltable chains_ht;
1300 struct list_head chains;
1301 struct list_head sets;
1302 struct list_head objects;
1303 struct list_head flowtables;
1317 static inline bool nft_table_has_owner(const struct nft_table *table)
1319 return table->flags & NFT_TABLE_F_OWNER;
1322 static inline bool nft_table_is_orphan(const struct nft_table *table)
1324 return (table->flags & (NFT_TABLE_F_OWNER | NFT_TABLE_F_PERSIST)) ==
1325 NFT_TABLE_F_PERSIST;
1328 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1330 return family == NFPROTO_NETDEV ||
1331 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1334 void nft_register_chain_type(const struct nft_chain_type *);
1335 void nft_unregister_chain_type(const struct nft_chain_type *);
1337 int nft_register_expr(struct nft_expr_type *);
1338 void nft_unregister_expr(struct nft_expr_type *);
1340 int nft_verdict_dump(struct sk_buff *skb, int type,
1341 const struct nft_verdict *v);
1344 * struct nft_object_hash_key - key to lookup nft_object
1346 * @name: name of the stateful object to look up
1347 * @table: table the object belongs to
1349 struct nft_object_hash_key {
1351 const struct nft_table *table;
1355 * struct nft_object - nf_tables stateful object
1357 * @list: table stateful object list node
1358 * @rhlhead: nft_objname_ht node
1359 * @key: keys that identify this object
1360 * @genmask: generation mask
1361 * @use: number of references to this stateful object
1362 * @handle: unique object handle
1363 * @udlen: length of user data
1365 * @ops: object operations
1366 * @data: object data, layout depends on type
1369 struct list_head list;
1370 struct rhlist_head rhlhead;
1371 struct nft_object_hash_key key;
1377 /* runtime data below here */
1378 const struct nft_object_ops *ops ____cacheline_aligned;
1379 unsigned char data[]
1380 __attribute__((aligned(__alignof__(u64))));
1383 static inline void *nft_obj_data(const struct nft_object *obj)
1385 return (void *)obj->data;
1388 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1390 struct nft_object *nft_obj_lookup(const struct net *net,
1391 const struct nft_table *table,
1392 const struct nlattr *nla, u32 objtype,
1395 void nft_obj_notify(struct net *net, const struct nft_table *table,
1396 struct nft_object *obj, u32 portid, u32 seq,
1397 int event, u16 flags, int family, int report, gfp_t gfp);
1400 * struct nft_object_type - stateful object type
1402 * @select_ops: function to select nft_object_ops
1403 * @ops: default ops, used when no select_ops functions is present
1404 * @list: list node in list of object types
1405 * @type: stateful object numeric type
1406 * @owner: module owner
1407 * @maxattr: maximum netlink attribute
1408 * @family: address family for AF-specific object types
1409 * @policy: netlink attribute policy
1411 struct nft_object_type {
1412 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1413 const struct nlattr * const tb[]);
1414 const struct nft_object_ops *ops;
1415 struct list_head list;
1417 unsigned int maxattr;
1419 struct module *owner;
1420 const struct nla_policy *policy;
1424 * struct nft_object_ops - stateful object operations
1426 * @eval: stateful object evaluation function
1427 * @size: stateful object size
1428 * @init: initialize object from netlink attributes
1429 * @destroy: release existing stateful object
1430 * @dump: netlink dump stateful object
1431 * @update: update stateful object
1432 * @type: pointer to object type
1434 struct nft_object_ops {
1435 void (*eval)(struct nft_object *obj,
1436 struct nft_regs *regs,
1437 const struct nft_pktinfo *pkt);
1439 int (*init)(const struct nft_ctx *ctx,
1440 const struct nlattr *const tb[],
1441 struct nft_object *obj);
1442 void (*destroy)(const struct nft_ctx *ctx,
1443 struct nft_object *obj);
1444 int (*dump)(struct sk_buff *skb,
1445 struct nft_object *obj,
1447 void (*update)(struct nft_object *obj,
1448 struct nft_object *newobj);
1449 const struct nft_object_type *type;
1452 int nft_register_obj(struct nft_object_type *obj_type);
1453 void nft_unregister_obj(struct nft_object_type *obj_type);
1455 #define NFT_NETDEVICE_MAX 256
1458 * struct nft_flowtable - nf_tables flow table
1460 * @list: flow table list node in table list
1461 * @table: the table the flow table is contained in
1462 * @name: name of this flow table
1463 * @hooknum: hook number
1464 * @ops_len: number of hooks in array
1465 * @genmask: generation mask
1466 * @use: number of references to this flow table
1467 * @handle: unique object handle
1468 * @hook_list: hook list for hooks per net_device in flowtables
1469 * @data: rhashtable and garbage collector
1471 struct nft_flowtable {
1472 struct list_head list;
1473 struct nft_table *table;
1480 /* runtime data below here */
1481 struct list_head hook_list ____cacheline_aligned;
1482 struct nf_flowtable data;
1485 struct nft_flowtable *nft_flowtable_lookup(const struct net *net,
1486 const struct nft_table *table,
1487 const struct nlattr *nla,
1490 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1491 struct nft_flowtable *flowtable,
1492 enum nft_trans_phase phase);
1494 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1495 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1498 * struct nft_traceinfo - nft tracing information and state
1500 * @trace: other struct members are initialised
1501 * @nf_trace: copy of skb->nf_trace before rule evaluation
1502 * @type: event type (enum nft_trace_types)
1503 * @skbid: hash of skb to be used as trace id
1504 * @packet_dumped: packet headers sent in a previous traceinfo message
1505 * @basechain: base chain currently processed
1507 struct nft_traceinfo {
1511 enum nft_trace_types type:8;
1513 const struct nft_base_chain *basechain;
1516 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1517 const struct nft_chain *basechain);
1519 void nft_trace_notify(const struct nft_pktinfo *pkt,
1520 const struct nft_verdict *verdict,
1521 const struct nft_rule_dp *rule,
1522 struct nft_traceinfo *info);
1524 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1525 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1527 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1528 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1530 #define MODULE_ALIAS_NFT_EXPR(name) \
1531 MODULE_ALIAS("nft-expr-" name)
1533 #define MODULE_ALIAS_NFT_OBJ(type) \
1534 MODULE_ALIAS("nft-obj-" __stringify(type))
1536 #if IS_ENABLED(CONFIG_NF_TABLES)
1539 * The gencursor defines two generations, the currently active and the
1540 * next one. Objects contain a bitmask of 2 bits specifying the generations
1541 * they're active in. A set bit means they're inactive in the generation
1542 * represented by that bit.
1544 * New objects start out as inactive in the current and active in the
1545 * next generation. When committing the ruleset the bitmask is cleared,
1546 * meaning they're active in all generations. When removing an object,
1547 * it is set inactive in the next generation. After committing the ruleset,
1548 * the objects are removed.
1550 static inline unsigned int nft_gencursor_next(const struct net *net)
1552 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1555 static inline u8 nft_genmask_next(const struct net *net)
1557 return 1 << nft_gencursor_next(net);
1560 static inline u8 nft_genmask_cur(const struct net *net)
1562 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1563 return 1 << READ_ONCE(net->nft.gencursor);
1566 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1569 * Generic transaction helpers
1572 /* Check if this object is currently active. */
1573 #define nft_is_active(__net, __obj) \
1574 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1576 /* Check if this object is active in the next generation. */
1577 #define nft_is_active_next(__net, __obj) \
1578 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1580 /* This object becomes active in the next generation. */
1581 #define nft_activate_next(__net, __obj) \
1582 (__obj)->genmask = nft_genmask_cur(__net)
1584 /* This object becomes inactive in the next generation. */
1585 #define nft_deactivate_next(__net, __obj) \
1586 (__obj)->genmask = nft_genmask_next(__net)
1588 /* After committing the ruleset, clear the stale generation bit. */
1589 #define nft_clear(__net, __obj) \
1590 (__obj)->genmask &= ~nft_genmask_next(__net)
1591 #define nft_active_genmask(__obj, __genmask) \
1592 !((__obj)->genmask & __genmask)
1595 * Set element transaction helpers
1598 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1601 return !(ext->genmask & genmask);
1604 static inline void nft_set_elem_change_active(const struct net *net,
1605 const struct nft_set *set,
1606 struct nft_set_ext *ext)
1608 ext->genmask ^= nft_genmask_next(net);
1611 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1613 #define NFT_SET_ELEM_DEAD_MASK (1 << 2)
1615 #if defined(__LITTLE_ENDIAN_BITFIELD)
1616 #define NFT_SET_ELEM_DEAD_BIT 2
1617 #elif defined(__BIG_ENDIAN_BITFIELD)
1618 #define NFT_SET_ELEM_DEAD_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1623 static inline void nft_set_elem_dead(struct nft_set_ext *ext)
1625 unsigned long *word = (unsigned long *)ext;
1627 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1628 set_bit(NFT_SET_ELEM_DEAD_BIT, word);
1631 static inline int nft_set_elem_is_dead(const struct nft_set_ext *ext)
1633 unsigned long *word = (unsigned long *)ext;
1635 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1636 return test_bit(NFT_SET_ELEM_DEAD_BIT, word);
1640 * struct nft_trans - nf_tables object update in transaction
1642 * @list: used internally
1644 * @table: struct nft_table the object resides in
1645 * @msg_type: message type
1646 * @seq: netlink sequence number
1647 * @flags: modifiers to new request
1648 * @report: notify via unicast netlink message
1649 * @put_net: net needs to be put
1651 * This is the information common to all objects in the transaction,
1652 * this must always be the first member of derived sub-types.
1655 struct list_head list;
1657 struct nft_table *table;
1666 * struct nft_trans_binding - nf_tables object with binding support in transaction
1667 * @nft_trans: base structure, MUST be first member
1668 * @binding_list: list of objects with possible bindings
1670 * This is the base type used by objects that can be bound to a chain.
1672 struct nft_trans_binding {
1673 struct nft_trans nft_trans;
1674 struct list_head binding_list;
1677 struct nft_trans_rule {
1678 struct nft_trans nft_trans;
1679 struct nft_rule *rule;
1680 struct nft_chain *chain;
1681 struct nft_flow_rule *flow;
1686 #define nft_trans_container_rule(trans) \
1687 container_of(trans, struct nft_trans_rule, nft_trans)
1688 #define nft_trans_rule(trans) \
1689 nft_trans_container_rule(trans)->rule
1690 #define nft_trans_flow_rule(trans) \
1691 nft_trans_container_rule(trans)->flow
1692 #define nft_trans_rule_id(trans) \
1693 nft_trans_container_rule(trans)->rule_id
1694 #define nft_trans_rule_bound(trans) \
1695 nft_trans_container_rule(trans)->bound
1696 #define nft_trans_rule_chain(trans) \
1697 nft_trans_container_rule(trans)->chain
1699 struct nft_trans_set {
1700 struct nft_trans_binding nft_trans_binding;
1701 struct list_head list_trans_newset;
1702 struct nft_set *set;
1711 #define nft_trans_container_set(t) \
1712 container_of(t, struct nft_trans_set, nft_trans_binding.nft_trans)
1713 #define nft_trans_set(trans) \
1714 nft_trans_container_set(trans)->set
1715 #define nft_trans_set_id(trans) \
1716 nft_trans_container_set(trans)->set_id
1717 #define nft_trans_set_bound(trans) \
1718 nft_trans_container_set(trans)->bound
1719 #define nft_trans_set_update(trans) \
1720 nft_trans_container_set(trans)->update
1721 #define nft_trans_set_timeout(trans) \
1722 nft_trans_container_set(trans)->timeout
1723 #define nft_trans_set_gc_int(trans) \
1724 nft_trans_container_set(trans)->gc_int
1725 #define nft_trans_set_size(trans) \
1726 nft_trans_container_set(trans)->size
1728 struct nft_trans_chain {
1729 struct nft_trans_binding nft_trans_binding;
1730 struct nft_chain *chain;
1732 struct nft_stats __percpu *stats;
1737 struct nft_base_chain *basechain;
1738 struct list_head hook_list;
1741 #define nft_trans_container_chain(t) \
1742 container_of(t, struct nft_trans_chain, nft_trans_binding.nft_trans)
1743 #define nft_trans_chain(trans) \
1744 nft_trans_container_chain(trans)->chain
1745 #define nft_trans_chain_update(trans) \
1746 nft_trans_container_chain(trans)->update
1747 #define nft_trans_chain_name(trans) \
1748 nft_trans_container_chain(trans)->name
1749 #define nft_trans_chain_stats(trans) \
1750 nft_trans_container_chain(trans)->stats
1751 #define nft_trans_chain_policy(trans) \
1752 nft_trans_container_chain(trans)->policy
1753 #define nft_trans_chain_bound(trans) \
1754 nft_trans_container_chain(trans)->bound
1755 #define nft_trans_chain_id(trans) \
1756 nft_trans_container_chain(trans)->chain_id
1757 #define nft_trans_basechain(trans) \
1758 nft_trans_container_chain(trans)->basechain
1759 #define nft_trans_chain_hooks(trans) \
1760 nft_trans_container_chain(trans)->hook_list
1762 struct nft_trans_table {
1763 struct nft_trans nft_trans;
1767 #define nft_trans_container_table(trans) \
1768 container_of(trans, struct nft_trans_table, nft_trans)
1769 #define nft_trans_table_update(trans) \
1770 nft_trans_container_table(trans)->update
1772 enum nft_trans_elem_flags {
1773 NFT_TRANS_UPD_TIMEOUT = (1 << 0),
1774 NFT_TRANS_UPD_EXPIRATION = (1 << 1),
1777 struct nft_elem_update {
1783 struct nft_trans_one_elem {
1784 struct nft_elem_priv *priv;
1785 struct nft_elem_update *update;
1788 struct nft_trans_elem {
1789 struct nft_trans nft_trans;
1790 struct nft_set *set;
1792 unsigned int nelems;
1793 struct nft_trans_one_elem elems[] __counted_by(nelems);
1796 #define nft_trans_container_elem(t) \
1797 container_of(t, struct nft_trans_elem, nft_trans)
1798 #define nft_trans_elem_set(trans) \
1799 nft_trans_container_elem(trans)->set
1800 #define nft_trans_elem_set_bound(trans) \
1801 nft_trans_container_elem(trans)->bound
1803 struct nft_trans_obj {
1804 struct nft_trans nft_trans;
1805 struct nft_object *obj;
1806 struct nft_object *newobj;
1810 #define nft_trans_container_obj(t) \
1811 container_of(t, struct nft_trans_obj, nft_trans)
1812 #define nft_trans_obj(trans) \
1813 nft_trans_container_obj(trans)->obj
1814 #define nft_trans_obj_newobj(trans) \
1815 nft_trans_container_obj(trans)->newobj
1816 #define nft_trans_obj_update(trans) \
1817 nft_trans_container_obj(trans)->update
1819 struct nft_trans_flowtable {
1820 struct nft_trans nft_trans;
1821 struct nft_flowtable *flowtable;
1822 struct list_head hook_list;
1827 #define nft_trans_container_flowtable(t) \
1828 container_of(t, struct nft_trans_flowtable, nft_trans)
1829 #define nft_trans_flowtable(trans) \
1830 nft_trans_container_flowtable(trans)->flowtable
1831 #define nft_trans_flowtable_update(trans) \
1832 nft_trans_container_flowtable(trans)->update
1833 #define nft_trans_flowtable_hooks(trans) \
1834 nft_trans_container_flowtable(trans)->hook_list
1835 #define nft_trans_flowtable_flags(trans) \
1836 nft_trans_container_flowtable(trans)->flags
1838 #define NFT_TRANS_GC_BATCHCOUNT 256
1840 struct nft_trans_gc {
1841 struct list_head list;
1843 struct nft_set *set;
1846 struct nft_elem_priv *priv[NFT_TRANS_GC_BATCHCOUNT];
1847 struct rcu_head rcu;
1850 static inline void nft_ctx_update(struct nft_ctx *ctx,
1851 const struct nft_trans *trans)
1853 switch (trans->msg_type) {
1854 case NFT_MSG_NEWRULE:
1855 case NFT_MSG_DELRULE:
1856 case NFT_MSG_DESTROYRULE:
1857 ctx->chain = nft_trans_rule_chain(trans);
1859 case NFT_MSG_NEWCHAIN:
1860 case NFT_MSG_DELCHAIN:
1861 case NFT_MSG_DESTROYCHAIN:
1862 ctx->chain = nft_trans_chain(trans);
1869 ctx->net = trans->net;
1870 ctx->table = trans->table;
1871 ctx->family = trans->table->family;
1872 ctx->report = trans->report;
1873 ctx->flags = trans->flags;
1874 ctx->seq = trans->seq;
1877 struct nft_trans_gc *nft_trans_gc_alloc(struct nft_set *set,
1878 unsigned int gc_seq, gfp_t gfp);
1879 void nft_trans_gc_destroy(struct nft_trans_gc *trans);
1881 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
1882 unsigned int gc_seq, gfp_t gfp);
1883 void nft_trans_gc_queue_async_done(struct nft_trans_gc *gc);
1885 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp);
1886 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans);
1888 void nft_trans_gc_elem_add(struct nft_trans_gc *gc, void *priv);
1890 struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
1891 unsigned int gc_seq);
1892 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
1894 void nft_setelem_data_deactivate(const struct net *net,
1895 const struct nft_set *set,
1896 struct nft_elem_priv *elem_priv);
1898 int __init nft_chain_filter_init(void);
1899 void nft_chain_filter_fini(void);
1901 void __init nft_chain_route_init(void);
1902 void nft_chain_route_fini(void);
1904 void nf_tables_trans_destroy_flush_work(struct net *net);
1906 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1907 __be64 nf_jiffies64_to_msecs(u64 input);
1909 #ifdef CONFIG_MODULES
1910 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1912 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1915 struct nftables_pernet {
1916 struct list_head tables;
1917 struct list_head commit_list;
1918 struct list_head destroy_list;
1919 struct list_head commit_set_list;
1920 struct list_head binding_list;
1921 struct list_head module_list;
1922 struct list_head notify_list;
1923 struct mutex commit_mutex;
1926 unsigned int base_seq;
1927 unsigned int gc_seq;
1929 struct work_struct destroy_work;
1932 extern unsigned int nf_tables_net_id;
1934 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1936 return net_generic(net, nf_tables_net_id);
1939 static inline u64 nft_net_tstamp(const struct net *net)
1941 return nft_pernet(net)->tstamp;
1944 #define __NFT_REDUCE_READONLY 1UL
1945 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1947 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1949 return expr->ops->reduce == NFT_REDUCE_READONLY;
1952 void nft_reg_track_update(struct nft_regs_track *track,
1953 const struct nft_expr *expr, u8 dreg, u8 len);
1954 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1955 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1957 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1958 const struct nft_expr *expr, u8 dreg)
1960 return track->regs[dreg].selector &&
1961 track->regs[dreg].selector->ops == expr->ops &&
1962 track->regs[dreg].num_reg == 0;
1965 #endif /* _NET_NF_TABLES_H */