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>
19 #define NFT_JUMP_STACK_SIZE 16
25 /* for x_tables compatibility */
26 struct xt_action_param xt;
29 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
31 return pkt->xt.state->net;
34 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
36 return pkt->xt.state->hook;
39 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
41 return pkt->xt.state->pf;
44 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
46 return pkt->xt.state->in;
49 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
51 return pkt->xt.state->out;
54 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
56 const struct nf_hook_state *state)
59 pkt->xt.state = state;
62 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
65 pkt->tprot_set = false;
72 * struct nft_verdict - nf_tables verdict
74 * @code: nf_tables/netfilter verdict code
75 * @chain: destination chain for NFT_JUMP/NFT_GOTO
79 struct nft_chain *chain;
85 struct nft_verdict verdict;
87 } __attribute__((aligned(__alignof__(u64))));
90 * struct nft_regs - nf_tables register set
92 * @data: data registers
93 * @verdict: verdict register
95 * The first four data registers alias to the verdict register.
100 struct nft_verdict verdict;
104 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
106 * Note, when using concatenations, register allocation happens at 32-bit
107 * level. So for store instruction, pad the rest part with zero to avoid
111 static inline void nft_reg_store8(u32 *dreg, u8 val)
117 static inline u8 nft_reg_load8(const u32 *sreg)
122 static inline void nft_reg_store16(u32 *dreg, u16 val)
128 static inline u16 nft_reg_load16(const u32 *sreg)
133 static inline void nft_reg_store64(u32 *dreg, u64 val)
135 put_unaligned(val, (u64 *)dreg);
138 static inline u64 nft_reg_load64(const u32 *sreg)
140 return get_unaligned((u64 *)sreg);
143 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
146 if (len % NFT_REG32_SIZE)
147 dst[len / NFT_REG32_SIZE] = 0;
148 memcpy(dst, src, len);
152 * struct nft_ctx - nf_tables rule/set context
154 * @net: net namespace
155 * @table: the table the chain is contained in
156 * @chain: the chain the rule is contained in
157 * @nla: netlink attributes
158 * @portid: netlink portID of the original message
159 * @seq: netlink sequence number
160 * @family: protocol family
161 * @level: depth of the chains
162 * @report: notify via unicast netlink message
166 struct nft_table *table;
167 struct nft_chain *chain;
168 const struct nlattr * const *nla;
177 struct nft_data_desc {
178 enum nft_data_types type;
182 int nft_data_init(const struct nft_ctx *ctx,
183 struct nft_data *data, unsigned int size,
184 struct nft_data_desc *desc, const struct nlattr *nla);
185 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
186 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
187 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
188 enum nft_data_types type, unsigned int len);
190 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
192 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
195 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
197 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
200 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
201 unsigned int nft_parse_register(const struct nlattr *attr);
202 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
204 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
205 int nft_validate_register_store(const struct nft_ctx *ctx,
206 enum nft_registers reg,
207 const struct nft_data *data,
208 enum nft_data_types type, unsigned int len);
211 * struct nft_userdata - user defined data associated with an object
213 * @len: length of the data
216 * The presence of user data is indicated in an object specific fashion,
217 * so a length of zero can't occur and the value "len" indicates data
220 struct nft_userdata {
222 unsigned char data[];
226 * struct nft_set_elem - generic representation of set elements
229 * @key_end: closing element key
230 * @priv: element private data and extensions
232 struct nft_set_elem {
234 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
238 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
242 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
249 struct nft_set_iter {
254 int (*fn)(const struct nft_ctx *ctx,
256 const struct nft_set_iter *iter,
257 struct nft_set_elem *elem);
261 * struct nft_set_desc - description of set elements
265 * @size: number of set elements
266 * @field_len: length of each field in concatenation, bytes
267 * @field_count: number of concatenated fields in element
268 * @expr: set must support for expressions
270 struct nft_set_desc {
274 u8 field_len[NFT_REG32_COUNT];
280 * enum nft_set_class - performance class
282 * @NFT_LOOKUP_O_1: constant, O(1)
283 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
284 * @NFT_LOOKUP_O_N: linear, O(N)
288 NFT_SET_CLASS_O_LOG_N,
293 * struct nft_set_estimate - estimation of memory and performance
296 * @size: required memory
297 * @lookup: lookup performance class
298 * @space: memory class
300 struct nft_set_estimate {
302 enum nft_set_class lookup;
303 enum nft_set_class space;
310 * struct nft_set_ops - nf_tables set operations
312 * @lookup: look up an element within the set
313 * @update: update an element if exists, add it if doesn't exist
314 * @delete: delete an element
315 * @insert: insert new element into set
316 * @activate: activate new element in the next generation
317 * @deactivate: lookup for element and deactivate it in the next generation
318 * @flush: deactivate element in the next generation
319 * @remove: remove element from set
320 * @walk: iterate over all set elements
321 * @get: get set elements
322 * @privsize: function to return size of set private data
323 * @init: initialize private data of new set instance
324 * @destroy: destroy private data of set instance
325 * @elemsize: element private size
327 * Operations lookup, update and delete have simpler interfaces, are faster
328 * and currently only used in the packet path. All the rest are slower,
329 * control plane functions.
332 bool (*lookup)(const struct net *net,
333 const struct nft_set *set,
335 const struct nft_set_ext **ext);
336 bool (*update)(struct nft_set *set,
338 void *(*new)(struct nft_set *,
339 const struct nft_expr *,
341 const struct nft_expr *expr,
342 struct nft_regs *regs,
343 const struct nft_set_ext **ext);
344 bool (*delete)(const struct nft_set *set,
347 int (*insert)(const struct net *net,
348 const struct nft_set *set,
349 const struct nft_set_elem *elem,
350 struct nft_set_ext **ext);
351 void (*activate)(const struct net *net,
352 const struct nft_set *set,
353 const struct nft_set_elem *elem);
354 void * (*deactivate)(const struct net *net,
355 const struct nft_set *set,
356 const struct nft_set_elem *elem);
357 bool (*flush)(const struct net *net,
358 const struct nft_set *set,
360 void (*remove)(const struct net *net,
361 const struct nft_set *set,
362 const struct nft_set_elem *elem);
363 void (*walk)(const struct nft_ctx *ctx,
365 struct nft_set_iter *iter);
366 void * (*get)(const struct net *net,
367 const struct nft_set *set,
368 const struct nft_set_elem *elem,
371 u64 (*privsize)(const struct nlattr * const nla[],
372 const struct nft_set_desc *desc);
373 bool (*estimate)(const struct nft_set_desc *desc,
375 struct nft_set_estimate *est);
376 int (*init)(const struct nft_set *set,
377 const struct nft_set_desc *desc,
378 const struct nlattr * const nla[]);
379 void (*destroy)(const struct nft_set *set);
380 void (*gc_init)(const struct nft_set *set);
382 unsigned int elemsize;
386 * struct nft_set_type - nf_tables set type
388 * @ops: set ops for this type
389 * @features: features supported by the implementation
391 struct nft_set_type {
392 const struct nft_set_ops ops;
395 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
398 * struct nft_set - nf_tables set instance
400 * @list: table set list node
401 * @bindings: list of set bindings
402 * @table: table this set belongs to
403 * @net: netnamespace this set belongs to
404 * @name: name of the set
405 * @handle: unique handle of the set
406 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
407 * @dtype: data type (verdict or numeric type defined by userspace)
408 * @objtype: object type (see NFT_OBJECT_* definitions)
409 * @size: maximum set size
410 * @field_len: length of each field in concatenation, bytes
411 * @field_count: number of concatenated fields in element
412 * @use: number of rules references to this set
413 * @nelems: number of elements
414 * @ndeact: number of deactivated elements queued for removal
415 * @timeout: default timeout value in jiffies
416 * @gc_int: garbage collection interval in msecs
417 * @policy: set parameterization (see enum nft_set_policies)
418 * @udlen: user data length
420 * @expr: stateful expression
423 * @genmask: generation mask
426 * @data: private set data
429 struct list_head list;
430 struct list_head bindings;
431 struct nft_table *table;
439 u8 field_len[NFT_REG32_COUNT];
448 unsigned char *udata;
449 struct nft_expr *expr;
450 /* runtime data below here */
451 const struct nft_set_ops *ops ____cacheline_aligned;
457 __attribute__((aligned(__alignof__(u64))));
460 static inline bool nft_set_is_anonymous(const struct nft_set *set)
462 return set->flags & NFT_SET_ANONYMOUS;
465 static inline void *nft_set_priv(const struct nft_set *set)
467 return (void *)set->data;
470 static inline struct nft_set *nft_set_container_of(const void *priv)
472 return (void *)priv - offsetof(struct nft_set, data);
475 struct nft_set *nft_set_lookup_global(const struct net *net,
476 const struct nft_table *table,
477 const struct nlattr *nla_set_name,
478 const struct nlattr *nla_set_id,
481 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
483 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
487 * struct nft_set_binding - nf_tables set binding
489 * @list: set bindings list node
490 * @chain: chain containing the rule bound to the set
491 * @flags: set action flags
493 * A set binding contains all information necessary for validation
494 * of new elements added to a bound set.
496 struct nft_set_binding {
497 struct list_head list;
498 const struct nft_chain *chain;
502 enum nft_trans_phase;
503 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
504 struct nft_set_binding *binding,
505 enum nft_trans_phase phase);
506 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
507 struct nft_set_binding *binding);
508 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
511 * enum nft_set_extensions - set extension type IDs
513 * @NFT_SET_EXT_KEY: element key
514 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
515 * @NFT_SET_EXT_DATA: mapping data
516 * @NFT_SET_EXT_FLAGS: element flags
517 * @NFT_SET_EXT_TIMEOUT: element timeout
518 * @NFT_SET_EXT_EXPIRATION: element expiration time
519 * @NFT_SET_EXT_USERDATA: user data associated with the element
520 * @NFT_SET_EXT_EXPR: expression assiociated with the element
521 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
522 * @NFT_SET_EXT_NUM: number of extension types
524 enum nft_set_extensions {
530 NFT_SET_EXT_EXPIRATION,
531 NFT_SET_EXT_USERDATA,
538 * struct nft_set_ext_type - set extension type
540 * @len: fixed part length of the extension
541 * @align: alignment requirements of the extension
543 struct nft_set_ext_type {
548 extern const struct nft_set_ext_type nft_set_ext_types[];
551 * struct nft_set_ext_tmpl - set extension template
553 * @len: length of extension area
554 * @offset: offsets of individual extension types
556 struct nft_set_ext_tmpl {
558 u8 offset[NFT_SET_EXT_NUM];
562 * struct nft_set_ext - set extensions
564 * @genmask: generation mask
565 * @offset: offsets of individual extension types
566 * @data: beginning of extension data
570 u8 offset[NFT_SET_EXT_NUM];
574 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
576 memset(tmpl, 0, sizeof(*tmpl));
577 tmpl->len = sizeof(struct nft_set_ext);
580 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
583 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
584 BUG_ON(tmpl->len > U8_MAX);
585 tmpl->offset[id] = tmpl->len;
586 tmpl->len += nft_set_ext_types[id].len + len;
589 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
591 nft_set_ext_add_length(tmpl, id, 0);
594 static inline void nft_set_ext_init(struct nft_set_ext *ext,
595 const struct nft_set_ext_tmpl *tmpl)
597 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
600 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
602 return !!ext->offset[id];
605 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
607 return ext && __nft_set_ext_exists(ext, id);
610 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
612 return (void *)ext + ext->offset[id];
615 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
617 return nft_set_ext(ext, NFT_SET_EXT_KEY);
620 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
622 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
625 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
627 return nft_set_ext(ext, NFT_SET_EXT_DATA);
630 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
632 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
635 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
637 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
640 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
642 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
645 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
647 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
650 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
652 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
655 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
657 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
658 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
661 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
664 return elem + set->ops->elemsize;
667 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
669 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
672 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
673 const struct nft_set *set,
674 const struct nlattr *attr);
676 void *nft_set_elem_init(const struct nft_set *set,
677 const struct nft_set_ext_tmpl *tmpl,
678 const u32 *key, const u32 *key_end, const u32 *data,
679 u64 timeout, u64 expiration, gfp_t gfp);
680 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
684 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
687 * @set: set the elements belong to
688 * @cnt: count of elements
690 struct nft_set_gc_batch_head {
692 const struct nft_set *set;
696 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
697 sizeof(struct nft_set_gc_batch_head)) / \
701 * struct nft_set_gc_batch - nf_tables set garbage collection batch
703 * @head: GC batch head
704 * @elems: garbage collection elements
706 struct nft_set_gc_batch {
707 struct nft_set_gc_batch_head head;
708 void *elems[NFT_SET_GC_BATCH_SIZE];
711 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
713 void nft_set_gc_batch_release(struct rcu_head *rcu);
715 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
718 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
721 static inline struct nft_set_gc_batch *
722 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
726 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
728 nft_set_gc_batch_complete(gcb);
730 return nft_set_gc_batch_alloc(set, gfp);
733 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
736 gcb->elems[gcb->head.cnt++] = elem;
741 * struct nft_expr_type - nf_tables expression type
743 * @select_ops: function to select nft_expr_ops
744 * @release_ops: release nft_expr_ops
745 * @ops: default ops, used when no select_ops functions is present
746 * @list: used internally
748 * @owner: module reference
749 * @policy: netlink attribute policy
750 * @maxattr: highest netlink attribute number
751 * @family: address family for AF-specific types
752 * @flags: expression type flags
754 struct nft_expr_type {
755 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
756 const struct nlattr * const tb[]);
757 void (*release_ops)(const struct nft_expr_ops *ops);
758 const struct nft_expr_ops *ops;
759 struct list_head list;
761 struct module *owner;
762 const struct nla_policy *policy;
763 unsigned int maxattr;
768 #define NFT_EXPR_STATEFUL 0x1
769 #define NFT_EXPR_GC 0x2
771 enum nft_trans_phase {
778 struct nft_flow_rule;
779 struct nft_offload_ctx;
782 * struct nft_expr_ops - nf_tables expression operations
784 * @eval: Expression evaluation function
785 * @size: full expression size, including private data size
786 * @init: initialization function
787 * @activate: activate expression in the next generation
788 * @deactivate: deactivate expression in next generation
789 * @destroy: destruction function, called after synchronize_rcu
790 * @dump: function to dump parameters
791 * @type: expression type
792 * @validate: validate expression, called during loop detection
793 * @data: extra data to attach to this expression operation
796 struct nft_expr_ops {
797 void (*eval)(const struct nft_expr *expr,
798 struct nft_regs *regs,
799 const struct nft_pktinfo *pkt);
800 int (*clone)(struct nft_expr *dst,
801 const struct nft_expr *src);
804 int (*init)(const struct nft_ctx *ctx,
805 const struct nft_expr *expr,
806 const struct nlattr * const tb[]);
807 void (*activate)(const struct nft_ctx *ctx,
808 const struct nft_expr *expr);
809 void (*deactivate)(const struct nft_ctx *ctx,
810 const struct nft_expr *expr,
811 enum nft_trans_phase phase);
812 void (*destroy)(const struct nft_ctx *ctx,
813 const struct nft_expr *expr);
814 void (*destroy_clone)(const struct nft_ctx *ctx,
815 const struct nft_expr *expr);
816 int (*dump)(struct sk_buff *skb,
817 const struct nft_expr *expr);
818 int (*validate)(const struct nft_ctx *ctx,
819 const struct nft_expr *expr,
820 const struct nft_data **data);
821 bool (*gc)(struct net *net,
822 const struct nft_expr *expr);
823 int (*offload)(struct nft_offload_ctx *ctx,
824 struct nft_flow_rule *flow,
825 const struct nft_expr *expr);
827 const struct nft_expr_type *type;
831 #define NFT_EXPR_MAXATTR 16
832 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
833 ALIGN(size, __alignof__(struct nft_expr)))
836 * struct nft_expr - nf_tables expression
838 * @ops: expression ops
839 * @data: expression private data
842 const struct nft_expr_ops *ops;
844 __attribute__((aligned(__alignof__(u64))));
847 static inline void *nft_expr_priv(const struct nft_expr *expr)
849 return (void *)expr->data;
852 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
853 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
854 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
855 const struct nft_expr *expr);
858 * struct nft_rule - nf_tables rule
860 * @list: used internally
861 * @handle: rule handle
862 * @genmask: generation mask
863 * @dlen: length of expression data
864 * @udata: user data is appended to the rule
865 * @data: expression data
868 struct list_head list;
874 __attribute__((aligned(__alignof__(struct nft_expr))));
877 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
879 return (struct nft_expr *)&rule->data[0];
882 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
884 return ((void *)expr) + expr->ops->size;
887 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
889 return (struct nft_expr *)&rule->data[rule->dlen];
892 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
894 return (void *)&rule->data[rule->dlen];
897 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
899 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
900 struct nft_regs *regs,
901 const struct nft_pktinfo *pkt)
903 struct nft_expr *expr;
905 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
906 expr = nft_set_ext_expr(ext);
907 expr->ops->eval(expr, regs, pkt);
912 * The last pointer isn't really necessary, but the compiler isn't able to
913 * determine that the result of nft_expr_last() is always the same since it
914 * can't assume that the dlen value wasn't changed within calls in the loop.
916 #define nft_rule_for_each_expr(expr, last, rule) \
917 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
919 (expr) = nft_expr_next(expr))
921 #define NFT_CHAIN_POLICY_UNSET U8_MAX
924 * struct nft_chain - nf_tables chain
926 * @rules: list of rules in the chain
927 * @list: used internally
928 * @rhlhead: used internally
929 * @table: table that this chain belongs to
930 * @handle: chain handle
931 * @use: number of jump references to this chain
932 * @flags: bitmask of enum nft_chain_flags
933 * @name: name of the chain
936 struct nft_rule *__rcu *rules_gen_0;
937 struct nft_rule *__rcu *rules_gen_1;
938 struct list_head rules;
939 struct list_head list;
940 struct rhlist_head rhlhead;
941 struct nft_table *table;
951 /* Only used during control plane commit phase: */
952 struct nft_rule **rules_next;
955 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
957 enum nft_chain_types {
958 NFT_CHAIN_T_DEFAULT = 0,
965 * struct nft_chain_type - nf_tables chain type info
967 * @name: name of the type
968 * @type: numeric identifier
969 * @family: address family
970 * @owner: module owner
971 * @hook_mask: mask of valid hooks
972 * @hooks: array of hook functions
973 * @ops_register: base chain register function
974 * @ops_unregister: base chain unregister function
976 struct nft_chain_type {
978 enum nft_chain_types type;
980 struct module *owner;
981 unsigned int hook_mask;
982 nf_hookfn *hooks[NF_MAX_HOOKS];
983 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
984 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
987 int nft_chain_validate_dependency(const struct nft_chain *chain,
988 enum nft_chain_types type);
989 int nft_chain_validate_hooks(const struct nft_chain *chain,
990 unsigned int hook_flags);
992 static inline bool nft_chain_is_bound(struct nft_chain *chain)
994 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
997 void nft_chain_del(struct nft_chain *chain);
998 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1003 struct u64_stats_sync syncp;
1007 struct list_head list;
1009 struct nf_hook_ops ops;
1010 struct rcu_head rcu;
1014 * struct nft_base_chain - nf_tables base chain
1016 * @ops: netfilter hook ops
1017 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1019 * @policy: default policy
1020 * @stats: per-cpu chain stats
1022 * @flow_block: flow block (for hardware offload)
1024 struct nft_base_chain {
1025 struct nf_hook_ops ops;
1026 struct list_head hook_list;
1027 const struct nft_chain_type *type;
1030 struct nft_stats __percpu *stats;
1031 struct nft_chain chain;
1032 struct flow_block flow_block;
1035 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1037 return container_of(chain, struct nft_base_chain, chain);
1040 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1042 return chain->flags & NFT_CHAIN_BASE;
1045 int __nft_release_basechain(struct nft_ctx *ctx);
1047 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1050 * struct nft_table - nf_tables table
1052 * @list: used internally
1053 * @chains_ht: chains in the table
1054 * @chains: same, for stable walks
1055 * @sets: sets in the table
1056 * @objects: stateful objects in the table
1057 * @flowtables: flow tables in the table
1058 * @hgenerator: handle generator state
1059 * @handle: table handle
1060 * @use: number of chain references to this table
1061 * @flags: table flag (see enum nft_table_flags)
1062 * @genmask: generation mask
1063 * @afinfo: address family info
1064 * @name: name of the table
1067 struct list_head list;
1068 struct rhltable chains_ht;
1069 struct list_head chains;
1070 struct list_head sets;
1071 struct list_head objects;
1072 struct list_head flowtables;
1084 void nft_register_chain_type(const struct nft_chain_type *);
1085 void nft_unregister_chain_type(const struct nft_chain_type *);
1087 int nft_register_expr(struct nft_expr_type *);
1088 void nft_unregister_expr(struct nft_expr_type *);
1090 int nft_verdict_dump(struct sk_buff *skb, int type,
1091 const struct nft_verdict *v);
1094 * struct nft_object_hash_key - key to lookup nft_object
1096 * @name: name of the stateful object to look up
1097 * @table: table the object belongs to
1099 struct nft_object_hash_key {
1101 const struct nft_table *table;
1105 * struct nft_object - nf_tables stateful object
1107 * @list: table stateful object list node
1108 * @key: keys that identify this object
1109 * @rhlhead: nft_objname_ht node
1110 * @genmask: generation mask
1111 * @use: number of references to this stateful object
1112 * @handle: unique object handle
1113 * @ops: object operations
1114 * @data: object data, layout depends on type
1117 struct list_head list;
1118 struct rhlist_head rhlhead;
1119 struct nft_object_hash_key key;
1125 /* runtime data below here */
1126 const struct nft_object_ops *ops ____cacheline_aligned;
1127 unsigned char data[]
1128 __attribute__((aligned(__alignof__(u64))));
1131 static inline void *nft_obj_data(const struct nft_object *obj)
1133 return (void *)obj->data;
1136 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1138 struct nft_object *nft_obj_lookup(const struct net *net,
1139 const struct nft_table *table,
1140 const struct nlattr *nla, u32 objtype,
1143 void nft_obj_notify(struct net *net, const struct nft_table *table,
1144 struct nft_object *obj, u32 portid, u32 seq,
1145 int event, int family, int report, gfp_t gfp);
1148 * struct nft_object_type - stateful object type
1150 * @select_ops: function to select nft_object_ops
1151 * @ops: default ops, used when no select_ops functions is present
1152 * @list: list node in list of object types
1153 * @type: stateful object numeric type
1154 * @owner: module owner
1155 * @maxattr: maximum netlink attribute
1156 * @policy: netlink attribute policy
1158 struct nft_object_type {
1159 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1160 const struct nlattr * const tb[]);
1161 const struct nft_object_ops *ops;
1162 struct list_head list;
1164 unsigned int maxattr;
1165 struct module *owner;
1166 const struct nla_policy *policy;
1170 * struct nft_object_ops - stateful object operations
1172 * @eval: stateful object evaluation function
1173 * @size: stateful object size
1174 * @init: initialize object from netlink attributes
1175 * @destroy: release existing stateful object
1176 * @dump: netlink dump stateful object
1177 * @update: update stateful object
1179 struct nft_object_ops {
1180 void (*eval)(struct nft_object *obj,
1181 struct nft_regs *regs,
1182 const struct nft_pktinfo *pkt);
1184 int (*init)(const struct nft_ctx *ctx,
1185 const struct nlattr *const tb[],
1186 struct nft_object *obj);
1187 void (*destroy)(const struct nft_ctx *ctx,
1188 struct nft_object *obj);
1189 int (*dump)(struct sk_buff *skb,
1190 struct nft_object *obj,
1192 void (*update)(struct nft_object *obj,
1193 struct nft_object *newobj);
1194 const struct nft_object_type *type;
1197 int nft_register_obj(struct nft_object_type *obj_type);
1198 void nft_unregister_obj(struct nft_object_type *obj_type);
1200 #define NFT_NETDEVICE_MAX 256
1203 * struct nft_flowtable - nf_tables flow table
1205 * @list: flow table list node in table list
1206 * @table: the table the flow table is contained in
1207 * @name: name of this flow table
1208 * @hooknum: hook number
1209 * @ops_len: number of hooks in array
1210 * @genmask: generation mask
1211 * @use: number of references to this flow table
1212 * @handle: unique object handle
1213 * @dev_name: array of device names
1214 * @data: rhashtable and garbage collector
1215 * @ops: array of hooks
1217 struct nft_flowtable {
1218 struct list_head list;
1219 struct nft_table *table;
1226 /* runtime data below here */
1227 struct list_head hook_list ____cacheline_aligned;
1228 struct nf_flowtable data;
1231 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1232 const struct nlattr *nla,
1235 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1236 struct nft_flowtable *flowtable,
1237 enum nft_trans_phase phase);
1239 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1240 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1243 * struct nft_traceinfo - nft tracing information and state
1245 * @pkt: pktinfo currently processed
1246 * @basechain: base chain currently processed
1247 * @chain: chain currently processed
1248 * @rule: rule that was evaluated
1249 * @verdict: verdict given by rule
1250 * @type: event type (enum nft_trace_types)
1251 * @packet_dumped: packet headers sent in a previous traceinfo message
1252 * @trace: other struct members are initialised
1254 struct nft_traceinfo {
1255 const struct nft_pktinfo *pkt;
1256 const struct nft_base_chain *basechain;
1257 const struct nft_chain *chain;
1258 const struct nft_rule *rule;
1259 const struct nft_verdict *verdict;
1260 enum nft_trace_types type;
1265 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1266 const struct nft_verdict *verdict,
1267 const struct nft_chain *basechain);
1269 void nft_trace_notify(struct nft_traceinfo *info);
1271 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1272 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1274 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1275 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1277 #define MODULE_ALIAS_NFT_EXPR(name) \
1278 MODULE_ALIAS("nft-expr-" name)
1280 #define MODULE_ALIAS_NFT_OBJ(type) \
1281 MODULE_ALIAS("nft-obj-" __stringify(type))
1283 #if IS_ENABLED(CONFIG_NF_TABLES)
1286 * The gencursor defines two generations, the currently active and the
1287 * next one. Objects contain a bitmask of 2 bits specifying the generations
1288 * they're active in. A set bit means they're inactive in the generation
1289 * represented by that bit.
1291 * New objects start out as inactive in the current and active in the
1292 * next generation. When committing the ruleset the bitmask is cleared,
1293 * meaning they're active in all generations. When removing an object,
1294 * it is set inactive in the next generation. After committing the ruleset,
1295 * the objects are removed.
1297 static inline unsigned int nft_gencursor_next(const struct net *net)
1299 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1302 static inline u8 nft_genmask_next(const struct net *net)
1304 return 1 << nft_gencursor_next(net);
1307 static inline u8 nft_genmask_cur(const struct net *net)
1309 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1310 return 1 << READ_ONCE(net->nft.gencursor);
1313 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1316 * Generic transaction helpers
1319 /* Check if this object is currently active. */
1320 #define nft_is_active(__net, __obj) \
1321 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1323 /* Check if this object is active in the next generation. */
1324 #define nft_is_active_next(__net, __obj) \
1325 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1327 /* This object becomes active in the next generation. */
1328 #define nft_activate_next(__net, __obj) \
1329 (__obj)->genmask = nft_genmask_cur(__net)
1331 /* This object becomes inactive in the next generation. */
1332 #define nft_deactivate_next(__net, __obj) \
1333 (__obj)->genmask = nft_genmask_next(__net)
1335 /* After committing the ruleset, clear the stale generation bit. */
1336 #define nft_clear(__net, __obj) \
1337 (__obj)->genmask &= ~nft_genmask_next(__net)
1338 #define nft_active_genmask(__obj, __genmask) \
1339 !((__obj)->genmask & __genmask)
1342 * Set element transaction helpers
1345 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1348 return !(ext->genmask & genmask);
1351 static inline void nft_set_elem_change_active(const struct net *net,
1352 const struct nft_set *set,
1353 struct nft_set_ext *ext)
1355 ext->genmask ^= nft_genmask_next(net);
1358 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1361 * We use a free bit in the genmask field to indicate the element
1362 * is busy, meaning it is currently being processed either by
1363 * the netlink API or GC.
1365 * Even though the genmask is only a single byte wide, this works
1366 * because the extension structure if fully constant once initialized,
1367 * so there are no non-atomic write accesses unless it is already
1370 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1372 #if defined(__LITTLE_ENDIAN_BITFIELD)
1373 #define NFT_SET_ELEM_BUSY_BIT 2
1374 #elif defined(__BIG_ENDIAN_BITFIELD)
1375 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1380 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1382 unsigned long *word = (unsigned long *)ext;
1384 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1385 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1388 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1390 unsigned long *word = (unsigned long *)ext;
1392 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1396 * struct nft_trans - nf_tables object update in transaction
1398 * @list: used internally
1399 * @msg_type: message type
1400 * @put_net: ctx->net needs to be put
1401 * @ctx: transaction context
1402 * @data: internal information related to the transaction
1405 struct list_head list;
1412 struct nft_trans_rule {
1413 struct nft_rule *rule;
1414 struct nft_flow_rule *flow;
1418 #define nft_trans_rule(trans) \
1419 (((struct nft_trans_rule *)trans->data)->rule)
1420 #define nft_trans_flow_rule(trans) \
1421 (((struct nft_trans_rule *)trans->data)->flow)
1422 #define nft_trans_rule_id(trans) \
1423 (((struct nft_trans_rule *)trans->data)->rule_id)
1425 struct nft_trans_set {
1426 struct nft_set *set;
1431 #define nft_trans_set(trans) \
1432 (((struct nft_trans_set *)trans->data)->set)
1433 #define nft_trans_set_id(trans) \
1434 (((struct nft_trans_set *)trans->data)->set_id)
1435 #define nft_trans_set_bound(trans) \
1436 (((struct nft_trans_set *)trans->data)->bound)
1438 struct nft_trans_chain {
1441 struct nft_stats __percpu *stats;
1446 #define nft_trans_chain_update(trans) \
1447 (((struct nft_trans_chain *)trans->data)->update)
1448 #define nft_trans_chain_name(trans) \
1449 (((struct nft_trans_chain *)trans->data)->name)
1450 #define nft_trans_chain_stats(trans) \
1451 (((struct nft_trans_chain *)trans->data)->stats)
1452 #define nft_trans_chain_policy(trans) \
1453 (((struct nft_trans_chain *)trans->data)->policy)
1454 #define nft_trans_chain_id(trans) \
1455 (((struct nft_trans_chain *)trans->data)->chain_id)
1457 struct nft_trans_table {
1462 #define nft_trans_table_update(trans) \
1463 (((struct nft_trans_table *)trans->data)->update)
1464 #define nft_trans_table_enable(trans) \
1465 (((struct nft_trans_table *)trans->data)->enable)
1467 struct nft_trans_elem {
1468 struct nft_set *set;
1469 struct nft_set_elem elem;
1473 #define nft_trans_elem_set(trans) \
1474 (((struct nft_trans_elem *)trans->data)->set)
1475 #define nft_trans_elem(trans) \
1476 (((struct nft_trans_elem *)trans->data)->elem)
1477 #define nft_trans_elem_set_bound(trans) \
1478 (((struct nft_trans_elem *)trans->data)->bound)
1480 struct nft_trans_obj {
1481 struct nft_object *obj;
1482 struct nft_object *newobj;
1486 #define nft_trans_obj(trans) \
1487 (((struct nft_trans_obj *)trans->data)->obj)
1488 #define nft_trans_obj_newobj(trans) \
1489 (((struct nft_trans_obj *)trans->data)->newobj)
1490 #define nft_trans_obj_update(trans) \
1491 (((struct nft_trans_obj *)trans->data)->update)
1493 struct nft_trans_flowtable {
1494 struct nft_flowtable *flowtable;
1496 struct list_head hook_list;
1499 #define nft_trans_flowtable(trans) \
1500 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1501 #define nft_trans_flowtable_update(trans) \
1502 (((struct nft_trans_flowtable *)trans->data)->update)
1503 #define nft_trans_flowtable_hooks(trans) \
1504 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1506 int __init nft_chain_filter_init(void);
1507 void nft_chain_filter_fini(void);
1509 void __init nft_chain_route_init(void);
1510 void nft_chain_route_fini(void);
1512 void nf_tables_trans_destroy_flush_work(void);
1513 #endif /* _NET_NF_TABLES_H */