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 struct nft_data_desc {
225 enum nft_data_types type;
229 int nft_data_init(const struct nft_ctx *ctx,
230 struct nft_data *data, unsigned int size,
231 struct nft_data_desc *desc, const struct nlattr *nla);
232 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
233 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
234 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
235 enum nft_data_types type, unsigned int len);
237 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
239 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
242 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
244 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
247 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
248 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
250 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
251 int nft_parse_register_store(const struct nft_ctx *ctx,
252 const struct nlattr *attr, u8 *dreg,
253 const struct nft_data *data,
254 enum nft_data_types type, unsigned int len);
257 * struct nft_userdata - user defined data associated with an object
259 * @len: length of the data
262 * The presence of user data is indicated in an object specific fashion,
263 * so a length of zero can't occur and the value "len" indicates data
266 struct nft_userdata {
268 unsigned char data[];
272 * struct nft_set_elem - generic representation of set elements
275 * @key_end: closing element key
276 * @priv: element private data and extensions
278 struct nft_set_elem {
280 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
284 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
288 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
295 struct nft_set_iter {
300 int (*fn)(const struct nft_ctx *ctx,
302 const struct nft_set_iter *iter,
303 struct nft_set_elem *elem);
307 * struct nft_set_desc - description of set elements
311 * @size: number of set elements
312 * @field_len: length of each field in concatenation, bytes
313 * @field_count: number of concatenated fields in element
314 * @expr: set must support for expressions
316 struct nft_set_desc {
320 u8 field_len[NFT_REG32_COUNT];
326 * enum nft_set_class - performance class
328 * @NFT_LOOKUP_O_1: constant, O(1)
329 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
330 * @NFT_LOOKUP_O_N: linear, O(N)
334 NFT_SET_CLASS_O_LOG_N,
339 * struct nft_set_estimate - estimation of memory and performance
342 * @size: required memory
343 * @lookup: lookup performance class
344 * @space: memory class
346 struct nft_set_estimate {
348 enum nft_set_class lookup;
349 enum nft_set_class space;
352 #define NFT_EXPR_MAXATTR 16
353 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
354 ALIGN(size, __alignof__(struct nft_expr)))
357 * struct nft_expr - nf_tables expression
359 * @ops: expression ops
360 * @data: expression private data
363 const struct nft_expr_ops *ops;
365 __attribute__((aligned(__alignof__(u64))));
368 static inline void *nft_expr_priv(const struct nft_expr *expr)
370 return (void *)expr->data;
373 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
374 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
375 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
376 const struct nft_expr *expr);
377 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
378 const struct nft_expr *expr);
383 * struct nft_set_ops - nf_tables set operations
385 * @lookup: look up an element within the set
386 * @update: update an element if exists, add it if doesn't exist
387 * @delete: delete an element
388 * @insert: insert new element into set
389 * @activate: activate new element in the next generation
390 * @deactivate: lookup for element and deactivate it in the next generation
391 * @flush: deactivate element in the next generation
392 * @remove: remove element from set
393 * @walk: iterate over all set elements
394 * @get: get set elements
395 * @privsize: function to return size of set private data
396 * @init: initialize private data of new set instance
397 * @destroy: destroy private data of set instance
398 * @elemsize: element private size
400 * Operations lookup, update and delete have simpler interfaces, are faster
401 * and currently only used in the packet path. All the rest are slower,
402 * control plane functions.
405 bool (*lookup)(const struct net *net,
406 const struct nft_set *set,
408 const struct nft_set_ext **ext);
409 bool (*update)(struct nft_set *set,
411 void *(*new)(struct nft_set *,
412 const struct nft_expr *,
414 const struct nft_expr *expr,
415 struct nft_regs *regs,
416 const struct nft_set_ext **ext);
417 bool (*delete)(const struct nft_set *set,
420 int (*insert)(const struct net *net,
421 const struct nft_set *set,
422 const struct nft_set_elem *elem,
423 struct nft_set_ext **ext);
424 void (*activate)(const struct net *net,
425 const struct nft_set *set,
426 const struct nft_set_elem *elem);
427 void * (*deactivate)(const struct net *net,
428 const struct nft_set *set,
429 const struct nft_set_elem *elem);
430 bool (*flush)(const struct net *net,
431 const struct nft_set *set,
433 void (*remove)(const struct net *net,
434 const struct nft_set *set,
435 const struct nft_set_elem *elem);
436 void (*walk)(const struct nft_ctx *ctx,
438 struct nft_set_iter *iter);
439 void * (*get)(const struct net *net,
440 const struct nft_set *set,
441 const struct nft_set_elem *elem,
444 u64 (*privsize)(const struct nlattr * const nla[],
445 const struct nft_set_desc *desc);
446 bool (*estimate)(const struct nft_set_desc *desc,
448 struct nft_set_estimate *est);
449 int (*init)(const struct nft_set *set,
450 const struct nft_set_desc *desc,
451 const struct nlattr * const nla[]);
452 void (*destroy)(const struct nft_set *set);
453 void (*gc_init)(const struct nft_set *set);
455 unsigned int elemsize;
459 * struct nft_set_type - nf_tables set type
461 * @ops: set ops for this type
462 * @features: features supported by the implementation
464 struct nft_set_type {
465 const struct nft_set_ops ops;
468 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
470 struct nft_set_elem_expr {
473 __attribute__((aligned(__alignof__(struct nft_expr))));
476 #define nft_setelem_expr_at(__elem_expr, __offset) \
477 ((struct nft_expr *)&__elem_expr->data[__offset])
479 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
480 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
481 __size < (__elem_expr)->size; \
482 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
484 #define NFT_SET_EXPR_MAX 2
487 * struct nft_set - nf_tables set instance
489 * @list: table set list node
490 * @bindings: list of set bindings
491 * @table: table this set belongs to
492 * @net: netnamespace this set belongs to
493 * @name: name of the set
494 * @handle: unique handle of the set
495 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
496 * @dtype: data type (verdict or numeric type defined by userspace)
497 * @objtype: object type (see NFT_OBJECT_* definitions)
498 * @size: maximum set size
499 * @field_len: length of each field in concatenation, bytes
500 * @field_count: number of concatenated fields in element
501 * @use: number of rules references to this set
502 * @nelems: number of elements
503 * @ndeact: number of deactivated elements queued for removal
504 * @timeout: default timeout value in jiffies
505 * @gc_int: garbage collection interval in msecs
506 * @policy: set parameterization (see enum nft_set_policies)
507 * @udlen: user data length
509 * @expr: stateful expression
512 * @genmask: generation mask
515 * @data: private set data
518 struct list_head list;
519 struct list_head bindings;
520 struct nft_table *table;
528 u8 field_len[NFT_REG32_COUNT];
537 unsigned char *udata;
538 /* runtime data below here */
539 const struct nft_set_ops *ops ____cacheline_aligned;
545 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
546 struct list_head catchall_list;
548 __attribute__((aligned(__alignof__(u64))));
551 static inline bool nft_set_is_anonymous(const struct nft_set *set)
553 return set->flags & NFT_SET_ANONYMOUS;
556 static inline void *nft_set_priv(const struct nft_set *set)
558 return (void *)set->data;
561 static inline struct nft_set *nft_set_container_of(const void *priv)
563 return (void *)priv - offsetof(struct nft_set, data);
566 struct nft_set *nft_set_lookup_global(const struct net *net,
567 const struct nft_table *table,
568 const struct nlattr *nla_set_name,
569 const struct nlattr *nla_set_id,
572 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
573 const struct nft_set *set);
574 void *nft_set_catchall_gc(const struct nft_set *set);
576 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
578 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
582 * struct nft_set_binding - nf_tables set binding
584 * @list: set bindings list node
585 * @chain: chain containing the rule bound to the set
586 * @flags: set action flags
588 * A set binding contains all information necessary for validation
589 * of new elements added to a bound set.
591 struct nft_set_binding {
592 struct list_head list;
593 const struct nft_chain *chain;
597 enum nft_trans_phase;
598 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
599 struct nft_set_binding *binding,
600 enum nft_trans_phase phase);
601 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
602 struct nft_set_binding *binding);
603 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
606 * enum nft_set_extensions - set extension type IDs
608 * @NFT_SET_EXT_KEY: element key
609 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
610 * @NFT_SET_EXT_DATA: mapping data
611 * @NFT_SET_EXT_FLAGS: element flags
612 * @NFT_SET_EXT_TIMEOUT: element timeout
613 * @NFT_SET_EXT_EXPIRATION: element expiration time
614 * @NFT_SET_EXT_USERDATA: user data associated with the element
615 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
616 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
617 * @NFT_SET_EXT_NUM: number of extension types
619 enum nft_set_extensions {
625 NFT_SET_EXT_EXPIRATION,
626 NFT_SET_EXT_USERDATA,
627 NFT_SET_EXT_EXPRESSIONS,
633 * struct nft_set_ext_type - set extension type
635 * @len: fixed part length of the extension
636 * @align: alignment requirements of the extension
638 struct nft_set_ext_type {
643 extern const struct nft_set_ext_type nft_set_ext_types[];
646 * struct nft_set_ext_tmpl - set extension template
648 * @len: length of extension area
649 * @offset: offsets of individual extension types
651 struct nft_set_ext_tmpl {
653 u8 offset[NFT_SET_EXT_NUM];
657 * struct nft_set_ext - set extensions
659 * @genmask: generation mask
660 * @offset: offsets of individual extension types
661 * @data: beginning of extension data
665 u8 offset[NFT_SET_EXT_NUM];
669 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
671 memset(tmpl, 0, sizeof(*tmpl));
672 tmpl->len = sizeof(struct nft_set_ext);
675 static inline int nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
678 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
679 if (tmpl->len > U8_MAX)
682 tmpl->offset[id] = tmpl->len;
683 tmpl->len += nft_set_ext_types[id].len + len;
688 static inline int nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
690 return nft_set_ext_add_length(tmpl, id, 0);
693 static inline void nft_set_ext_init(struct nft_set_ext *ext,
694 const struct nft_set_ext_tmpl *tmpl)
696 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
699 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
701 return !!ext->offset[id];
704 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
706 return ext && __nft_set_ext_exists(ext, id);
709 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
711 return (void *)ext + ext->offset[id];
714 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
716 return nft_set_ext(ext, NFT_SET_EXT_KEY);
719 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
721 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
724 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
726 return nft_set_ext(ext, NFT_SET_EXT_DATA);
729 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
731 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
734 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
736 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
739 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
741 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
744 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
746 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
749 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
751 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
754 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
756 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
757 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
760 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
763 return elem + set->ops->elemsize;
766 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
768 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
771 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
772 const struct nft_set *set,
773 const struct nlattr *attr);
775 void *nft_set_elem_init(const struct nft_set *set,
776 const struct nft_set_ext_tmpl *tmpl,
777 const u32 *key, const u32 *key_end, const u32 *data,
778 u64 timeout, u64 expiration, gfp_t gfp);
779 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
780 struct nft_expr *expr_array[]);
781 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
785 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
788 * @set: set the elements belong to
789 * @cnt: count of elements
791 struct nft_set_gc_batch_head {
793 const struct nft_set *set;
797 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
798 sizeof(struct nft_set_gc_batch_head)) / \
802 * struct nft_set_gc_batch - nf_tables set garbage collection batch
804 * @head: GC batch head
805 * @elems: garbage collection elements
807 struct nft_set_gc_batch {
808 struct nft_set_gc_batch_head head;
809 void *elems[NFT_SET_GC_BATCH_SIZE];
812 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
814 void nft_set_gc_batch_release(struct rcu_head *rcu);
816 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
819 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
822 static inline struct nft_set_gc_batch *
823 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
827 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
829 nft_set_gc_batch_complete(gcb);
831 return nft_set_gc_batch_alloc(set, gfp);
834 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
837 gcb->elems[gcb->head.cnt++] = elem;
842 * struct nft_expr_type - nf_tables expression type
844 * @select_ops: function to select nft_expr_ops
845 * @release_ops: release nft_expr_ops
846 * @ops: default ops, used when no select_ops functions is present
847 * @list: used internally
849 * @owner: module reference
850 * @policy: netlink attribute policy
851 * @maxattr: highest netlink attribute number
852 * @family: address family for AF-specific types
853 * @flags: expression type flags
855 struct nft_expr_type {
856 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
857 const struct nlattr * const tb[]);
858 void (*release_ops)(const struct nft_expr_ops *ops);
859 const struct nft_expr_ops *ops;
860 struct list_head list;
862 struct module *owner;
863 const struct nla_policy *policy;
864 unsigned int maxattr;
869 #define NFT_EXPR_STATEFUL 0x1
870 #define NFT_EXPR_GC 0x2
872 enum nft_trans_phase {
879 struct nft_flow_rule;
880 struct nft_offload_ctx;
883 * struct nft_expr_ops - nf_tables expression operations
885 * @eval: Expression evaluation function
886 * @size: full expression size, including private data size
887 * @init: initialization function
888 * @activate: activate expression in the next generation
889 * @deactivate: deactivate expression in next generation
890 * @destroy: destruction function, called after synchronize_rcu
891 * @dump: function to dump parameters
892 * @type: expression type
893 * @validate: validate expression, called during loop detection
894 * @data: extra data to attach to this expression operation
896 struct nft_expr_ops {
897 void (*eval)(const struct nft_expr *expr,
898 struct nft_regs *regs,
899 const struct nft_pktinfo *pkt);
900 int (*clone)(struct nft_expr *dst,
901 const struct nft_expr *src);
904 int (*init)(const struct nft_ctx *ctx,
905 const struct nft_expr *expr,
906 const struct nlattr * const tb[]);
907 void (*activate)(const struct nft_ctx *ctx,
908 const struct nft_expr *expr);
909 void (*deactivate)(const struct nft_ctx *ctx,
910 const struct nft_expr *expr,
911 enum nft_trans_phase phase);
912 void (*destroy)(const struct nft_ctx *ctx,
913 const struct nft_expr *expr);
914 void (*destroy_clone)(const struct nft_ctx *ctx,
915 const struct nft_expr *expr);
916 int (*dump)(struct sk_buff *skb,
917 const struct nft_expr *expr);
918 int (*validate)(const struct nft_ctx *ctx,
919 const struct nft_expr *expr,
920 const struct nft_data **data);
921 bool (*reduce)(struct nft_regs_track *track,
922 const struct nft_expr *expr);
923 bool (*gc)(struct net *net,
924 const struct nft_expr *expr);
925 int (*offload)(struct nft_offload_ctx *ctx,
926 struct nft_flow_rule *flow,
927 const struct nft_expr *expr);
928 bool (*offload_action)(const struct nft_expr *expr);
929 void (*offload_stats)(struct nft_expr *expr,
930 const struct flow_stats *stats);
931 const struct nft_expr_type *type;
936 * struct nft_rule - nf_tables rule
938 * @list: used internally
939 * @handle: rule handle
940 * @genmask: generation mask
941 * @dlen: length of expression data
942 * @udata: user data is appended to the rule
943 * @data: expression data
946 struct list_head list;
952 __attribute__((aligned(__alignof__(struct nft_expr))));
955 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
957 return (struct nft_expr *)&rule->data[0];
960 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
962 return ((void *)expr) + expr->ops->size;
965 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
967 return (struct nft_expr *)&rule->data[rule->dlen];
970 static inline bool nft_expr_more(const struct nft_rule *rule,
971 const struct nft_expr *expr)
973 return expr != nft_expr_last(rule) && expr->ops;
976 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
978 return (void *)&rule->data[rule->dlen];
981 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
983 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
984 struct nft_regs *regs,
985 const struct nft_pktinfo *pkt)
987 struct nft_set_elem_expr *elem_expr;
988 struct nft_expr *expr;
991 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
992 elem_expr = nft_set_ext_expr(ext);
993 nft_setelem_expr_foreach(expr, elem_expr, size) {
994 expr->ops->eval(expr, regs, pkt);
995 if (regs->verdict.code == NFT_BREAK)
1002 * The last pointer isn't really necessary, but the compiler isn't able to
1003 * determine that the result of nft_expr_last() is always the same since it
1004 * can't assume that the dlen value wasn't changed within calls in the loop.
1006 #define nft_rule_for_each_expr(expr, last, rule) \
1007 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
1009 (expr) = nft_expr_next(expr))
1011 #define NFT_CHAIN_POLICY_UNSET U8_MAX
1013 struct nft_rule_dp {
1016 handle:42; /* for tracing */
1017 unsigned char data[]
1018 __attribute__((aligned(__alignof__(struct nft_expr))));
1021 struct nft_rule_blob {
1023 unsigned char data[]
1024 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1028 * struct nft_chain - nf_tables chain
1030 * @rules: list of rules in the chain
1031 * @list: used internally
1032 * @rhlhead: used internally
1033 * @table: table that this chain belongs to
1034 * @handle: chain handle
1035 * @use: number of jump references to this chain
1036 * @flags: bitmask of enum nft_chain_flags
1037 * @name: name of the chain
1040 struct nft_rule_blob __rcu *blob_gen_0;
1041 struct nft_rule_blob __rcu *blob_gen_1;
1042 struct list_head rules;
1043 struct list_head list;
1044 struct rhlist_head rhlhead;
1045 struct nft_table *table;
1055 /* Only used during control plane commit phase: */
1056 struct nft_rule_blob *blob_next;
1059 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1061 enum nft_chain_types {
1062 NFT_CHAIN_T_DEFAULT = 0,
1069 * struct nft_chain_type - nf_tables chain type info
1071 * @name: name of the type
1072 * @type: numeric identifier
1073 * @family: address family
1074 * @owner: module owner
1075 * @hook_mask: mask of valid hooks
1076 * @hooks: array of hook functions
1077 * @ops_register: base chain register function
1078 * @ops_unregister: base chain unregister function
1080 struct nft_chain_type {
1082 enum nft_chain_types type;
1084 struct module *owner;
1085 unsigned int hook_mask;
1086 nf_hookfn *hooks[NFT_MAX_HOOKS];
1087 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1088 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1091 int nft_chain_validate_dependency(const struct nft_chain *chain,
1092 enum nft_chain_types type);
1093 int nft_chain_validate_hooks(const struct nft_chain *chain,
1094 unsigned int hook_flags);
1096 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1098 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1101 void nft_chain_del(struct nft_chain *chain);
1102 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1107 struct u64_stats_sync syncp;
1111 struct list_head list;
1112 struct nf_hook_ops ops;
1113 struct rcu_head rcu;
1117 * struct nft_base_chain - nf_tables base chain
1119 * @ops: netfilter hook ops
1120 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1122 * @policy: default policy
1123 * @stats: per-cpu chain stats
1125 * @flow_block: flow block (for hardware offload)
1127 struct nft_base_chain {
1128 struct nf_hook_ops ops;
1129 struct list_head hook_list;
1130 const struct nft_chain_type *type;
1133 struct nft_stats __percpu *stats;
1134 struct nft_chain chain;
1135 struct flow_block flow_block;
1138 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1140 return container_of(chain, struct nft_base_chain, chain);
1143 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1145 return chain->flags & NFT_CHAIN_BASE;
1148 int __nft_release_basechain(struct nft_ctx *ctx);
1150 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1153 * struct nft_table - nf_tables table
1155 * @list: used internally
1156 * @chains_ht: chains in the table
1157 * @chains: same, for stable walks
1158 * @sets: sets in the table
1159 * @objects: stateful objects in the table
1160 * @flowtables: flow tables in the table
1161 * @hgenerator: handle generator state
1162 * @handle: table handle
1163 * @use: number of chain references to this table
1164 * @flags: table flag (see enum nft_table_flags)
1165 * @genmask: generation mask
1166 * @afinfo: address family info
1167 * @name: name of the table
1170 struct list_head list;
1171 struct rhltable chains_ht;
1172 struct list_head chains;
1173 struct list_head sets;
1174 struct list_head objects;
1175 struct list_head flowtables;
1188 static inline bool nft_table_has_owner(const struct nft_table *table)
1190 return table->flags & NFT_TABLE_F_OWNER;
1193 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1195 return family == NFPROTO_NETDEV ||
1196 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1199 void nft_register_chain_type(const struct nft_chain_type *);
1200 void nft_unregister_chain_type(const struct nft_chain_type *);
1202 int nft_register_expr(struct nft_expr_type *);
1203 void nft_unregister_expr(struct nft_expr_type *);
1205 int nft_verdict_dump(struct sk_buff *skb, int type,
1206 const struct nft_verdict *v);
1209 * struct nft_object_hash_key - key to lookup nft_object
1211 * @name: name of the stateful object to look up
1212 * @table: table the object belongs to
1214 struct nft_object_hash_key {
1216 const struct nft_table *table;
1220 * struct nft_object - nf_tables stateful object
1222 * @list: table stateful object list node
1223 * @key: keys that identify this object
1224 * @rhlhead: nft_objname_ht node
1225 * @genmask: generation mask
1226 * @use: number of references to this stateful object
1227 * @handle: unique object handle
1228 * @ops: object operations
1229 * @data: object data, layout depends on type
1232 struct list_head list;
1233 struct rhlist_head rhlhead;
1234 struct nft_object_hash_key key;
1240 /* runtime data below here */
1241 const struct nft_object_ops *ops ____cacheline_aligned;
1242 unsigned char data[]
1243 __attribute__((aligned(__alignof__(u64))));
1246 static inline void *nft_obj_data(const struct nft_object *obj)
1248 return (void *)obj->data;
1251 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1253 struct nft_object *nft_obj_lookup(const struct net *net,
1254 const struct nft_table *table,
1255 const struct nlattr *nla, u32 objtype,
1258 void nft_obj_notify(struct net *net, const struct nft_table *table,
1259 struct nft_object *obj, u32 portid, u32 seq,
1260 int event, u16 flags, int family, int report, gfp_t gfp);
1263 * struct nft_object_type - stateful object type
1265 * @select_ops: function to select nft_object_ops
1266 * @ops: default ops, used when no select_ops functions is present
1267 * @list: list node in list of object types
1268 * @type: stateful object numeric type
1269 * @owner: module owner
1270 * @maxattr: maximum netlink attribute
1271 * @policy: netlink attribute policy
1273 struct nft_object_type {
1274 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1275 const struct nlattr * const tb[]);
1276 const struct nft_object_ops *ops;
1277 struct list_head list;
1279 unsigned int maxattr;
1280 struct module *owner;
1281 const struct nla_policy *policy;
1285 * struct nft_object_ops - stateful object operations
1287 * @eval: stateful object evaluation function
1288 * @size: stateful object size
1289 * @init: initialize object from netlink attributes
1290 * @destroy: release existing stateful object
1291 * @dump: netlink dump stateful object
1292 * @update: update stateful object
1294 struct nft_object_ops {
1295 void (*eval)(struct nft_object *obj,
1296 struct nft_regs *regs,
1297 const struct nft_pktinfo *pkt);
1299 int (*init)(const struct nft_ctx *ctx,
1300 const struct nlattr *const tb[],
1301 struct nft_object *obj);
1302 void (*destroy)(const struct nft_ctx *ctx,
1303 struct nft_object *obj);
1304 int (*dump)(struct sk_buff *skb,
1305 struct nft_object *obj,
1307 void (*update)(struct nft_object *obj,
1308 struct nft_object *newobj);
1309 const struct nft_object_type *type;
1312 int nft_register_obj(struct nft_object_type *obj_type);
1313 void nft_unregister_obj(struct nft_object_type *obj_type);
1315 #define NFT_NETDEVICE_MAX 256
1318 * struct nft_flowtable - nf_tables flow table
1320 * @list: flow table list node in table list
1321 * @table: the table the flow table is contained in
1322 * @name: name of this flow table
1323 * @hooknum: hook number
1324 * @ops_len: number of hooks in array
1325 * @genmask: generation mask
1326 * @use: number of references to this flow table
1327 * @handle: unique object handle
1328 * @dev_name: array of device names
1329 * @data: rhashtable and garbage collector
1330 * @ops: array of hooks
1332 struct nft_flowtable {
1333 struct list_head list;
1334 struct nft_table *table;
1341 /* runtime data below here */
1342 struct list_head hook_list ____cacheline_aligned;
1343 struct nf_flowtable data;
1346 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1347 const struct nlattr *nla,
1350 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1351 struct nft_flowtable *flowtable,
1352 enum nft_trans_phase phase);
1354 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1355 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1358 * struct nft_traceinfo - nft tracing information and state
1360 * @trace: other struct members are initialised
1361 * @nf_trace: copy of skb->nf_trace before rule evaluation
1362 * @type: event type (enum nft_trace_types)
1363 * @skbid: hash of skb to be used as trace id
1364 * @packet_dumped: packet headers sent in a previous traceinfo message
1365 * @pkt: pktinfo currently processed
1366 * @basechain: base chain currently processed
1367 * @chain: chain currently processed
1368 * @rule: rule that was evaluated
1369 * @verdict: verdict given by rule
1371 struct nft_traceinfo {
1375 enum nft_trace_types type:8;
1377 const struct nft_pktinfo *pkt;
1378 const struct nft_base_chain *basechain;
1379 const struct nft_chain *chain;
1380 const struct nft_rule_dp *rule;
1381 const struct nft_verdict *verdict;
1384 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1385 const struct nft_verdict *verdict,
1386 const struct nft_chain *basechain);
1388 void nft_trace_notify(struct nft_traceinfo *info);
1390 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1391 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1393 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1394 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1396 #define MODULE_ALIAS_NFT_EXPR(name) \
1397 MODULE_ALIAS("nft-expr-" name)
1399 #define MODULE_ALIAS_NFT_OBJ(type) \
1400 MODULE_ALIAS("nft-obj-" __stringify(type))
1402 #if IS_ENABLED(CONFIG_NF_TABLES)
1405 * The gencursor defines two generations, the currently active and the
1406 * next one. Objects contain a bitmask of 2 bits specifying the generations
1407 * they're active in. A set bit means they're inactive in the generation
1408 * represented by that bit.
1410 * New objects start out as inactive in the current and active in the
1411 * next generation. When committing the ruleset the bitmask is cleared,
1412 * meaning they're active in all generations. When removing an object,
1413 * it is set inactive in the next generation. After committing the ruleset,
1414 * the objects are removed.
1416 static inline unsigned int nft_gencursor_next(const struct net *net)
1418 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1421 static inline u8 nft_genmask_next(const struct net *net)
1423 return 1 << nft_gencursor_next(net);
1426 static inline u8 nft_genmask_cur(const struct net *net)
1428 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1429 return 1 << READ_ONCE(net->nft.gencursor);
1432 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1435 * Generic transaction helpers
1438 /* Check if this object is currently active. */
1439 #define nft_is_active(__net, __obj) \
1440 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1442 /* Check if this object is active in the next generation. */
1443 #define nft_is_active_next(__net, __obj) \
1444 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1446 /* This object becomes active in the next generation. */
1447 #define nft_activate_next(__net, __obj) \
1448 (__obj)->genmask = nft_genmask_cur(__net)
1450 /* This object becomes inactive in the next generation. */
1451 #define nft_deactivate_next(__net, __obj) \
1452 (__obj)->genmask = nft_genmask_next(__net)
1454 /* After committing the ruleset, clear the stale generation bit. */
1455 #define nft_clear(__net, __obj) \
1456 (__obj)->genmask &= ~nft_genmask_next(__net)
1457 #define nft_active_genmask(__obj, __genmask) \
1458 !((__obj)->genmask & __genmask)
1461 * Set element transaction helpers
1464 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1467 return !(ext->genmask & genmask);
1470 static inline void nft_set_elem_change_active(const struct net *net,
1471 const struct nft_set *set,
1472 struct nft_set_ext *ext)
1474 ext->genmask ^= nft_genmask_next(net);
1477 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1480 * We use a free bit in the genmask field to indicate the element
1481 * is busy, meaning it is currently being processed either by
1482 * the netlink API or GC.
1484 * Even though the genmask is only a single byte wide, this works
1485 * because the extension structure if fully constant once initialized,
1486 * so there are no non-atomic write accesses unless it is already
1489 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1491 #if defined(__LITTLE_ENDIAN_BITFIELD)
1492 #define NFT_SET_ELEM_BUSY_BIT 2
1493 #elif defined(__BIG_ENDIAN_BITFIELD)
1494 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1499 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1501 unsigned long *word = (unsigned long *)ext;
1503 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1504 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1507 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1509 unsigned long *word = (unsigned long *)ext;
1511 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1515 * struct nft_trans - nf_tables object update in transaction
1517 * @list: used internally
1518 * @msg_type: message type
1519 * @put_net: ctx->net needs to be put
1520 * @ctx: transaction context
1521 * @data: internal information related to the transaction
1524 struct list_head list;
1531 struct nft_trans_rule {
1532 struct nft_rule *rule;
1533 struct nft_flow_rule *flow;
1537 #define nft_trans_rule(trans) \
1538 (((struct nft_trans_rule *)trans->data)->rule)
1539 #define nft_trans_flow_rule(trans) \
1540 (((struct nft_trans_rule *)trans->data)->flow)
1541 #define nft_trans_rule_id(trans) \
1542 (((struct nft_trans_rule *)trans->data)->rule_id)
1544 struct nft_trans_set {
1545 struct nft_set *set;
1550 #define nft_trans_set(trans) \
1551 (((struct nft_trans_set *)trans->data)->set)
1552 #define nft_trans_set_id(trans) \
1553 (((struct nft_trans_set *)trans->data)->set_id)
1554 #define nft_trans_set_bound(trans) \
1555 (((struct nft_trans_set *)trans->data)->bound)
1557 struct nft_trans_chain {
1560 struct nft_stats __percpu *stats;
1565 #define nft_trans_chain_update(trans) \
1566 (((struct nft_trans_chain *)trans->data)->update)
1567 #define nft_trans_chain_name(trans) \
1568 (((struct nft_trans_chain *)trans->data)->name)
1569 #define nft_trans_chain_stats(trans) \
1570 (((struct nft_trans_chain *)trans->data)->stats)
1571 #define nft_trans_chain_policy(trans) \
1572 (((struct nft_trans_chain *)trans->data)->policy)
1573 #define nft_trans_chain_id(trans) \
1574 (((struct nft_trans_chain *)trans->data)->chain_id)
1576 struct nft_trans_table {
1580 #define nft_trans_table_update(trans) \
1581 (((struct nft_trans_table *)trans->data)->update)
1583 struct nft_trans_elem {
1584 struct nft_set *set;
1585 struct nft_set_elem elem;
1589 #define nft_trans_elem_set(trans) \
1590 (((struct nft_trans_elem *)trans->data)->set)
1591 #define nft_trans_elem(trans) \
1592 (((struct nft_trans_elem *)trans->data)->elem)
1593 #define nft_trans_elem_set_bound(trans) \
1594 (((struct nft_trans_elem *)trans->data)->bound)
1596 struct nft_trans_obj {
1597 struct nft_object *obj;
1598 struct nft_object *newobj;
1602 #define nft_trans_obj(trans) \
1603 (((struct nft_trans_obj *)trans->data)->obj)
1604 #define nft_trans_obj_newobj(trans) \
1605 (((struct nft_trans_obj *)trans->data)->newobj)
1606 #define nft_trans_obj_update(trans) \
1607 (((struct nft_trans_obj *)trans->data)->update)
1609 struct nft_trans_flowtable {
1610 struct nft_flowtable *flowtable;
1612 struct list_head hook_list;
1616 #define nft_trans_flowtable(trans) \
1617 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1618 #define nft_trans_flowtable_update(trans) \
1619 (((struct nft_trans_flowtable *)trans->data)->update)
1620 #define nft_trans_flowtable_hooks(trans) \
1621 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1622 #define nft_trans_flowtable_flags(trans) \
1623 (((struct nft_trans_flowtable *)trans->data)->flags)
1625 int __init nft_chain_filter_init(void);
1626 void nft_chain_filter_fini(void);
1628 void __init nft_chain_route_init(void);
1629 void nft_chain_route_fini(void);
1631 void nf_tables_trans_destroy_flush_work(void);
1633 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1634 __be64 nf_jiffies64_to_msecs(u64 input);
1636 #ifdef CONFIG_MODULES
1637 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1639 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1642 struct nftables_pernet {
1643 struct list_head tables;
1644 struct list_head commit_list;
1645 struct list_head module_list;
1646 struct list_head notify_list;
1647 struct mutex commit_mutex;
1648 unsigned int base_seq;
1652 extern unsigned int nf_tables_net_id;
1654 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1656 return net_generic(net, nf_tables_net_id);
1659 #define __NFT_REDUCE_READONLY 1UL
1660 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1662 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1664 return expr->ops->reduce == NFT_REDUCE_READONLY;
1667 void nft_reg_track_update(struct nft_regs_track *track,
1668 const struct nft_expr *expr, u8 dreg, u8 len);
1669 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1670 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1672 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1673 const struct nft_expr *expr, u8 dreg)
1675 return track->regs[dreg].selector &&
1676 track->regs[dreg].selector->ops == expr->ops &&
1677 track->regs[dreg].num_reg == 0;
1680 #endif /* _NET_NF_TABLES_H */