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),
31 const struct nf_hook_state *state;
36 unsigned int inneroff;
39 static inline struct sock *nft_sk(const struct nft_pktinfo *pkt)
41 return pkt->state->sk;
44 static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt)
49 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
51 return pkt->state->net;
54 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
56 return pkt->state->hook;
59 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
61 return pkt->state->pf;
64 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
66 return pkt->state->in;
69 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
71 return pkt->state->out;
74 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
76 const struct nf_hook_state *state)
82 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt)
91 * struct nft_verdict - nf_tables verdict
93 * @code: nf_tables/netfilter verdict code
94 * @chain: destination chain for NFT_JUMP/NFT_GOTO
98 struct nft_chain *chain;
104 struct nft_verdict verdict;
106 } __attribute__((aligned(__alignof__(u64))));
108 #define NFT_REG32_NUM 20
111 * struct nft_regs - nf_tables register set
113 * @data: data registers
114 * @verdict: verdict register
116 * The first four data registers alias to the verdict register.
120 u32 data[NFT_REG32_NUM];
121 struct nft_verdict verdict;
125 struct nft_regs_track {
127 const struct nft_expr *selector;
128 const struct nft_expr *bitwise;
130 } regs[NFT_REG32_NUM];
132 const struct nft_expr *cur;
133 const struct nft_expr *last;
136 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
138 * Note, when using concatenations, register allocation happens at 32-bit
139 * level. So for store instruction, pad the rest part with zero to avoid
143 static inline void nft_reg_store8(u32 *dreg, u8 val)
149 static inline u8 nft_reg_load8(const u32 *sreg)
154 static inline void nft_reg_store16(u32 *dreg, u16 val)
160 static inline void nft_reg_store_be16(u32 *dreg, __be16 val)
162 nft_reg_store16(dreg, (__force __u16)val);
165 static inline u16 nft_reg_load16(const u32 *sreg)
170 static inline __be16 nft_reg_load_be16(const u32 *sreg)
172 return (__force __be16)nft_reg_load16(sreg);
175 static inline __be32 nft_reg_load_be32(const u32 *sreg)
177 return *(__force __be32 *)sreg;
180 static inline void nft_reg_store64(u32 *dreg, u64 val)
182 put_unaligned(val, (u64 *)dreg);
185 static inline u64 nft_reg_load64(const u32 *sreg)
187 return get_unaligned((u64 *)sreg);
190 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
193 if (len % NFT_REG32_SIZE)
194 dst[len / NFT_REG32_SIZE] = 0;
195 memcpy(dst, src, len);
199 * struct nft_ctx - nf_tables rule/set context
201 * @net: net namespace
202 * @table: the table the chain is contained in
203 * @chain: the chain the rule is contained in
204 * @nla: netlink attributes
205 * @portid: netlink portID of the original message
206 * @seq: netlink sequence number
207 * @family: protocol family
208 * @level: depth of the chains
209 * @report: notify via unicast netlink message
213 struct nft_table *table;
214 struct nft_chain *chain;
215 const struct nlattr * const *nla;
224 enum nft_data_desc_flags {
225 NFT_DATA_DESC_SETELEM = (1 << 0),
228 struct nft_data_desc {
229 enum nft_data_types type;
235 int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
236 struct nft_data_desc *desc, const struct nlattr *nla);
237 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
238 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
239 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
240 enum nft_data_types type, unsigned int len);
242 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
244 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
247 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
249 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
252 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
253 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
255 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
256 int nft_parse_register_store(const struct nft_ctx *ctx,
257 const struct nlattr *attr, u8 *dreg,
258 const struct nft_data *data,
259 enum nft_data_types type, unsigned int len);
262 * struct nft_userdata - user defined data associated with an object
264 * @len: length of the data
267 * The presence of user data is indicated in an object specific fashion,
268 * so a length of zero can't occur and the value "len" indicates data
271 struct nft_userdata {
273 unsigned char data[];
277 * struct nft_set_elem - generic representation of set elements
280 * @key_end: closing element key
281 * @priv: element private data and extensions
283 struct nft_set_elem {
285 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
289 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
293 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
300 struct nft_set_iter {
305 int (*fn)(const struct nft_ctx *ctx,
307 const struct nft_set_iter *iter,
308 struct nft_set_elem *elem);
312 * struct nft_set_desc - description of set elements
316 * @size: number of set elements
317 * @field_len: length of each field in concatenation, bytes
318 * @field_count: number of concatenated fields in element
319 * @expr: set must support for expressions
321 struct nft_set_desc {
325 u8 field_len[NFT_REG32_COUNT];
331 * enum nft_set_class - performance class
333 * @NFT_LOOKUP_O_1: constant, O(1)
334 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
335 * @NFT_LOOKUP_O_N: linear, O(N)
339 NFT_SET_CLASS_O_LOG_N,
344 * struct nft_set_estimate - estimation of memory and performance
347 * @size: required memory
348 * @lookup: lookup performance class
349 * @space: memory class
351 struct nft_set_estimate {
353 enum nft_set_class lookup;
354 enum nft_set_class space;
357 #define NFT_EXPR_MAXATTR 16
358 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
359 ALIGN(size, __alignof__(struct nft_expr)))
362 * struct nft_expr - nf_tables expression
364 * @ops: expression ops
365 * @data: expression private data
368 const struct nft_expr_ops *ops;
370 __attribute__((aligned(__alignof__(u64))));
373 static inline void *nft_expr_priv(const struct nft_expr *expr)
375 return (void *)expr->data;
378 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
379 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
380 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
381 const struct nft_expr *expr);
382 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
383 const struct nft_expr *expr);
388 * struct nft_set_ops - nf_tables set operations
390 * @lookup: look up an element within the set
391 * @update: update an element if exists, add it if doesn't exist
392 * @delete: delete an element
393 * @insert: insert new element into set
394 * @activate: activate new element in the next generation
395 * @deactivate: lookup for element and deactivate it in the next generation
396 * @flush: deactivate element in the next generation
397 * @remove: remove element from set
398 * @walk: iterate over all set elements
399 * @get: get set elements
400 * @privsize: function to return size of set private data
401 * @init: initialize private data of new set instance
402 * @destroy: destroy private data of set instance
403 * @elemsize: element private size
405 * Operations lookup, update and delete have simpler interfaces, are faster
406 * and currently only used in the packet path. All the rest are slower,
407 * control plane functions.
410 bool (*lookup)(const struct net *net,
411 const struct nft_set *set,
413 const struct nft_set_ext **ext);
414 bool (*update)(struct nft_set *set,
416 void *(*new)(struct nft_set *,
417 const struct nft_expr *,
419 const struct nft_expr *expr,
420 struct nft_regs *regs,
421 const struct nft_set_ext **ext);
422 bool (*delete)(const struct nft_set *set,
425 int (*insert)(const struct net *net,
426 const struct nft_set *set,
427 const struct nft_set_elem *elem,
428 struct nft_set_ext **ext);
429 void (*activate)(const struct net *net,
430 const struct nft_set *set,
431 const struct nft_set_elem *elem);
432 void * (*deactivate)(const struct net *net,
433 const struct nft_set *set,
434 const struct nft_set_elem *elem);
435 bool (*flush)(const struct net *net,
436 const struct nft_set *set,
438 void (*remove)(const struct net *net,
439 const struct nft_set *set,
440 const struct nft_set_elem *elem);
441 void (*walk)(const struct nft_ctx *ctx,
443 struct nft_set_iter *iter);
444 void * (*get)(const struct net *net,
445 const struct nft_set *set,
446 const struct nft_set_elem *elem,
449 u64 (*privsize)(const struct nlattr * const nla[],
450 const struct nft_set_desc *desc);
451 bool (*estimate)(const struct nft_set_desc *desc,
453 struct nft_set_estimate *est);
454 int (*init)(const struct nft_set *set,
455 const struct nft_set_desc *desc,
456 const struct nlattr * const nla[]);
457 void (*destroy)(const struct nft_set *set);
458 void (*gc_init)(const struct nft_set *set);
460 unsigned int elemsize;
464 * struct nft_set_type - nf_tables set type
466 * @ops: set ops for this type
467 * @features: features supported by the implementation
469 struct nft_set_type {
470 const struct nft_set_ops ops;
473 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
475 struct nft_set_elem_expr {
478 __attribute__((aligned(__alignof__(struct nft_expr))));
481 #define nft_setelem_expr_at(__elem_expr, __offset) \
482 ((struct nft_expr *)&__elem_expr->data[__offset])
484 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
485 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
486 __size < (__elem_expr)->size; \
487 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
489 #define NFT_SET_EXPR_MAX 2
492 * struct nft_set - nf_tables set instance
494 * @list: table set list node
495 * @bindings: list of set bindings
496 * @table: table this set belongs to
497 * @net: netnamespace this set belongs to
498 * @name: name of the set
499 * @handle: unique handle of the set
500 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
501 * @dtype: data type (verdict or numeric type defined by userspace)
502 * @objtype: object type (see NFT_OBJECT_* definitions)
503 * @size: maximum set size
504 * @field_len: length of each field in concatenation, bytes
505 * @field_count: number of concatenated fields in element
506 * @use: number of rules references to this set
507 * @nelems: number of elements
508 * @ndeact: number of deactivated elements queued for removal
509 * @timeout: default timeout value in jiffies
510 * @gc_int: garbage collection interval in msecs
511 * @policy: set parameterization (see enum nft_set_policies)
512 * @udlen: user data length
514 * @expr: stateful expression
517 * @genmask: generation mask
520 * @data: private set data
523 struct list_head list;
524 struct list_head bindings;
525 struct nft_table *table;
533 u8 field_len[NFT_REG32_COUNT];
542 unsigned char *udata;
543 /* runtime data below here */
544 const struct nft_set_ops *ops ____cacheline_aligned;
550 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
551 struct list_head catchall_list;
553 __attribute__((aligned(__alignof__(u64))));
556 static inline bool nft_set_is_anonymous(const struct nft_set *set)
558 return set->flags & NFT_SET_ANONYMOUS;
561 static inline void *nft_set_priv(const struct nft_set *set)
563 return (void *)set->data;
566 static inline struct nft_set *nft_set_container_of(const void *priv)
568 return (void *)priv - offsetof(struct nft_set, data);
571 struct nft_set *nft_set_lookup_global(const struct net *net,
572 const struct nft_table *table,
573 const struct nlattr *nla_set_name,
574 const struct nlattr *nla_set_id,
577 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
578 const struct nft_set *set);
579 void *nft_set_catchall_gc(const struct nft_set *set);
581 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
583 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
587 * struct nft_set_binding - nf_tables set binding
589 * @list: set bindings list node
590 * @chain: chain containing the rule bound to the set
591 * @flags: set action flags
593 * A set binding contains all information necessary for validation
594 * of new elements added to a bound set.
596 struct nft_set_binding {
597 struct list_head list;
598 const struct nft_chain *chain;
602 enum nft_trans_phase;
603 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
604 struct nft_set_binding *binding,
605 enum nft_trans_phase phase);
606 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
607 struct nft_set_binding *binding);
608 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
611 * enum nft_set_extensions - set extension type IDs
613 * @NFT_SET_EXT_KEY: element key
614 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
615 * @NFT_SET_EXT_DATA: mapping data
616 * @NFT_SET_EXT_FLAGS: element flags
617 * @NFT_SET_EXT_TIMEOUT: element timeout
618 * @NFT_SET_EXT_EXPIRATION: element expiration time
619 * @NFT_SET_EXT_USERDATA: user data associated with the element
620 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
621 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
622 * @NFT_SET_EXT_NUM: number of extension types
624 enum nft_set_extensions {
630 NFT_SET_EXT_EXPIRATION,
631 NFT_SET_EXT_USERDATA,
632 NFT_SET_EXT_EXPRESSIONS,
638 * struct nft_set_ext_type - set extension type
640 * @len: fixed part length of the extension
641 * @align: alignment requirements of the extension
643 struct nft_set_ext_type {
648 extern const struct nft_set_ext_type nft_set_ext_types[];
651 * struct nft_set_ext_tmpl - set extension template
653 * @len: length of extension area
654 * @offset: offsets of individual extension types
656 struct nft_set_ext_tmpl {
658 u8 offset[NFT_SET_EXT_NUM];
659 u8 ext_len[NFT_SET_EXT_NUM];
663 * struct nft_set_ext - set extensions
665 * @genmask: generation mask
666 * @offset: offsets of individual extension types
667 * @data: beginning of extension data
671 u8 offset[NFT_SET_EXT_NUM];
675 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
677 memset(tmpl, 0, sizeof(*tmpl));
678 tmpl->len = sizeof(struct nft_set_ext);
681 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
684 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
685 if (tmpl->len > U8_MAX)
688 tmpl->offset[id] = tmpl->len;
689 tmpl->ext_len[id] = nft_set_ext_types[id].len + len;
690 tmpl->len += tmpl->ext_len[id];
695 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
697 return nft_set_ext_add_length(tmpl, id, 0);
700 static inline void nft_set_ext_init(struct nft_set_ext *ext,
701 const struct nft_set_ext_tmpl *tmpl)
703 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
706 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
708 return !!ext->offset[id];
711 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
713 return ext && __nft_set_ext_exists(ext, id);
716 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
718 return (void *)ext + ext->offset[id];
721 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
723 return nft_set_ext(ext, NFT_SET_EXT_KEY);
726 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
728 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
731 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
733 return nft_set_ext(ext, NFT_SET_EXT_DATA);
736 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
738 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
741 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
743 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
746 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
748 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
751 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
753 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
756 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
758 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
761 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
763 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
764 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
767 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
770 return elem + set->ops->elemsize;
773 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
775 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
778 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
779 const struct nft_set *set,
780 const struct nlattr *attr);
782 void *nft_set_elem_init(const struct nft_set *set,
783 const struct nft_set_ext_tmpl *tmpl,
784 const u32 *key, const u32 *key_end, const u32 *data,
785 u64 timeout, u64 expiration, gfp_t gfp);
786 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
787 struct nft_expr *expr_array[]);
788 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
792 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
795 * @set: set the elements belong to
796 * @cnt: count of elements
798 struct nft_set_gc_batch_head {
800 const struct nft_set *set;
804 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
805 sizeof(struct nft_set_gc_batch_head)) / \
809 * struct nft_set_gc_batch - nf_tables set garbage collection batch
811 * @head: GC batch head
812 * @elems: garbage collection elements
814 struct nft_set_gc_batch {
815 struct nft_set_gc_batch_head head;
816 void *elems[NFT_SET_GC_BATCH_SIZE];
819 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
821 void nft_set_gc_batch_release(struct rcu_head *rcu);
823 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
826 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
829 static inline struct nft_set_gc_batch *
830 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
834 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
836 nft_set_gc_batch_complete(gcb);
838 return nft_set_gc_batch_alloc(set, gfp);
841 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
844 gcb->elems[gcb->head.cnt++] = elem;
849 * struct nft_expr_type - nf_tables expression type
851 * @select_ops: function to select nft_expr_ops
852 * @release_ops: release nft_expr_ops
853 * @ops: default ops, used when no select_ops functions is present
854 * @list: used internally
856 * @owner: module reference
857 * @policy: netlink attribute policy
858 * @maxattr: highest netlink attribute number
859 * @family: address family for AF-specific types
860 * @flags: expression type flags
862 struct nft_expr_type {
863 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
864 const struct nlattr * const tb[]);
865 void (*release_ops)(const struct nft_expr_ops *ops);
866 const struct nft_expr_ops *ops;
867 struct list_head list;
869 struct module *owner;
870 const struct nla_policy *policy;
871 unsigned int maxattr;
876 #define NFT_EXPR_STATEFUL 0x1
877 #define NFT_EXPR_GC 0x2
879 enum nft_trans_phase {
886 struct nft_flow_rule;
887 struct nft_offload_ctx;
890 * struct nft_expr_ops - nf_tables expression operations
892 * @eval: Expression evaluation function
893 * @size: full expression size, including private data size
894 * @init: initialization function
895 * @activate: activate expression in the next generation
896 * @deactivate: deactivate expression in next generation
897 * @destroy: destruction function, called after synchronize_rcu
898 * @dump: function to dump parameters
899 * @type: expression type
900 * @validate: validate expression, called during loop detection
901 * @data: extra data to attach to this expression operation
903 struct nft_expr_ops {
904 void (*eval)(const struct nft_expr *expr,
905 struct nft_regs *regs,
906 const struct nft_pktinfo *pkt);
907 int (*clone)(struct nft_expr *dst,
908 const struct nft_expr *src);
911 int (*init)(const struct nft_ctx *ctx,
912 const struct nft_expr *expr,
913 const struct nlattr * const tb[]);
914 void (*activate)(const struct nft_ctx *ctx,
915 const struct nft_expr *expr);
916 void (*deactivate)(const struct nft_ctx *ctx,
917 const struct nft_expr *expr,
918 enum nft_trans_phase phase);
919 void (*destroy)(const struct nft_ctx *ctx,
920 const struct nft_expr *expr);
921 void (*destroy_clone)(const struct nft_ctx *ctx,
922 const struct nft_expr *expr);
923 int (*dump)(struct sk_buff *skb,
924 const struct nft_expr *expr);
925 int (*validate)(const struct nft_ctx *ctx,
926 const struct nft_expr *expr,
927 const struct nft_data **data);
928 bool (*reduce)(struct nft_regs_track *track,
929 const struct nft_expr *expr);
930 bool (*gc)(struct net *net,
931 const struct nft_expr *expr);
932 int (*offload)(struct nft_offload_ctx *ctx,
933 struct nft_flow_rule *flow,
934 const struct nft_expr *expr);
935 bool (*offload_action)(const struct nft_expr *expr);
936 void (*offload_stats)(struct nft_expr *expr,
937 const struct flow_stats *stats);
938 const struct nft_expr_type *type;
943 * struct nft_rule - nf_tables rule
945 * @list: used internally
946 * @handle: rule handle
947 * @genmask: generation mask
948 * @dlen: length of expression data
949 * @udata: user data is appended to the rule
950 * @data: expression data
953 struct list_head list;
959 __attribute__((aligned(__alignof__(struct nft_expr))));
962 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
964 return (struct nft_expr *)&rule->data[0];
967 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
969 return ((void *)expr) + expr->ops->size;
972 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
974 return (struct nft_expr *)&rule->data[rule->dlen];
977 static inline bool nft_expr_more(const struct nft_rule *rule,
978 const struct nft_expr *expr)
980 return expr != nft_expr_last(rule) && expr->ops;
983 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
985 return (void *)&rule->data[rule->dlen];
988 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
990 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
991 struct nft_regs *regs,
992 const struct nft_pktinfo *pkt)
994 struct nft_set_elem_expr *elem_expr;
995 struct nft_expr *expr;
998 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
999 elem_expr = nft_set_ext_expr(ext);
1000 nft_setelem_expr_foreach(expr, elem_expr, size) {
1001 expr->ops->eval(expr, regs, pkt);
1002 if (regs->verdict.code == NFT_BREAK)
1009 * The last pointer isn't really necessary, but the compiler isn't able to
1010 * determine that the result of nft_expr_last() is always the same since it
1011 * can't assume that the dlen value wasn't changed within calls in the loop.
1013 #define nft_rule_for_each_expr(expr, last, rule) \
1014 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1016 (expr) = nft_expr_next(expr))
1018 #define NFT_CHAIN_POLICY_UNSET U8_MAX
1020 struct nft_rule_dp {
1023 handle:42; /* for tracing */
1024 unsigned char data[]
1025 __attribute__((aligned(__alignof__(struct nft_expr))));
1028 struct nft_rule_blob {
1030 unsigned char data[]
1031 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1035 * struct nft_chain - nf_tables chain
1037 * @rules: list of rules in the chain
1038 * @list: used internally
1039 * @rhlhead: used internally
1040 * @table: table that this chain belongs to
1041 * @handle: chain handle
1042 * @use: number of jump references to this chain
1043 * @flags: bitmask of enum nft_chain_flags
1044 * @name: name of the chain
1047 struct nft_rule_blob __rcu *blob_gen_0;
1048 struct nft_rule_blob __rcu *blob_gen_1;
1049 struct list_head rules;
1050 struct list_head list;
1051 struct rhlist_head rhlhead;
1052 struct nft_table *table;
1062 /* Only used during control plane commit phase: */
1063 struct nft_rule_blob *blob_next;
1066 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1068 enum nft_chain_types {
1069 NFT_CHAIN_T_DEFAULT = 0,
1076 * struct nft_chain_type - nf_tables chain type info
1078 * @name: name of the type
1079 * @type: numeric identifier
1080 * @family: address family
1081 * @owner: module owner
1082 * @hook_mask: mask of valid hooks
1083 * @hooks: array of hook functions
1084 * @ops_register: base chain register function
1085 * @ops_unregister: base chain unregister function
1087 struct nft_chain_type {
1089 enum nft_chain_types type;
1091 struct module *owner;
1092 unsigned int hook_mask;
1093 nf_hookfn *hooks[NFT_MAX_HOOKS];
1094 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1095 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1098 int nft_chain_validate_dependency(const struct nft_chain *chain,
1099 enum nft_chain_types type);
1100 int nft_chain_validate_hooks(const struct nft_chain *chain,
1101 unsigned int hook_flags);
1103 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1105 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1108 void nft_chain_del(struct nft_chain *chain);
1109 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1114 struct u64_stats_sync syncp;
1118 struct list_head list;
1119 struct nf_hook_ops ops;
1120 struct rcu_head rcu;
1124 * struct nft_base_chain - nf_tables base chain
1126 * @ops: netfilter hook ops
1127 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1129 * @policy: default policy
1130 * @stats: per-cpu chain stats
1132 * @flow_block: flow block (for hardware offload)
1134 struct nft_base_chain {
1135 struct nf_hook_ops ops;
1136 struct list_head hook_list;
1137 const struct nft_chain_type *type;
1140 struct nft_stats __percpu *stats;
1141 struct nft_chain chain;
1142 struct flow_block flow_block;
1145 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1147 return container_of(chain, struct nft_base_chain, chain);
1150 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1152 return chain->flags & NFT_CHAIN_BASE;
1155 int __nft_release_basechain(struct nft_ctx *ctx);
1157 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1160 * struct nft_table - nf_tables table
1162 * @list: used internally
1163 * @chains_ht: chains in the table
1164 * @chains: same, for stable walks
1165 * @sets: sets in the table
1166 * @objects: stateful objects in the table
1167 * @flowtables: flow tables in the table
1168 * @hgenerator: handle generator state
1169 * @handle: table handle
1170 * @use: number of chain references to this table
1171 * @flags: table flag (see enum nft_table_flags)
1172 * @genmask: generation mask
1173 * @afinfo: address family info
1174 * @name: name of the table
1177 struct list_head list;
1178 struct rhltable chains_ht;
1179 struct list_head chains;
1180 struct list_head sets;
1181 struct list_head objects;
1182 struct list_head flowtables;
1195 static inline bool nft_table_has_owner(const struct nft_table *table)
1197 return table->flags & NFT_TABLE_F_OWNER;
1200 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1202 return family == NFPROTO_NETDEV ||
1203 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1206 void nft_register_chain_type(const struct nft_chain_type *);
1207 void nft_unregister_chain_type(const struct nft_chain_type *);
1209 int nft_register_expr(struct nft_expr_type *);
1210 void nft_unregister_expr(struct nft_expr_type *);
1212 int nft_verdict_dump(struct sk_buff *skb, int type,
1213 const struct nft_verdict *v);
1216 * struct nft_object_hash_key - key to lookup nft_object
1218 * @name: name of the stateful object to look up
1219 * @table: table the object belongs to
1221 struct nft_object_hash_key {
1223 const struct nft_table *table;
1227 * struct nft_object - nf_tables stateful object
1229 * @list: table stateful object list node
1230 * @key: keys that identify this object
1231 * @rhlhead: nft_objname_ht node
1232 * @genmask: generation mask
1233 * @use: number of references to this stateful object
1234 * @handle: unique object handle
1235 * @ops: object operations
1236 * @data: object data, layout depends on type
1239 struct list_head list;
1240 struct rhlist_head rhlhead;
1241 struct nft_object_hash_key key;
1247 /* runtime data below here */
1248 const struct nft_object_ops *ops ____cacheline_aligned;
1249 unsigned char data[]
1250 __attribute__((aligned(__alignof__(u64))));
1253 static inline void *nft_obj_data(const struct nft_object *obj)
1255 return (void *)obj->data;
1258 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1260 struct nft_object *nft_obj_lookup(const struct net *net,
1261 const struct nft_table *table,
1262 const struct nlattr *nla, u32 objtype,
1265 void nft_obj_notify(struct net *net, const struct nft_table *table,
1266 struct nft_object *obj, u32 portid, u32 seq,
1267 int event, u16 flags, int family, int report, gfp_t gfp);
1270 * struct nft_object_type - stateful object type
1272 * @select_ops: function to select nft_object_ops
1273 * @ops: default ops, used when no select_ops functions is present
1274 * @list: list node in list of object types
1275 * @type: stateful object numeric type
1276 * @owner: module owner
1277 * @maxattr: maximum netlink attribute
1278 * @policy: netlink attribute policy
1280 struct nft_object_type {
1281 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1282 const struct nlattr * const tb[]);
1283 const struct nft_object_ops *ops;
1284 struct list_head list;
1286 unsigned int maxattr;
1287 struct module *owner;
1288 const struct nla_policy *policy;
1292 * struct nft_object_ops - stateful object operations
1294 * @eval: stateful object evaluation function
1295 * @size: stateful object size
1296 * @init: initialize object from netlink attributes
1297 * @destroy: release existing stateful object
1298 * @dump: netlink dump stateful object
1299 * @update: update stateful object
1301 struct nft_object_ops {
1302 void (*eval)(struct nft_object *obj,
1303 struct nft_regs *regs,
1304 const struct nft_pktinfo *pkt);
1306 int (*init)(const struct nft_ctx *ctx,
1307 const struct nlattr *const tb[],
1308 struct nft_object *obj);
1309 void (*destroy)(const struct nft_ctx *ctx,
1310 struct nft_object *obj);
1311 int (*dump)(struct sk_buff *skb,
1312 struct nft_object *obj,
1314 void (*update)(struct nft_object *obj,
1315 struct nft_object *newobj);
1316 const struct nft_object_type *type;
1319 int nft_register_obj(struct nft_object_type *obj_type);
1320 void nft_unregister_obj(struct nft_object_type *obj_type);
1322 #define NFT_NETDEVICE_MAX 256
1325 * struct nft_flowtable - nf_tables flow table
1327 * @list: flow table list node in table list
1328 * @table: the table the flow table is contained in
1329 * @name: name of this flow table
1330 * @hooknum: hook number
1331 * @ops_len: number of hooks in array
1332 * @genmask: generation mask
1333 * @use: number of references to this flow table
1334 * @handle: unique object handle
1335 * @dev_name: array of device names
1336 * @data: rhashtable and garbage collector
1337 * @ops: array of hooks
1339 struct nft_flowtable {
1340 struct list_head list;
1341 struct nft_table *table;
1348 /* runtime data below here */
1349 struct list_head hook_list ____cacheline_aligned;
1350 struct nf_flowtable data;
1353 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1354 const struct nlattr *nla,
1357 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1358 struct nft_flowtable *flowtable,
1359 enum nft_trans_phase phase);
1361 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1362 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1365 * struct nft_traceinfo - nft tracing information and state
1367 * @trace: other struct members are initialised
1368 * @nf_trace: copy of skb->nf_trace before rule evaluation
1369 * @type: event type (enum nft_trace_types)
1370 * @skbid: hash of skb to be used as trace id
1371 * @packet_dumped: packet headers sent in a previous traceinfo message
1372 * @pkt: pktinfo currently processed
1373 * @basechain: base chain currently processed
1374 * @chain: chain currently processed
1375 * @rule: rule that was evaluated
1376 * @verdict: verdict given by rule
1378 struct nft_traceinfo {
1382 enum nft_trace_types type:8;
1384 const struct nft_pktinfo *pkt;
1385 const struct nft_base_chain *basechain;
1386 const struct nft_chain *chain;
1387 const struct nft_rule_dp *rule;
1388 const struct nft_verdict *verdict;
1391 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1392 const struct nft_verdict *verdict,
1393 const struct nft_chain *basechain);
1395 void nft_trace_notify(struct nft_traceinfo *info);
1397 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1398 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1400 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1401 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1403 #define MODULE_ALIAS_NFT_EXPR(name) \
1404 MODULE_ALIAS("nft-expr-" name)
1406 #define MODULE_ALIAS_NFT_OBJ(type) \
1407 MODULE_ALIAS("nft-obj-" __stringify(type))
1409 #if IS_ENABLED(CONFIG_NF_TABLES)
1412 * The gencursor defines two generations, the currently active and the
1413 * next one. Objects contain a bitmask of 2 bits specifying the generations
1414 * they're active in. A set bit means they're inactive in the generation
1415 * represented by that bit.
1417 * New objects start out as inactive in the current and active in the
1418 * next generation. When committing the ruleset the bitmask is cleared,
1419 * meaning they're active in all generations. When removing an object,
1420 * it is set inactive in the next generation. After committing the ruleset,
1421 * the objects are removed.
1423 static inline unsigned int nft_gencursor_next(const struct net *net)
1425 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1428 static inline u8 nft_genmask_next(const struct net *net)
1430 return 1 << nft_gencursor_next(net);
1433 static inline u8 nft_genmask_cur(const struct net *net)
1435 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1436 return 1 << READ_ONCE(net->nft.gencursor);
1439 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1442 * Generic transaction helpers
1445 /* Check if this object is currently active. */
1446 #define nft_is_active(__net, __obj) \
1447 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1449 /* Check if this object is active in the next generation. */
1450 #define nft_is_active_next(__net, __obj) \
1451 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1453 /* This object becomes active in the next generation. */
1454 #define nft_activate_next(__net, __obj) \
1455 (__obj)->genmask = nft_genmask_cur(__net)
1457 /* This object becomes inactive in the next generation. */
1458 #define nft_deactivate_next(__net, __obj) \
1459 (__obj)->genmask = nft_genmask_next(__net)
1461 /* After committing the ruleset, clear the stale generation bit. */
1462 #define nft_clear(__net, __obj) \
1463 (__obj)->genmask &= ~nft_genmask_next(__net)
1464 #define nft_active_genmask(__obj, __genmask) \
1465 !((__obj)->genmask & __genmask)
1468 * Set element transaction helpers
1471 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1474 return !(ext->genmask & genmask);
1477 static inline void nft_set_elem_change_active(const struct net *net,
1478 const struct nft_set *set,
1479 struct nft_set_ext *ext)
1481 ext->genmask ^= nft_genmask_next(net);
1484 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1487 * We use a free bit in the genmask field to indicate the element
1488 * is busy, meaning it is currently being processed either by
1489 * the netlink API or GC.
1491 * Even though the genmask is only a single byte wide, this works
1492 * because the extension structure if fully constant once initialized,
1493 * so there are no non-atomic write accesses unless it is already
1496 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1498 #if defined(__LITTLE_ENDIAN_BITFIELD)
1499 #define NFT_SET_ELEM_BUSY_BIT 2
1500 #elif defined(__BIG_ENDIAN_BITFIELD)
1501 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1506 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1508 unsigned long *word = (unsigned long *)ext;
1510 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1511 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1514 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1516 unsigned long *word = (unsigned long *)ext;
1518 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1522 * struct nft_trans - nf_tables object update in transaction
1524 * @list: used internally
1525 * @msg_type: message type
1526 * @put_net: ctx->net needs to be put
1527 * @ctx: transaction context
1528 * @data: internal information related to the transaction
1531 struct list_head list;
1538 struct nft_trans_rule {
1539 struct nft_rule *rule;
1540 struct nft_flow_rule *flow;
1544 #define nft_trans_rule(trans) \
1545 (((struct nft_trans_rule *)trans->data)->rule)
1546 #define nft_trans_flow_rule(trans) \
1547 (((struct nft_trans_rule *)trans->data)->flow)
1548 #define nft_trans_rule_id(trans) \
1549 (((struct nft_trans_rule *)trans->data)->rule_id)
1551 struct nft_trans_set {
1552 struct nft_set *set;
1557 #define nft_trans_set(trans) \
1558 (((struct nft_trans_set *)trans->data)->set)
1559 #define nft_trans_set_id(trans) \
1560 (((struct nft_trans_set *)trans->data)->set_id)
1561 #define nft_trans_set_bound(trans) \
1562 (((struct nft_trans_set *)trans->data)->bound)
1564 struct nft_trans_chain {
1567 struct nft_stats __percpu *stats;
1572 #define nft_trans_chain_update(trans) \
1573 (((struct nft_trans_chain *)trans->data)->update)
1574 #define nft_trans_chain_name(trans) \
1575 (((struct nft_trans_chain *)trans->data)->name)
1576 #define nft_trans_chain_stats(trans) \
1577 (((struct nft_trans_chain *)trans->data)->stats)
1578 #define nft_trans_chain_policy(trans) \
1579 (((struct nft_trans_chain *)trans->data)->policy)
1580 #define nft_trans_chain_id(trans) \
1581 (((struct nft_trans_chain *)trans->data)->chain_id)
1583 struct nft_trans_table {
1587 #define nft_trans_table_update(trans) \
1588 (((struct nft_trans_table *)trans->data)->update)
1590 struct nft_trans_elem {
1591 struct nft_set *set;
1592 struct nft_set_elem elem;
1596 #define nft_trans_elem_set(trans) \
1597 (((struct nft_trans_elem *)trans->data)->set)
1598 #define nft_trans_elem(trans) \
1599 (((struct nft_trans_elem *)trans->data)->elem)
1600 #define nft_trans_elem_set_bound(trans) \
1601 (((struct nft_trans_elem *)trans->data)->bound)
1603 struct nft_trans_obj {
1604 struct nft_object *obj;
1605 struct nft_object *newobj;
1609 #define nft_trans_obj(trans) \
1610 (((struct nft_trans_obj *)trans->data)->obj)
1611 #define nft_trans_obj_newobj(trans) \
1612 (((struct nft_trans_obj *)trans->data)->newobj)
1613 #define nft_trans_obj_update(trans) \
1614 (((struct nft_trans_obj *)trans->data)->update)
1616 struct nft_trans_flowtable {
1617 struct nft_flowtable *flowtable;
1619 struct list_head hook_list;
1623 #define nft_trans_flowtable(trans) \
1624 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1625 #define nft_trans_flowtable_update(trans) \
1626 (((struct nft_trans_flowtable *)trans->data)->update)
1627 #define nft_trans_flowtable_hooks(trans) \
1628 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1629 #define nft_trans_flowtable_flags(trans) \
1630 (((struct nft_trans_flowtable *)trans->data)->flags)
1632 int __init nft_chain_filter_init(void);
1633 void nft_chain_filter_fini(void);
1635 void __init nft_chain_route_init(void);
1636 void nft_chain_route_fini(void);
1638 void nf_tables_trans_destroy_flush_work(void);
1640 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1641 __be64 nf_jiffies64_to_msecs(u64 input);
1643 #ifdef CONFIG_MODULES
1644 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1646 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1649 struct nftables_pernet {
1650 struct list_head tables;
1651 struct list_head commit_list;
1652 struct list_head module_list;
1653 struct list_head notify_list;
1654 struct mutex commit_mutex;
1656 unsigned int base_seq;
1660 extern unsigned int nf_tables_net_id;
1662 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1664 return net_generic(net, nf_tables_net_id);
1667 #define __NFT_REDUCE_READONLY 1UL
1668 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1670 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1672 return expr->ops->reduce == NFT_REDUCE_READONLY;
1675 void nft_reg_track_update(struct nft_regs_track *track,
1676 const struct nft_expr *expr, u8 dreg, u8 len);
1677 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1678 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1680 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1681 const struct nft_expr *expr, u8 dreg)
1683 return track->regs[dreg].selector &&
1684 track->regs[dreg].selector->ops == expr->ops &&
1685 track->regs[dreg].num_reg == 0;
1688 #endif /* _NET_NF_TABLES_H */