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 u16 nft_reg_load16(const u32 *sreg)
165 static inline void nft_reg_store64(u32 *dreg, u64 val)
167 put_unaligned(val, (u64 *)dreg);
170 static inline u64 nft_reg_load64(const u32 *sreg)
172 return get_unaligned((u64 *)sreg);
175 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
178 if (len % NFT_REG32_SIZE)
179 dst[len / NFT_REG32_SIZE] = 0;
180 memcpy(dst, src, len);
184 * struct nft_ctx - nf_tables rule/set context
186 * @net: net namespace
187 * @table: the table the chain is contained in
188 * @chain: the chain the rule is contained in
189 * @nla: netlink attributes
190 * @portid: netlink portID of the original message
191 * @seq: netlink sequence number
192 * @family: protocol family
193 * @level: depth of the chains
194 * @report: notify via unicast netlink message
198 struct nft_table *table;
199 struct nft_chain *chain;
200 const struct nlattr * const *nla;
209 struct nft_data_desc {
210 enum nft_data_types type;
214 int nft_data_init(const struct nft_ctx *ctx,
215 struct nft_data *data, unsigned int size,
216 struct nft_data_desc *desc, const struct nlattr *nla);
217 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
218 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
219 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
220 enum nft_data_types type, unsigned int len);
222 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
224 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
227 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
229 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
232 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
233 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
235 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
236 int nft_parse_register_store(const struct nft_ctx *ctx,
237 const struct nlattr *attr, u8 *dreg,
238 const struct nft_data *data,
239 enum nft_data_types type, unsigned int len);
242 * struct nft_userdata - user defined data associated with an object
244 * @len: length of the data
247 * The presence of user data is indicated in an object specific fashion,
248 * so a length of zero can't occur and the value "len" indicates data
251 struct nft_userdata {
253 unsigned char data[];
257 * struct nft_set_elem - generic representation of set elements
260 * @key_end: closing element key
261 * @priv: element private data and extensions
263 struct nft_set_elem {
265 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
269 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
273 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
280 struct nft_set_iter {
285 int (*fn)(const struct nft_ctx *ctx,
287 const struct nft_set_iter *iter,
288 struct nft_set_elem *elem);
292 * struct nft_set_desc - description of set elements
296 * @size: number of set elements
297 * @field_len: length of each field in concatenation, bytes
298 * @field_count: number of concatenated fields in element
299 * @expr: set must support for expressions
301 struct nft_set_desc {
305 u8 field_len[NFT_REG32_COUNT];
311 * enum nft_set_class - performance class
313 * @NFT_LOOKUP_O_1: constant, O(1)
314 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
315 * @NFT_LOOKUP_O_N: linear, O(N)
319 NFT_SET_CLASS_O_LOG_N,
324 * struct nft_set_estimate - estimation of memory and performance
327 * @size: required memory
328 * @lookup: lookup performance class
329 * @space: memory class
331 struct nft_set_estimate {
333 enum nft_set_class lookup;
334 enum nft_set_class space;
337 #define NFT_EXPR_MAXATTR 16
338 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
339 ALIGN(size, __alignof__(struct nft_expr)))
342 * struct nft_expr - nf_tables expression
344 * @ops: expression ops
345 * @data: expression private data
348 const struct nft_expr_ops *ops;
350 __attribute__((aligned(__alignof__(u64))));
353 static inline void *nft_expr_priv(const struct nft_expr *expr)
355 return (void *)expr->data;
358 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
359 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
360 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
361 const struct nft_expr *expr);
362 bool nft_expr_reduce_bitwise(struct nft_regs_track *track,
363 const struct nft_expr *expr);
368 * struct nft_set_ops - nf_tables set operations
370 * @lookup: look up an element within the set
371 * @update: update an element if exists, add it if doesn't exist
372 * @delete: delete an element
373 * @insert: insert new element into set
374 * @activate: activate new element in the next generation
375 * @deactivate: lookup for element and deactivate it in the next generation
376 * @flush: deactivate element in the next generation
377 * @remove: remove element from set
378 * @walk: iterate over all set elements
379 * @get: get set elements
380 * @privsize: function to return size of set private data
381 * @init: initialize private data of new set instance
382 * @destroy: destroy private data of set instance
383 * @elemsize: element private size
385 * Operations lookup, update and delete have simpler interfaces, are faster
386 * and currently only used in the packet path. All the rest are slower,
387 * control plane functions.
390 bool (*lookup)(const struct net *net,
391 const struct nft_set *set,
393 const struct nft_set_ext **ext);
394 bool (*update)(struct nft_set *set,
396 void *(*new)(struct nft_set *,
397 const struct nft_expr *,
399 const struct nft_expr *expr,
400 struct nft_regs *regs,
401 const struct nft_set_ext **ext);
402 bool (*delete)(const struct nft_set *set,
405 int (*insert)(const struct net *net,
406 const struct nft_set *set,
407 const struct nft_set_elem *elem,
408 struct nft_set_ext **ext);
409 void (*activate)(const struct net *net,
410 const struct nft_set *set,
411 const struct nft_set_elem *elem);
412 void * (*deactivate)(const struct net *net,
413 const struct nft_set *set,
414 const struct nft_set_elem *elem);
415 bool (*flush)(const struct net *net,
416 const struct nft_set *set,
418 void (*remove)(const struct net *net,
419 const struct nft_set *set,
420 const struct nft_set_elem *elem);
421 void (*walk)(const struct nft_ctx *ctx,
423 struct nft_set_iter *iter);
424 void * (*get)(const struct net *net,
425 const struct nft_set *set,
426 const struct nft_set_elem *elem,
429 u64 (*privsize)(const struct nlattr * const nla[],
430 const struct nft_set_desc *desc);
431 bool (*estimate)(const struct nft_set_desc *desc,
433 struct nft_set_estimate *est);
434 int (*init)(const struct nft_set *set,
435 const struct nft_set_desc *desc,
436 const struct nlattr * const nla[]);
437 void (*destroy)(const struct nft_set *set);
438 void (*gc_init)(const struct nft_set *set);
440 unsigned int elemsize;
444 * struct nft_set_type - nf_tables set type
446 * @ops: set ops for this type
447 * @features: features supported by the implementation
449 struct nft_set_type {
450 const struct nft_set_ops ops;
453 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
455 struct nft_set_elem_expr {
458 __attribute__((aligned(__alignof__(struct nft_expr))));
461 #define nft_setelem_expr_at(__elem_expr, __offset) \
462 ((struct nft_expr *)&__elem_expr->data[__offset])
464 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
465 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
466 __size < (__elem_expr)->size; \
467 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
469 #define NFT_SET_EXPR_MAX 2
472 * struct nft_set - nf_tables set instance
474 * @list: table set list node
475 * @bindings: list of set bindings
476 * @table: table this set belongs to
477 * @net: netnamespace this set belongs to
478 * @name: name of the set
479 * @handle: unique handle of the set
480 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
481 * @dtype: data type (verdict or numeric type defined by userspace)
482 * @objtype: object type (see NFT_OBJECT_* definitions)
483 * @size: maximum set size
484 * @field_len: length of each field in concatenation, bytes
485 * @field_count: number of concatenated fields in element
486 * @use: number of rules references to this set
487 * @nelems: number of elements
488 * @ndeact: number of deactivated elements queued for removal
489 * @timeout: default timeout value in jiffies
490 * @gc_int: garbage collection interval in msecs
491 * @policy: set parameterization (see enum nft_set_policies)
492 * @udlen: user data length
494 * @expr: stateful expression
497 * @genmask: generation mask
500 * @data: private set data
503 struct list_head list;
504 struct list_head bindings;
505 struct nft_table *table;
513 u8 field_len[NFT_REG32_COUNT];
522 unsigned char *udata;
523 /* runtime data below here */
524 const struct nft_set_ops *ops ____cacheline_aligned;
530 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
531 struct list_head catchall_list;
533 __attribute__((aligned(__alignof__(u64))));
536 static inline bool nft_set_is_anonymous(const struct nft_set *set)
538 return set->flags & NFT_SET_ANONYMOUS;
541 static inline void *nft_set_priv(const struct nft_set *set)
543 return (void *)set->data;
546 static inline struct nft_set *nft_set_container_of(const void *priv)
548 return (void *)priv - offsetof(struct nft_set, data);
551 struct nft_set *nft_set_lookup_global(const struct net *net,
552 const struct nft_table *table,
553 const struct nlattr *nla_set_name,
554 const struct nlattr *nla_set_id,
557 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
558 const struct nft_set *set);
559 void *nft_set_catchall_gc(const struct nft_set *set);
561 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
563 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
567 * struct nft_set_binding - nf_tables set binding
569 * @list: set bindings list node
570 * @chain: chain containing the rule bound to the set
571 * @flags: set action flags
573 * A set binding contains all information necessary for validation
574 * of new elements added to a bound set.
576 struct nft_set_binding {
577 struct list_head list;
578 const struct nft_chain *chain;
582 enum nft_trans_phase;
583 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
584 struct nft_set_binding *binding,
585 enum nft_trans_phase phase);
586 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
587 struct nft_set_binding *binding);
588 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
591 * enum nft_set_extensions - set extension type IDs
593 * @NFT_SET_EXT_KEY: element key
594 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
595 * @NFT_SET_EXT_DATA: mapping data
596 * @NFT_SET_EXT_FLAGS: element flags
597 * @NFT_SET_EXT_TIMEOUT: element timeout
598 * @NFT_SET_EXT_EXPIRATION: element expiration time
599 * @NFT_SET_EXT_USERDATA: user data associated with the element
600 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
601 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
602 * @NFT_SET_EXT_NUM: number of extension types
604 enum nft_set_extensions {
610 NFT_SET_EXT_EXPIRATION,
611 NFT_SET_EXT_USERDATA,
612 NFT_SET_EXT_EXPRESSIONS,
618 * struct nft_set_ext_type - set extension type
620 * @len: fixed part length of the extension
621 * @align: alignment requirements of the extension
623 struct nft_set_ext_type {
628 extern const struct nft_set_ext_type nft_set_ext_types[];
631 * struct nft_set_ext_tmpl - set extension template
633 * @len: length of extension area
634 * @offset: offsets of individual extension types
636 struct nft_set_ext_tmpl {
638 u8 offset[NFT_SET_EXT_NUM];
642 * struct nft_set_ext - set extensions
644 * @genmask: generation mask
645 * @offset: offsets of individual extension types
646 * @data: beginning of extension data
650 u8 offset[NFT_SET_EXT_NUM];
654 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
656 memset(tmpl, 0, sizeof(*tmpl));
657 tmpl->len = sizeof(struct nft_set_ext);
660 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
663 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
664 BUG_ON(tmpl->len > U8_MAX);
665 tmpl->offset[id] = tmpl->len;
666 tmpl->len += nft_set_ext_types[id].len + len;
669 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
671 nft_set_ext_add_length(tmpl, id, 0);
674 static inline void nft_set_ext_init(struct nft_set_ext *ext,
675 const struct nft_set_ext_tmpl *tmpl)
677 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
680 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
682 return !!ext->offset[id];
685 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
687 return ext && __nft_set_ext_exists(ext, id);
690 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
692 return (void *)ext + ext->offset[id];
695 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
697 return nft_set_ext(ext, NFT_SET_EXT_KEY);
700 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
702 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
705 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
707 return nft_set_ext(ext, NFT_SET_EXT_DATA);
710 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
712 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
715 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
717 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
720 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
722 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
725 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
727 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
730 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
732 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
735 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
737 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
738 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
741 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
744 return elem + set->ops->elemsize;
747 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
749 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
752 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
753 const struct nft_set *set,
754 const struct nlattr *attr);
756 void *nft_set_elem_init(const struct nft_set *set,
757 const struct nft_set_ext_tmpl *tmpl,
758 const u32 *key, const u32 *key_end, const u32 *data,
759 u64 timeout, u64 expiration, gfp_t gfp);
760 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
761 struct nft_expr *expr_array[]);
762 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
766 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
769 * @set: set the elements belong to
770 * @cnt: count of elements
772 struct nft_set_gc_batch_head {
774 const struct nft_set *set;
778 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
779 sizeof(struct nft_set_gc_batch_head)) / \
783 * struct nft_set_gc_batch - nf_tables set garbage collection batch
785 * @head: GC batch head
786 * @elems: garbage collection elements
788 struct nft_set_gc_batch {
789 struct nft_set_gc_batch_head head;
790 void *elems[NFT_SET_GC_BATCH_SIZE];
793 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
795 void nft_set_gc_batch_release(struct rcu_head *rcu);
797 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
800 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
803 static inline struct nft_set_gc_batch *
804 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
808 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
810 nft_set_gc_batch_complete(gcb);
812 return nft_set_gc_batch_alloc(set, gfp);
815 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
818 gcb->elems[gcb->head.cnt++] = elem;
823 * struct nft_expr_type - nf_tables expression type
825 * @select_ops: function to select nft_expr_ops
826 * @release_ops: release nft_expr_ops
827 * @ops: default ops, used when no select_ops functions is present
828 * @list: used internally
830 * @owner: module reference
831 * @policy: netlink attribute policy
832 * @maxattr: highest netlink attribute number
833 * @family: address family for AF-specific types
834 * @flags: expression type flags
836 struct nft_expr_type {
837 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
838 const struct nlattr * const tb[]);
839 void (*release_ops)(const struct nft_expr_ops *ops);
840 const struct nft_expr_ops *ops;
841 struct list_head list;
843 struct module *owner;
844 const struct nla_policy *policy;
845 unsigned int maxattr;
850 #define NFT_EXPR_STATEFUL 0x1
851 #define NFT_EXPR_GC 0x2
853 enum nft_trans_phase {
860 struct nft_flow_rule;
861 struct nft_offload_ctx;
864 * struct nft_expr_ops - nf_tables expression operations
866 * @eval: Expression evaluation function
867 * @size: full expression size, including private data size
868 * @init: initialization function
869 * @activate: activate expression in the next generation
870 * @deactivate: deactivate expression in next generation
871 * @destroy: destruction function, called after synchronize_rcu
872 * @dump: function to dump parameters
873 * @type: expression type
874 * @validate: validate expression, called during loop detection
875 * @data: extra data to attach to this expression operation
877 struct nft_expr_ops {
878 void (*eval)(const struct nft_expr *expr,
879 struct nft_regs *regs,
880 const struct nft_pktinfo *pkt);
881 int (*clone)(struct nft_expr *dst,
882 const struct nft_expr *src);
885 int (*init)(const struct nft_ctx *ctx,
886 const struct nft_expr *expr,
887 const struct nlattr * const tb[]);
888 void (*activate)(const struct nft_ctx *ctx,
889 const struct nft_expr *expr);
890 void (*deactivate)(const struct nft_ctx *ctx,
891 const struct nft_expr *expr,
892 enum nft_trans_phase phase);
893 void (*destroy)(const struct nft_ctx *ctx,
894 const struct nft_expr *expr);
895 void (*destroy_clone)(const struct nft_ctx *ctx,
896 const struct nft_expr *expr);
897 int (*dump)(struct sk_buff *skb,
898 const struct nft_expr *expr);
899 int (*validate)(const struct nft_ctx *ctx,
900 const struct nft_expr *expr,
901 const struct nft_data **data);
902 bool (*reduce)(struct nft_regs_track *track,
903 const struct nft_expr *expr);
904 bool (*gc)(struct net *net,
905 const struct nft_expr *expr);
906 int (*offload)(struct nft_offload_ctx *ctx,
907 struct nft_flow_rule *flow,
908 const struct nft_expr *expr);
909 bool (*offload_action)(const struct nft_expr *expr);
910 void (*offload_stats)(struct nft_expr *expr,
911 const struct flow_stats *stats);
912 const struct nft_expr_type *type;
917 * struct nft_rule - nf_tables rule
919 * @list: used internally
920 * @handle: rule handle
921 * @genmask: generation mask
922 * @dlen: length of expression data
923 * @udata: user data is appended to the rule
924 * @data: expression data
927 struct list_head list;
933 __attribute__((aligned(__alignof__(struct nft_expr))));
936 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
938 return (struct nft_expr *)&rule->data[0];
941 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
943 return ((void *)expr) + expr->ops->size;
946 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
948 return (struct nft_expr *)&rule->data[rule->dlen];
951 static inline bool nft_expr_more(const struct nft_rule *rule,
952 const struct nft_expr *expr)
954 return expr != nft_expr_last(rule) && expr->ops;
957 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
959 return (void *)&rule->data[rule->dlen];
962 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
964 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
965 struct nft_regs *regs,
966 const struct nft_pktinfo *pkt)
968 struct nft_set_elem_expr *elem_expr;
969 struct nft_expr *expr;
972 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
973 elem_expr = nft_set_ext_expr(ext);
974 nft_setelem_expr_foreach(expr, elem_expr, size) {
975 expr->ops->eval(expr, regs, pkt);
976 if (regs->verdict.code == NFT_BREAK)
983 * The last pointer isn't really necessary, but the compiler isn't able to
984 * determine that the result of nft_expr_last() is always the same since it
985 * can't assume that the dlen value wasn't changed within calls in the loop.
987 #define nft_rule_for_each_expr(expr, last, rule) \
988 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
990 (expr) = nft_expr_next(expr))
992 #define NFT_CHAIN_POLICY_UNSET U8_MAX
997 handle:42; /* for tracing */
999 __attribute__((aligned(__alignof__(struct nft_expr))));
1002 struct nft_rule_blob {
1004 unsigned char data[]
1005 __attribute__((aligned(__alignof__(struct nft_rule_dp))));
1009 * struct nft_chain - nf_tables chain
1011 * @rules: list of rules in the chain
1012 * @list: used internally
1013 * @rhlhead: used internally
1014 * @table: table that this chain belongs to
1015 * @handle: chain handle
1016 * @use: number of jump references to this chain
1017 * @flags: bitmask of enum nft_chain_flags
1018 * @name: name of the chain
1021 struct nft_rule_blob __rcu *blob_gen_0;
1022 struct nft_rule_blob __rcu *blob_gen_1;
1023 struct list_head rules;
1024 struct list_head list;
1025 struct rhlist_head rhlhead;
1026 struct nft_table *table;
1036 /* Only used during control plane commit phase: */
1037 struct nft_rule_blob *blob_next;
1040 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1042 enum nft_chain_types {
1043 NFT_CHAIN_T_DEFAULT = 0,
1050 * struct nft_chain_type - nf_tables chain type info
1052 * @name: name of the type
1053 * @type: numeric identifier
1054 * @family: address family
1055 * @owner: module owner
1056 * @hook_mask: mask of valid hooks
1057 * @hooks: array of hook functions
1058 * @ops_register: base chain register function
1059 * @ops_unregister: base chain unregister function
1061 struct nft_chain_type {
1063 enum nft_chain_types type;
1065 struct module *owner;
1066 unsigned int hook_mask;
1067 nf_hookfn *hooks[NFT_MAX_HOOKS];
1068 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1069 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1072 int nft_chain_validate_dependency(const struct nft_chain *chain,
1073 enum nft_chain_types type);
1074 int nft_chain_validate_hooks(const struct nft_chain *chain,
1075 unsigned int hook_flags);
1077 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1079 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1082 void nft_chain_del(struct nft_chain *chain);
1083 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1088 struct u64_stats_sync syncp;
1092 struct list_head list;
1093 struct nf_hook_ops ops;
1094 struct rcu_head rcu;
1098 * struct nft_base_chain - nf_tables base chain
1100 * @ops: netfilter hook ops
1101 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1103 * @policy: default policy
1104 * @stats: per-cpu chain stats
1106 * @flow_block: flow block (for hardware offload)
1108 struct nft_base_chain {
1109 struct nf_hook_ops ops;
1110 struct list_head hook_list;
1111 const struct nft_chain_type *type;
1114 struct nft_stats __percpu *stats;
1115 struct nft_chain chain;
1116 struct flow_block flow_block;
1119 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1121 return container_of(chain, struct nft_base_chain, chain);
1124 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1126 return chain->flags & NFT_CHAIN_BASE;
1129 int __nft_release_basechain(struct nft_ctx *ctx);
1131 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1134 * struct nft_table - nf_tables table
1136 * @list: used internally
1137 * @chains_ht: chains in the table
1138 * @chains: same, for stable walks
1139 * @sets: sets in the table
1140 * @objects: stateful objects in the table
1141 * @flowtables: flow tables in the table
1142 * @hgenerator: handle generator state
1143 * @handle: table handle
1144 * @use: number of chain references to this table
1145 * @flags: table flag (see enum nft_table_flags)
1146 * @genmask: generation mask
1147 * @afinfo: address family info
1148 * @name: name of the table
1151 struct list_head list;
1152 struct rhltable chains_ht;
1153 struct list_head chains;
1154 struct list_head sets;
1155 struct list_head objects;
1156 struct list_head flowtables;
1169 static inline bool nft_table_has_owner(const struct nft_table *table)
1171 return table->flags & NFT_TABLE_F_OWNER;
1174 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1176 return family == NFPROTO_NETDEV ||
1177 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1180 void nft_register_chain_type(const struct nft_chain_type *);
1181 void nft_unregister_chain_type(const struct nft_chain_type *);
1183 int nft_register_expr(struct nft_expr_type *);
1184 void nft_unregister_expr(struct nft_expr_type *);
1186 int nft_verdict_dump(struct sk_buff *skb, int type,
1187 const struct nft_verdict *v);
1190 * struct nft_object_hash_key - key to lookup nft_object
1192 * @name: name of the stateful object to look up
1193 * @table: table the object belongs to
1195 struct nft_object_hash_key {
1197 const struct nft_table *table;
1201 * struct nft_object - nf_tables stateful object
1203 * @list: table stateful object list node
1204 * @key: keys that identify this object
1205 * @rhlhead: nft_objname_ht node
1206 * @genmask: generation mask
1207 * @use: number of references to this stateful object
1208 * @handle: unique object handle
1209 * @ops: object operations
1210 * @data: object data, layout depends on type
1213 struct list_head list;
1214 struct rhlist_head rhlhead;
1215 struct nft_object_hash_key key;
1221 /* runtime data below here */
1222 const struct nft_object_ops *ops ____cacheline_aligned;
1223 unsigned char data[]
1224 __attribute__((aligned(__alignof__(u64))));
1227 static inline void *nft_obj_data(const struct nft_object *obj)
1229 return (void *)obj->data;
1232 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1234 struct nft_object *nft_obj_lookup(const struct net *net,
1235 const struct nft_table *table,
1236 const struct nlattr *nla, u32 objtype,
1239 void nft_obj_notify(struct net *net, const struct nft_table *table,
1240 struct nft_object *obj, u32 portid, u32 seq,
1241 int event, u16 flags, int family, int report, gfp_t gfp);
1244 * struct nft_object_type - stateful object type
1246 * @select_ops: function to select nft_object_ops
1247 * @ops: default ops, used when no select_ops functions is present
1248 * @list: list node in list of object types
1249 * @type: stateful object numeric type
1250 * @owner: module owner
1251 * @maxattr: maximum netlink attribute
1252 * @policy: netlink attribute policy
1254 struct nft_object_type {
1255 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1256 const struct nlattr * const tb[]);
1257 const struct nft_object_ops *ops;
1258 struct list_head list;
1260 unsigned int maxattr;
1261 struct module *owner;
1262 const struct nla_policy *policy;
1266 * struct nft_object_ops - stateful object operations
1268 * @eval: stateful object evaluation function
1269 * @size: stateful object size
1270 * @init: initialize object from netlink attributes
1271 * @destroy: release existing stateful object
1272 * @dump: netlink dump stateful object
1273 * @update: update stateful object
1275 struct nft_object_ops {
1276 void (*eval)(struct nft_object *obj,
1277 struct nft_regs *regs,
1278 const struct nft_pktinfo *pkt);
1280 int (*init)(const struct nft_ctx *ctx,
1281 const struct nlattr *const tb[],
1282 struct nft_object *obj);
1283 void (*destroy)(const struct nft_ctx *ctx,
1284 struct nft_object *obj);
1285 int (*dump)(struct sk_buff *skb,
1286 struct nft_object *obj,
1288 void (*update)(struct nft_object *obj,
1289 struct nft_object *newobj);
1290 const struct nft_object_type *type;
1293 int nft_register_obj(struct nft_object_type *obj_type);
1294 void nft_unregister_obj(struct nft_object_type *obj_type);
1296 #define NFT_NETDEVICE_MAX 256
1299 * struct nft_flowtable - nf_tables flow table
1301 * @list: flow table list node in table list
1302 * @table: the table the flow table is contained in
1303 * @name: name of this flow table
1304 * @hooknum: hook number
1305 * @ops_len: number of hooks in array
1306 * @genmask: generation mask
1307 * @use: number of references to this flow table
1308 * @handle: unique object handle
1309 * @dev_name: array of device names
1310 * @data: rhashtable and garbage collector
1311 * @ops: array of hooks
1313 struct nft_flowtable {
1314 struct list_head list;
1315 struct nft_table *table;
1322 /* runtime data below here */
1323 struct list_head hook_list ____cacheline_aligned;
1324 struct nf_flowtable data;
1327 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1328 const struct nlattr *nla,
1331 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1332 struct nft_flowtable *flowtable,
1333 enum nft_trans_phase phase);
1335 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1336 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1339 * struct nft_traceinfo - nft tracing information and state
1341 * @pkt: pktinfo currently processed
1342 * @basechain: base chain currently processed
1343 * @chain: chain currently processed
1344 * @rule: rule that was evaluated
1345 * @verdict: verdict given by rule
1346 * @type: event type (enum nft_trace_types)
1347 * @packet_dumped: packet headers sent in a previous traceinfo message
1348 * @trace: other struct members are initialised
1350 struct nft_traceinfo {
1351 const struct nft_pktinfo *pkt;
1352 const struct nft_base_chain *basechain;
1353 const struct nft_chain *chain;
1354 const struct nft_rule_dp *rule;
1355 const struct nft_verdict *verdict;
1356 enum nft_trace_types type;
1361 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1362 const struct nft_verdict *verdict,
1363 const struct nft_chain *basechain);
1365 void nft_trace_notify(struct nft_traceinfo *info);
1367 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1368 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1370 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1371 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1373 #define MODULE_ALIAS_NFT_EXPR(name) \
1374 MODULE_ALIAS("nft-expr-" name)
1376 #define MODULE_ALIAS_NFT_OBJ(type) \
1377 MODULE_ALIAS("nft-obj-" __stringify(type))
1379 #if IS_ENABLED(CONFIG_NF_TABLES)
1382 * The gencursor defines two generations, the currently active and the
1383 * next one. Objects contain a bitmask of 2 bits specifying the generations
1384 * they're active in. A set bit means they're inactive in the generation
1385 * represented by that bit.
1387 * New objects start out as inactive in the current and active in the
1388 * next generation. When committing the ruleset the bitmask is cleared,
1389 * meaning they're active in all generations. When removing an object,
1390 * it is set inactive in the next generation. After committing the ruleset,
1391 * the objects are removed.
1393 static inline unsigned int nft_gencursor_next(const struct net *net)
1395 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1398 static inline u8 nft_genmask_next(const struct net *net)
1400 return 1 << nft_gencursor_next(net);
1403 static inline u8 nft_genmask_cur(const struct net *net)
1405 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1406 return 1 << READ_ONCE(net->nft.gencursor);
1409 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1412 * Generic transaction helpers
1415 /* Check if this object is currently active. */
1416 #define nft_is_active(__net, __obj) \
1417 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1419 /* Check if this object is active in the next generation. */
1420 #define nft_is_active_next(__net, __obj) \
1421 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1423 /* This object becomes active in the next generation. */
1424 #define nft_activate_next(__net, __obj) \
1425 (__obj)->genmask = nft_genmask_cur(__net)
1427 /* This object becomes inactive in the next generation. */
1428 #define nft_deactivate_next(__net, __obj) \
1429 (__obj)->genmask = nft_genmask_next(__net)
1431 /* After committing the ruleset, clear the stale generation bit. */
1432 #define nft_clear(__net, __obj) \
1433 (__obj)->genmask &= ~nft_genmask_next(__net)
1434 #define nft_active_genmask(__obj, __genmask) \
1435 !((__obj)->genmask & __genmask)
1438 * Set element transaction helpers
1441 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1444 return !(ext->genmask & genmask);
1447 static inline void nft_set_elem_change_active(const struct net *net,
1448 const struct nft_set *set,
1449 struct nft_set_ext *ext)
1451 ext->genmask ^= nft_genmask_next(net);
1454 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1457 * We use a free bit in the genmask field to indicate the element
1458 * is busy, meaning it is currently being processed either by
1459 * the netlink API or GC.
1461 * Even though the genmask is only a single byte wide, this works
1462 * because the extension structure if fully constant once initialized,
1463 * so there are no non-atomic write accesses unless it is already
1466 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1468 #if defined(__LITTLE_ENDIAN_BITFIELD)
1469 #define NFT_SET_ELEM_BUSY_BIT 2
1470 #elif defined(__BIG_ENDIAN_BITFIELD)
1471 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1476 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1478 unsigned long *word = (unsigned long *)ext;
1480 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1481 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1484 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1486 unsigned long *word = (unsigned long *)ext;
1488 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1492 * struct nft_trans - nf_tables object update in transaction
1494 * @list: used internally
1495 * @msg_type: message type
1496 * @put_net: ctx->net needs to be put
1497 * @ctx: transaction context
1498 * @data: internal information related to the transaction
1501 struct list_head list;
1508 struct nft_trans_rule {
1509 struct nft_rule *rule;
1510 struct nft_flow_rule *flow;
1514 #define nft_trans_rule(trans) \
1515 (((struct nft_trans_rule *)trans->data)->rule)
1516 #define nft_trans_flow_rule(trans) \
1517 (((struct nft_trans_rule *)trans->data)->flow)
1518 #define nft_trans_rule_id(trans) \
1519 (((struct nft_trans_rule *)trans->data)->rule_id)
1521 struct nft_trans_set {
1522 struct nft_set *set;
1527 #define nft_trans_set(trans) \
1528 (((struct nft_trans_set *)trans->data)->set)
1529 #define nft_trans_set_id(trans) \
1530 (((struct nft_trans_set *)trans->data)->set_id)
1531 #define nft_trans_set_bound(trans) \
1532 (((struct nft_trans_set *)trans->data)->bound)
1534 struct nft_trans_chain {
1537 struct nft_stats __percpu *stats;
1542 #define nft_trans_chain_update(trans) \
1543 (((struct nft_trans_chain *)trans->data)->update)
1544 #define nft_trans_chain_name(trans) \
1545 (((struct nft_trans_chain *)trans->data)->name)
1546 #define nft_trans_chain_stats(trans) \
1547 (((struct nft_trans_chain *)trans->data)->stats)
1548 #define nft_trans_chain_policy(trans) \
1549 (((struct nft_trans_chain *)trans->data)->policy)
1550 #define nft_trans_chain_id(trans) \
1551 (((struct nft_trans_chain *)trans->data)->chain_id)
1553 struct nft_trans_table {
1557 #define nft_trans_table_update(trans) \
1558 (((struct nft_trans_table *)trans->data)->update)
1560 struct nft_trans_elem {
1561 struct nft_set *set;
1562 struct nft_set_elem elem;
1566 #define nft_trans_elem_set(trans) \
1567 (((struct nft_trans_elem *)trans->data)->set)
1568 #define nft_trans_elem(trans) \
1569 (((struct nft_trans_elem *)trans->data)->elem)
1570 #define nft_trans_elem_set_bound(trans) \
1571 (((struct nft_trans_elem *)trans->data)->bound)
1573 struct nft_trans_obj {
1574 struct nft_object *obj;
1575 struct nft_object *newobj;
1579 #define nft_trans_obj(trans) \
1580 (((struct nft_trans_obj *)trans->data)->obj)
1581 #define nft_trans_obj_newobj(trans) \
1582 (((struct nft_trans_obj *)trans->data)->newobj)
1583 #define nft_trans_obj_update(trans) \
1584 (((struct nft_trans_obj *)trans->data)->update)
1586 struct nft_trans_flowtable {
1587 struct nft_flowtable *flowtable;
1589 struct list_head hook_list;
1593 #define nft_trans_flowtable(trans) \
1594 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1595 #define nft_trans_flowtable_update(trans) \
1596 (((struct nft_trans_flowtable *)trans->data)->update)
1597 #define nft_trans_flowtable_hooks(trans) \
1598 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1599 #define nft_trans_flowtable_flags(trans) \
1600 (((struct nft_trans_flowtable *)trans->data)->flags)
1602 int __init nft_chain_filter_init(void);
1603 void nft_chain_filter_fini(void);
1605 void __init nft_chain_route_init(void);
1606 void nft_chain_route_fini(void);
1608 void nf_tables_trans_destroy_flush_work(void);
1610 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1611 __be64 nf_jiffies64_to_msecs(u64 input);
1613 #ifdef CONFIG_MODULES
1614 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1616 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1619 struct nftables_pernet {
1620 struct list_head tables;
1621 struct list_head commit_list;
1622 struct list_head module_list;
1623 struct list_head notify_list;
1624 struct mutex commit_mutex;
1625 unsigned int base_seq;
1629 extern unsigned int nf_tables_net_id;
1631 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1633 return net_generic(net, nf_tables_net_id);
1636 #define __NFT_REDUCE_READONLY 1UL
1637 #define NFT_REDUCE_READONLY (void *)__NFT_REDUCE_READONLY
1639 static inline bool nft_reduce_is_readonly(const struct nft_expr *expr)
1641 return expr->ops->reduce == NFT_REDUCE_READONLY;
1644 void nft_reg_track_update(struct nft_regs_track *track,
1645 const struct nft_expr *expr, u8 dreg, u8 len);
1646 void nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg, u8 len);
1647 void __nft_reg_track_cancel(struct nft_regs_track *track, u8 dreg);
1649 static inline bool nft_reg_track_cmp(struct nft_regs_track *track,
1650 const struct nft_expr *expr, u8 dreg)
1652 return track->regs[dreg].selector &&
1653 track->regs[dreg].selector->ops == expr->ops &&
1654 track->regs[dreg].num_reg == 0;
1657 #endif /* _NET_NF_TABLES_H */