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>
17 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
21 #define NFT_JUMP_STACK_SIZE 16
27 /* for x_tables compatibility */
28 struct xt_action_param xt;
31 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
33 return pkt->xt.state->net;
36 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
38 return pkt->xt.state->hook;
41 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
43 return pkt->xt.state->pf;
46 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
48 return pkt->xt.state->in;
51 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
53 return pkt->xt.state->out;
56 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
58 const struct nf_hook_state *state)
61 pkt->xt.state = state;
64 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
67 pkt->tprot_set = false;
74 * struct nft_verdict - nf_tables verdict
76 * @code: nf_tables/netfilter verdict code
77 * @chain: destination chain for NFT_JUMP/NFT_GOTO
81 struct nft_chain *chain;
87 struct nft_verdict verdict;
89 } __attribute__((aligned(__alignof__(u64))));
92 * struct nft_regs - nf_tables register set
94 * @data: data registers
95 * @verdict: verdict register
97 * The first four data registers alias to the verdict register.
102 struct nft_verdict verdict;
106 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
108 * Note, when using concatenations, register allocation happens at 32-bit
109 * level. So for store instruction, pad the rest part with zero to avoid
113 static inline void nft_reg_store8(u32 *dreg, u8 val)
119 static inline u8 nft_reg_load8(const u32 *sreg)
124 static inline void nft_reg_store16(u32 *dreg, u16 val)
130 static inline u16 nft_reg_load16(const u32 *sreg)
135 static inline void nft_reg_store64(u32 *dreg, u64 val)
137 put_unaligned(val, (u64 *)dreg);
140 static inline u64 nft_reg_load64(const u32 *sreg)
142 return get_unaligned((u64 *)sreg);
145 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
148 if (len % NFT_REG32_SIZE)
149 dst[len / NFT_REG32_SIZE] = 0;
150 memcpy(dst, src, len);
154 * struct nft_ctx - nf_tables rule/set context
156 * @net: net namespace
157 * @table: the table the chain is contained in
158 * @chain: the chain the rule is contained in
159 * @nla: netlink attributes
160 * @portid: netlink portID of the original message
161 * @seq: netlink sequence number
162 * @family: protocol family
163 * @level: depth of the chains
164 * @report: notify via unicast netlink message
168 struct nft_table *table;
169 struct nft_chain *chain;
170 const struct nlattr * const *nla;
179 struct nft_data_desc {
180 enum nft_data_types type;
184 int nft_data_init(const struct nft_ctx *ctx,
185 struct nft_data *data, unsigned int size,
186 struct nft_data_desc *desc, const struct nlattr *nla);
187 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
188 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
189 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
190 enum nft_data_types type, unsigned int len);
192 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
194 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
197 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
199 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
202 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
203 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
205 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
206 int nft_parse_register_store(const struct nft_ctx *ctx,
207 const struct nlattr *attr, u8 *dreg,
208 const struct nft_data *data,
209 enum nft_data_types type, unsigned int len);
212 * struct nft_userdata - user defined data associated with an object
214 * @len: length of the data
217 * The presence of user data is indicated in an object specific fashion,
218 * so a length of zero can't occur and the value "len" indicates data
221 struct nft_userdata {
223 unsigned char data[];
227 * struct nft_set_elem - generic representation of set elements
230 * @key_end: closing element key
231 * @priv: element private data and extensions
233 struct nft_set_elem {
235 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
239 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
243 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
250 struct nft_set_iter {
255 int (*fn)(const struct nft_ctx *ctx,
257 const struct nft_set_iter *iter,
258 struct nft_set_elem *elem);
262 * struct nft_set_desc - description of set elements
266 * @size: number of set elements
267 * @field_len: length of each field in concatenation, bytes
268 * @field_count: number of concatenated fields in element
269 * @expr: set must support for expressions
271 struct nft_set_desc {
275 u8 field_len[NFT_REG32_COUNT];
281 * enum nft_set_class - performance class
283 * @NFT_LOOKUP_O_1: constant, O(1)
284 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
285 * @NFT_LOOKUP_O_N: linear, O(N)
289 NFT_SET_CLASS_O_LOG_N,
294 * struct nft_set_estimate - estimation of memory and performance
297 * @size: required memory
298 * @lookup: lookup performance class
299 * @space: memory class
301 struct nft_set_estimate {
303 enum nft_set_class lookup;
304 enum nft_set_class space;
307 #define NFT_EXPR_MAXATTR 16
308 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
309 ALIGN(size, __alignof__(struct nft_expr)))
312 * struct nft_expr - nf_tables expression
314 * @ops: expression ops
315 * @data: expression private data
318 const struct nft_expr_ops *ops;
320 __attribute__((aligned(__alignof__(u64))));
323 static inline void *nft_expr_priv(const struct nft_expr *expr)
325 return (void *)expr->data;
328 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
329 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
330 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
331 const struct nft_expr *expr);
336 * struct nft_set_ops - nf_tables set operations
338 * @lookup: look up an element within the set
339 * @update: update an element if exists, add it if doesn't exist
340 * @delete: delete an element
341 * @insert: insert new element into set
342 * @activate: activate new element in the next generation
343 * @deactivate: lookup for element and deactivate it in the next generation
344 * @flush: deactivate element in the next generation
345 * @remove: remove element from set
346 * @walk: iterate over all set elements
347 * @get: get set elements
348 * @privsize: function to return size of set private data
349 * @init: initialize private data of new set instance
350 * @destroy: destroy private data of set instance
351 * @elemsize: element private size
353 * Operations lookup, update and delete have simpler interfaces, are faster
354 * and currently only used in the packet path. All the rest are slower,
355 * control plane functions.
358 bool (*lookup)(const struct net *net,
359 const struct nft_set *set,
361 const struct nft_set_ext **ext);
362 bool (*update)(struct nft_set *set,
364 void *(*new)(struct nft_set *,
365 const struct nft_expr *,
367 const struct nft_expr *expr,
368 struct nft_regs *regs,
369 const struct nft_set_ext **ext);
370 bool (*delete)(const struct nft_set *set,
373 int (*insert)(const struct net *net,
374 const struct nft_set *set,
375 const struct nft_set_elem *elem,
376 struct nft_set_ext **ext);
377 void (*activate)(const struct net *net,
378 const struct nft_set *set,
379 const struct nft_set_elem *elem);
380 void * (*deactivate)(const struct net *net,
381 const struct nft_set *set,
382 const struct nft_set_elem *elem);
383 bool (*flush)(const struct net *net,
384 const struct nft_set *set,
386 void (*remove)(const struct net *net,
387 const struct nft_set *set,
388 const struct nft_set_elem *elem);
389 void (*walk)(const struct nft_ctx *ctx,
391 struct nft_set_iter *iter);
392 void * (*get)(const struct net *net,
393 const struct nft_set *set,
394 const struct nft_set_elem *elem,
397 u64 (*privsize)(const struct nlattr * const nla[],
398 const struct nft_set_desc *desc);
399 bool (*estimate)(const struct nft_set_desc *desc,
401 struct nft_set_estimate *est);
402 int (*init)(const struct nft_set *set,
403 const struct nft_set_desc *desc,
404 const struct nlattr * const nla[]);
405 void (*destroy)(const struct nft_set *set);
406 void (*gc_init)(const struct nft_set *set);
408 unsigned int elemsize;
412 * struct nft_set_type - nf_tables set type
414 * @ops: set ops for this type
415 * @features: features supported by the implementation
417 struct nft_set_type {
418 const struct nft_set_ops ops;
421 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
423 struct nft_set_elem_expr {
426 __attribute__((aligned(__alignof__(struct nft_expr))));
429 #define nft_setelem_expr_at(__elem_expr, __offset) \
430 ((struct nft_expr *)&__elem_expr->data[__offset])
432 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
433 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
434 __size < (__elem_expr)->size; \
435 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
437 #define NFT_SET_EXPR_MAX 2
440 * struct nft_set - nf_tables set instance
442 * @list: table set list node
443 * @bindings: list of set bindings
444 * @table: table this set belongs to
445 * @net: netnamespace this set belongs to
446 * @name: name of the set
447 * @handle: unique handle of the set
448 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
449 * @dtype: data type (verdict or numeric type defined by userspace)
450 * @objtype: object type (see NFT_OBJECT_* definitions)
451 * @size: maximum set size
452 * @field_len: length of each field in concatenation, bytes
453 * @field_count: number of concatenated fields in element
454 * @use: number of rules references to this set
455 * @nelems: number of elements
456 * @ndeact: number of deactivated elements queued for removal
457 * @timeout: default timeout value in jiffies
458 * @gc_int: garbage collection interval in msecs
459 * @policy: set parameterization (see enum nft_set_policies)
460 * @udlen: user data length
462 * @expr: stateful expression
465 * @genmask: generation mask
468 * @data: private set data
471 struct list_head list;
472 struct list_head bindings;
473 struct nft_table *table;
481 u8 field_len[NFT_REG32_COUNT];
490 unsigned char *udata;
491 /* runtime data below here */
492 const struct nft_set_ops *ops ____cacheline_aligned;
498 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
500 __attribute__((aligned(__alignof__(u64))));
503 static inline bool nft_set_is_anonymous(const struct nft_set *set)
505 return set->flags & NFT_SET_ANONYMOUS;
508 static inline void *nft_set_priv(const struct nft_set *set)
510 return (void *)set->data;
513 static inline struct nft_set *nft_set_container_of(const void *priv)
515 return (void *)priv - offsetof(struct nft_set, data);
518 struct nft_set *nft_set_lookup_global(const struct net *net,
519 const struct nft_table *table,
520 const struct nlattr *nla_set_name,
521 const struct nlattr *nla_set_id,
524 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
526 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
530 * struct nft_set_binding - nf_tables set binding
532 * @list: set bindings list node
533 * @chain: chain containing the rule bound to the set
534 * @flags: set action flags
536 * A set binding contains all information necessary for validation
537 * of new elements added to a bound set.
539 struct nft_set_binding {
540 struct list_head list;
541 const struct nft_chain *chain;
545 enum nft_trans_phase;
546 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
547 struct nft_set_binding *binding,
548 enum nft_trans_phase phase);
549 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
550 struct nft_set_binding *binding);
551 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
554 * enum nft_set_extensions - set extension type IDs
556 * @NFT_SET_EXT_KEY: element key
557 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
558 * @NFT_SET_EXT_DATA: mapping data
559 * @NFT_SET_EXT_FLAGS: element flags
560 * @NFT_SET_EXT_TIMEOUT: element timeout
561 * @NFT_SET_EXT_EXPIRATION: element expiration time
562 * @NFT_SET_EXT_USERDATA: user data associated with the element
563 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
564 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
565 * @NFT_SET_EXT_NUM: number of extension types
567 enum nft_set_extensions {
573 NFT_SET_EXT_EXPIRATION,
574 NFT_SET_EXT_USERDATA,
575 NFT_SET_EXT_EXPRESSIONS,
581 * struct nft_set_ext_type - set extension type
583 * @len: fixed part length of the extension
584 * @align: alignment requirements of the extension
586 struct nft_set_ext_type {
591 extern const struct nft_set_ext_type nft_set_ext_types[];
594 * struct nft_set_ext_tmpl - set extension template
596 * @len: length of extension area
597 * @offset: offsets of individual extension types
599 struct nft_set_ext_tmpl {
601 u8 offset[NFT_SET_EXT_NUM];
605 * struct nft_set_ext - set extensions
607 * @genmask: generation mask
608 * @offset: offsets of individual extension types
609 * @data: beginning of extension data
613 u8 offset[NFT_SET_EXT_NUM];
617 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
619 memset(tmpl, 0, sizeof(*tmpl));
620 tmpl->len = sizeof(struct nft_set_ext);
623 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
626 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
627 BUG_ON(tmpl->len > U8_MAX);
628 tmpl->offset[id] = tmpl->len;
629 tmpl->len += nft_set_ext_types[id].len + len;
632 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
634 nft_set_ext_add_length(tmpl, id, 0);
637 static inline void nft_set_ext_init(struct nft_set_ext *ext,
638 const struct nft_set_ext_tmpl *tmpl)
640 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
643 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
645 return !!ext->offset[id];
648 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
650 return ext && __nft_set_ext_exists(ext, id);
653 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
655 return (void *)ext + ext->offset[id];
658 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
660 return nft_set_ext(ext, NFT_SET_EXT_KEY);
663 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
665 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
668 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
670 return nft_set_ext(ext, NFT_SET_EXT_DATA);
673 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
675 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
678 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
680 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
683 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
685 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
688 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
690 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
693 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
695 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
698 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
700 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
701 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
704 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
707 return elem + set->ops->elemsize;
710 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
712 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
715 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
716 const struct nft_set *set,
717 const struct nlattr *attr);
719 void *nft_set_elem_init(const struct nft_set *set,
720 const struct nft_set_ext_tmpl *tmpl,
721 const u32 *key, const u32 *key_end, const u32 *data,
722 u64 timeout, u64 expiration, gfp_t gfp);
723 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
724 struct nft_expr *expr_array[]);
725 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
729 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
732 * @set: set the elements belong to
733 * @cnt: count of elements
735 struct nft_set_gc_batch_head {
737 const struct nft_set *set;
741 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
742 sizeof(struct nft_set_gc_batch_head)) / \
746 * struct nft_set_gc_batch - nf_tables set garbage collection batch
748 * @head: GC batch head
749 * @elems: garbage collection elements
751 struct nft_set_gc_batch {
752 struct nft_set_gc_batch_head head;
753 void *elems[NFT_SET_GC_BATCH_SIZE];
756 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
758 void nft_set_gc_batch_release(struct rcu_head *rcu);
760 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
763 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
766 static inline struct nft_set_gc_batch *
767 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
771 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
773 nft_set_gc_batch_complete(gcb);
775 return nft_set_gc_batch_alloc(set, gfp);
778 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
781 gcb->elems[gcb->head.cnt++] = elem;
786 * struct nft_expr_type - nf_tables expression type
788 * @select_ops: function to select nft_expr_ops
789 * @release_ops: release nft_expr_ops
790 * @ops: default ops, used when no select_ops functions is present
791 * @list: used internally
793 * @owner: module reference
794 * @policy: netlink attribute policy
795 * @maxattr: highest netlink attribute number
796 * @family: address family for AF-specific types
797 * @flags: expression type flags
799 struct nft_expr_type {
800 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
801 const struct nlattr * const tb[]);
802 void (*release_ops)(const struct nft_expr_ops *ops);
803 const struct nft_expr_ops *ops;
804 struct list_head list;
806 struct module *owner;
807 const struct nla_policy *policy;
808 unsigned int maxattr;
813 #define NFT_EXPR_STATEFUL 0x1
814 #define NFT_EXPR_GC 0x2
816 enum nft_trans_phase {
823 struct nft_flow_rule;
824 struct nft_offload_ctx;
827 * struct nft_expr_ops - nf_tables expression operations
829 * @eval: Expression evaluation function
830 * @size: full expression size, including private data size
831 * @init: initialization function
832 * @activate: activate expression in the next generation
833 * @deactivate: deactivate expression in next generation
834 * @destroy: destruction function, called after synchronize_rcu
835 * @dump: function to dump parameters
836 * @type: expression type
837 * @validate: validate expression, called during loop detection
838 * @data: extra data to attach to this expression operation
840 struct nft_expr_ops {
841 void (*eval)(const struct nft_expr *expr,
842 struct nft_regs *regs,
843 const struct nft_pktinfo *pkt);
844 int (*clone)(struct nft_expr *dst,
845 const struct nft_expr *src);
848 int (*init)(const struct nft_ctx *ctx,
849 const struct nft_expr *expr,
850 const struct nlattr * const tb[]);
851 void (*activate)(const struct nft_ctx *ctx,
852 const struct nft_expr *expr);
853 void (*deactivate)(const struct nft_ctx *ctx,
854 const struct nft_expr *expr,
855 enum nft_trans_phase phase);
856 void (*destroy)(const struct nft_ctx *ctx,
857 const struct nft_expr *expr);
858 void (*destroy_clone)(const struct nft_ctx *ctx,
859 const struct nft_expr *expr);
860 int (*dump)(struct sk_buff *skb,
861 const struct nft_expr *expr);
862 int (*validate)(const struct nft_ctx *ctx,
863 const struct nft_expr *expr,
864 const struct nft_data **data);
865 bool (*gc)(struct net *net,
866 const struct nft_expr *expr);
867 int (*offload)(struct nft_offload_ctx *ctx,
868 struct nft_flow_rule *flow,
869 const struct nft_expr *expr);
871 const struct nft_expr_type *type;
876 * struct nft_rule - nf_tables rule
878 * @list: used internally
879 * @handle: rule handle
880 * @genmask: generation mask
881 * @dlen: length of expression data
882 * @udata: user data is appended to the rule
883 * @data: expression data
886 struct list_head list;
892 __attribute__((aligned(__alignof__(struct nft_expr))));
895 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
897 return (struct nft_expr *)&rule->data[0];
900 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
902 return ((void *)expr) + expr->ops->size;
905 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
907 return (struct nft_expr *)&rule->data[rule->dlen];
910 static inline bool nft_expr_more(const struct nft_rule *rule,
911 const struct nft_expr *expr)
913 return expr != nft_expr_last(rule) && expr->ops;
916 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
918 return (void *)&rule->data[rule->dlen];
921 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
923 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
924 struct nft_regs *regs,
925 const struct nft_pktinfo *pkt)
927 struct nft_set_elem_expr *elem_expr;
928 struct nft_expr *expr;
931 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
932 elem_expr = nft_set_ext_expr(ext);
933 nft_setelem_expr_foreach(expr, elem_expr, size) {
934 expr->ops->eval(expr, regs, pkt);
935 if (regs->verdict.code == NFT_BREAK)
942 * The last pointer isn't really necessary, but the compiler isn't able to
943 * determine that the result of nft_expr_last() is always the same since it
944 * can't assume that the dlen value wasn't changed within calls in the loop.
946 #define nft_rule_for_each_expr(expr, last, rule) \
947 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
949 (expr) = nft_expr_next(expr))
951 #define NFT_CHAIN_POLICY_UNSET U8_MAX
954 * struct nft_chain - nf_tables chain
956 * @rules: list of rules in the chain
957 * @list: used internally
958 * @rhlhead: used internally
959 * @table: table that this chain belongs to
960 * @handle: chain handle
961 * @use: number of jump references to this chain
962 * @flags: bitmask of enum nft_chain_flags
963 * @name: name of the chain
966 struct nft_rule *__rcu *rules_gen_0;
967 struct nft_rule *__rcu *rules_gen_1;
968 struct list_head rules;
969 struct list_head list;
970 struct rhlist_head rhlhead;
971 struct nft_table *table;
981 /* Only used during control plane commit phase: */
982 struct nft_rule **rules_next;
985 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
987 enum nft_chain_types {
988 NFT_CHAIN_T_DEFAULT = 0,
995 * struct nft_chain_type - nf_tables chain type info
997 * @name: name of the type
998 * @type: numeric identifier
999 * @family: address family
1000 * @owner: module owner
1001 * @hook_mask: mask of valid hooks
1002 * @hooks: array of hook functions
1003 * @ops_register: base chain register function
1004 * @ops_unregister: base chain unregister function
1006 struct nft_chain_type {
1008 enum nft_chain_types type;
1010 struct module *owner;
1011 unsigned int hook_mask;
1012 nf_hookfn *hooks[NFT_MAX_HOOKS];
1013 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1014 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1017 int nft_chain_validate_dependency(const struct nft_chain *chain,
1018 enum nft_chain_types type);
1019 int nft_chain_validate_hooks(const struct nft_chain *chain,
1020 unsigned int hook_flags);
1022 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1024 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1027 void nft_chain_del(struct nft_chain *chain);
1028 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1033 struct u64_stats_sync syncp;
1037 struct list_head list;
1039 struct nf_hook_ops ops;
1040 struct rcu_head rcu;
1044 * struct nft_base_chain - nf_tables base chain
1046 * @ops: netfilter hook ops
1047 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1049 * @policy: default policy
1050 * @stats: per-cpu chain stats
1052 * @flow_block: flow block (for hardware offload)
1054 struct nft_base_chain {
1055 struct nf_hook_ops ops;
1056 struct list_head hook_list;
1057 const struct nft_chain_type *type;
1060 struct nft_stats __percpu *stats;
1061 struct nft_chain chain;
1062 struct flow_block flow_block;
1065 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1067 return container_of(chain, struct nft_base_chain, chain);
1070 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1072 return chain->flags & NFT_CHAIN_BASE;
1075 int __nft_release_basechain(struct nft_ctx *ctx);
1077 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1080 * struct nft_table - nf_tables table
1082 * @list: used internally
1083 * @chains_ht: chains in the table
1084 * @chains: same, for stable walks
1085 * @sets: sets in the table
1086 * @objects: stateful objects in the table
1087 * @flowtables: flow tables in the table
1088 * @hgenerator: handle generator state
1089 * @handle: table handle
1090 * @use: number of chain references to this table
1091 * @flags: table flag (see enum nft_table_flags)
1092 * @genmask: generation mask
1093 * @afinfo: address family info
1094 * @name: name of the table
1097 struct list_head list;
1098 struct rhltable chains_ht;
1099 struct list_head chains;
1100 struct list_head sets;
1101 struct list_head objects;
1102 struct list_head flowtables;
1115 static inline bool nft_table_has_owner(const struct nft_table *table)
1117 return table->flags & NFT_TABLE_F_OWNER;
1120 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1122 return family == NFPROTO_NETDEV ||
1123 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1126 void nft_register_chain_type(const struct nft_chain_type *);
1127 void nft_unregister_chain_type(const struct nft_chain_type *);
1129 int nft_register_expr(struct nft_expr_type *);
1130 void nft_unregister_expr(struct nft_expr_type *);
1132 int nft_verdict_dump(struct sk_buff *skb, int type,
1133 const struct nft_verdict *v);
1136 * struct nft_object_hash_key - key to lookup nft_object
1138 * @name: name of the stateful object to look up
1139 * @table: table the object belongs to
1141 struct nft_object_hash_key {
1143 const struct nft_table *table;
1147 * struct nft_object - nf_tables stateful object
1149 * @list: table stateful object list node
1150 * @key: keys that identify this object
1151 * @rhlhead: nft_objname_ht node
1152 * @genmask: generation mask
1153 * @use: number of references to this stateful object
1154 * @handle: unique object handle
1155 * @ops: object operations
1156 * @data: object data, layout depends on type
1159 struct list_head list;
1160 struct rhlist_head rhlhead;
1161 struct nft_object_hash_key key;
1167 /* runtime data below here */
1168 const struct nft_object_ops *ops ____cacheline_aligned;
1169 unsigned char data[]
1170 __attribute__((aligned(__alignof__(u64))));
1173 static inline void *nft_obj_data(const struct nft_object *obj)
1175 return (void *)obj->data;
1178 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1180 struct nft_object *nft_obj_lookup(const struct net *net,
1181 const struct nft_table *table,
1182 const struct nlattr *nla, u32 objtype,
1185 void nft_obj_notify(struct net *net, const struct nft_table *table,
1186 struct nft_object *obj, u32 portid, u32 seq,
1187 int event, int family, int report, gfp_t gfp);
1190 * struct nft_object_type - stateful object type
1192 * @select_ops: function to select nft_object_ops
1193 * @ops: default ops, used when no select_ops functions is present
1194 * @list: list node in list of object types
1195 * @type: stateful object numeric type
1196 * @owner: module owner
1197 * @maxattr: maximum netlink attribute
1198 * @policy: netlink attribute policy
1200 struct nft_object_type {
1201 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1202 const struct nlattr * const tb[]);
1203 const struct nft_object_ops *ops;
1204 struct list_head list;
1206 unsigned int maxattr;
1207 struct module *owner;
1208 const struct nla_policy *policy;
1212 * struct nft_object_ops - stateful object operations
1214 * @eval: stateful object evaluation function
1215 * @size: stateful object size
1216 * @init: initialize object from netlink attributes
1217 * @destroy: release existing stateful object
1218 * @dump: netlink dump stateful object
1219 * @update: update stateful object
1221 struct nft_object_ops {
1222 void (*eval)(struct nft_object *obj,
1223 struct nft_regs *regs,
1224 const struct nft_pktinfo *pkt);
1226 int (*init)(const struct nft_ctx *ctx,
1227 const struct nlattr *const tb[],
1228 struct nft_object *obj);
1229 void (*destroy)(const struct nft_ctx *ctx,
1230 struct nft_object *obj);
1231 int (*dump)(struct sk_buff *skb,
1232 struct nft_object *obj,
1234 void (*update)(struct nft_object *obj,
1235 struct nft_object *newobj);
1236 const struct nft_object_type *type;
1239 int nft_register_obj(struct nft_object_type *obj_type);
1240 void nft_unregister_obj(struct nft_object_type *obj_type);
1242 #define NFT_NETDEVICE_MAX 256
1245 * struct nft_flowtable - nf_tables flow table
1247 * @list: flow table list node in table list
1248 * @table: the table the flow table is contained in
1249 * @name: name of this flow table
1250 * @hooknum: hook number
1251 * @ops_len: number of hooks in array
1252 * @genmask: generation mask
1253 * @use: number of references to this flow table
1254 * @handle: unique object handle
1255 * @dev_name: array of device names
1256 * @data: rhashtable and garbage collector
1257 * @ops: array of hooks
1259 struct nft_flowtable {
1260 struct list_head list;
1261 struct nft_table *table;
1268 /* runtime data below here */
1269 struct list_head hook_list ____cacheline_aligned;
1270 struct nf_flowtable data;
1273 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1274 const struct nlattr *nla,
1277 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1278 struct nft_flowtable *flowtable,
1279 enum nft_trans_phase phase);
1281 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1282 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1285 * struct nft_traceinfo - nft tracing information and state
1287 * @pkt: pktinfo currently processed
1288 * @basechain: base chain currently processed
1289 * @chain: chain currently processed
1290 * @rule: rule that was evaluated
1291 * @verdict: verdict given by rule
1292 * @type: event type (enum nft_trace_types)
1293 * @packet_dumped: packet headers sent in a previous traceinfo message
1294 * @trace: other struct members are initialised
1296 struct nft_traceinfo {
1297 const struct nft_pktinfo *pkt;
1298 const struct nft_base_chain *basechain;
1299 const struct nft_chain *chain;
1300 const struct nft_rule *rule;
1301 const struct nft_verdict *verdict;
1302 enum nft_trace_types type;
1307 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1308 const struct nft_verdict *verdict,
1309 const struct nft_chain *basechain);
1311 void nft_trace_notify(struct nft_traceinfo *info);
1313 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1314 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1316 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1317 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1319 #define MODULE_ALIAS_NFT_EXPR(name) \
1320 MODULE_ALIAS("nft-expr-" name)
1322 #define MODULE_ALIAS_NFT_OBJ(type) \
1323 MODULE_ALIAS("nft-obj-" __stringify(type))
1325 #if IS_ENABLED(CONFIG_NF_TABLES)
1328 * The gencursor defines two generations, the currently active and the
1329 * next one. Objects contain a bitmask of 2 bits specifying the generations
1330 * they're active in. A set bit means they're inactive in the generation
1331 * represented by that bit.
1333 * New objects start out as inactive in the current and active in the
1334 * next generation. When committing the ruleset the bitmask is cleared,
1335 * meaning they're active in all generations. When removing an object,
1336 * it is set inactive in the next generation. After committing the ruleset,
1337 * the objects are removed.
1339 static inline unsigned int nft_gencursor_next(const struct net *net)
1341 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1344 static inline u8 nft_genmask_next(const struct net *net)
1346 return 1 << nft_gencursor_next(net);
1349 static inline u8 nft_genmask_cur(const struct net *net)
1351 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1352 return 1 << READ_ONCE(net->nft.gencursor);
1355 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1358 * Generic transaction helpers
1361 /* Check if this object is currently active. */
1362 #define nft_is_active(__net, __obj) \
1363 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1365 /* Check if this object is active in the next generation. */
1366 #define nft_is_active_next(__net, __obj) \
1367 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1369 /* This object becomes active in the next generation. */
1370 #define nft_activate_next(__net, __obj) \
1371 (__obj)->genmask = nft_genmask_cur(__net)
1373 /* This object becomes inactive in the next generation. */
1374 #define nft_deactivate_next(__net, __obj) \
1375 (__obj)->genmask = nft_genmask_next(__net)
1377 /* After committing the ruleset, clear the stale generation bit. */
1378 #define nft_clear(__net, __obj) \
1379 (__obj)->genmask &= ~nft_genmask_next(__net)
1380 #define nft_active_genmask(__obj, __genmask) \
1381 !((__obj)->genmask & __genmask)
1384 * Set element transaction helpers
1387 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1390 return !(ext->genmask & genmask);
1393 static inline void nft_set_elem_change_active(const struct net *net,
1394 const struct nft_set *set,
1395 struct nft_set_ext *ext)
1397 ext->genmask ^= nft_genmask_next(net);
1400 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1403 * We use a free bit in the genmask field to indicate the element
1404 * is busy, meaning it is currently being processed either by
1405 * the netlink API or GC.
1407 * Even though the genmask is only a single byte wide, this works
1408 * because the extension structure if fully constant once initialized,
1409 * so there are no non-atomic write accesses unless it is already
1412 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1414 #if defined(__LITTLE_ENDIAN_BITFIELD)
1415 #define NFT_SET_ELEM_BUSY_BIT 2
1416 #elif defined(__BIG_ENDIAN_BITFIELD)
1417 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1422 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1424 unsigned long *word = (unsigned long *)ext;
1426 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1427 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1430 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1432 unsigned long *word = (unsigned long *)ext;
1434 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1438 * struct nft_trans - nf_tables object update in transaction
1440 * @list: used internally
1441 * @msg_type: message type
1442 * @put_net: ctx->net needs to be put
1443 * @ctx: transaction context
1444 * @data: internal information related to the transaction
1447 struct list_head list;
1454 struct nft_trans_rule {
1455 struct nft_rule *rule;
1456 struct nft_flow_rule *flow;
1460 #define nft_trans_rule(trans) \
1461 (((struct nft_trans_rule *)trans->data)->rule)
1462 #define nft_trans_flow_rule(trans) \
1463 (((struct nft_trans_rule *)trans->data)->flow)
1464 #define nft_trans_rule_id(trans) \
1465 (((struct nft_trans_rule *)trans->data)->rule_id)
1467 struct nft_trans_set {
1468 struct nft_set *set;
1473 #define nft_trans_set(trans) \
1474 (((struct nft_trans_set *)trans->data)->set)
1475 #define nft_trans_set_id(trans) \
1476 (((struct nft_trans_set *)trans->data)->set_id)
1477 #define nft_trans_set_bound(trans) \
1478 (((struct nft_trans_set *)trans->data)->bound)
1480 struct nft_trans_chain {
1483 struct nft_stats __percpu *stats;
1488 #define nft_trans_chain_update(trans) \
1489 (((struct nft_trans_chain *)trans->data)->update)
1490 #define nft_trans_chain_name(trans) \
1491 (((struct nft_trans_chain *)trans->data)->name)
1492 #define nft_trans_chain_stats(trans) \
1493 (((struct nft_trans_chain *)trans->data)->stats)
1494 #define nft_trans_chain_policy(trans) \
1495 (((struct nft_trans_chain *)trans->data)->policy)
1496 #define nft_trans_chain_id(trans) \
1497 (((struct nft_trans_chain *)trans->data)->chain_id)
1499 struct nft_trans_table {
1504 #define nft_trans_table_update(trans) \
1505 (((struct nft_trans_table *)trans->data)->update)
1506 #define nft_trans_table_enable(trans) \
1507 (((struct nft_trans_table *)trans->data)->enable)
1509 struct nft_trans_elem {
1510 struct nft_set *set;
1511 struct nft_set_elem elem;
1515 #define nft_trans_elem_set(trans) \
1516 (((struct nft_trans_elem *)trans->data)->set)
1517 #define nft_trans_elem(trans) \
1518 (((struct nft_trans_elem *)trans->data)->elem)
1519 #define nft_trans_elem_set_bound(trans) \
1520 (((struct nft_trans_elem *)trans->data)->bound)
1522 struct nft_trans_obj {
1523 struct nft_object *obj;
1524 struct nft_object *newobj;
1528 #define nft_trans_obj(trans) \
1529 (((struct nft_trans_obj *)trans->data)->obj)
1530 #define nft_trans_obj_newobj(trans) \
1531 (((struct nft_trans_obj *)trans->data)->newobj)
1532 #define nft_trans_obj_update(trans) \
1533 (((struct nft_trans_obj *)trans->data)->update)
1535 struct nft_trans_flowtable {
1536 struct nft_flowtable *flowtable;
1538 struct list_head hook_list;
1542 #define nft_trans_flowtable(trans) \
1543 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1544 #define nft_trans_flowtable_update(trans) \
1545 (((struct nft_trans_flowtable *)trans->data)->update)
1546 #define nft_trans_flowtable_hooks(trans) \
1547 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1548 #define nft_trans_flowtable_flags(trans) \
1549 (((struct nft_trans_flowtable *)trans->data)->flags)
1551 int __init nft_chain_filter_init(void);
1552 void nft_chain_filter_fini(void);
1554 void __init nft_chain_route_init(void);
1555 void nft_chain_route_fini(void);
1557 void nf_tables_trans_destroy_flush_work(void);
1559 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1560 __be64 nf_jiffies64_to_msecs(u64 input);
1562 #endif /* _NET_NF_TABLES_H */