1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <linux/list.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter/nfnetlink.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/nf_tables.h>
10 #include <linux/u64_stats_sync.h>
11 #include <linux/rhashtable.h>
12 #include <net/netfilter/nf_flow_table.h>
13 #include <net/netlink.h>
14 #include <net/flow_offload.h>
18 #define NFT_JUMP_STACK_SIZE 16
24 /* for x_tables compatibility */
25 struct xt_action_param xt;
28 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
30 return pkt->xt.state->net;
33 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
35 return pkt->xt.state->hook;
38 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
40 return pkt->xt.state->pf;
43 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
45 return pkt->xt.state->in;
48 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
50 return pkt->xt.state->out;
53 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
55 const struct nf_hook_state *state)
58 pkt->xt.state = state;
61 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
64 pkt->tprot_set = false;
71 * struct nft_verdict - nf_tables verdict
73 * @code: nf_tables/netfilter verdict code
74 * @chain: destination chain for NFT_JUMP/NFT_GOTO
78 struct nft_chain *chain;
84 struct nft_verdict verdict;
86 } __attribute__((aligned(__alignof__(u64))));
89 * struct nft_regs - nf_tables register set
91 * @data: data registers
92 * @verdict: verdict register
94 * The first four data registers alias to the verdict register.
99 struct nft_verdict verdict;
103 /* Store/load an u16 or u8 integer to/from the u32 data register.
105 * Note, when using concatenations, register allocation happens at 32-bit
106 * level. So for store instruction, pad the rest part with zero to avoid
110 static inline void nft_reg_store16(u32 *dreg, u16 val)
116 static inline void nft_reg_store8(u32 *dreg, u8 val)
122 static inline u16 nft_reg_load16(u32 *sreg)
127 static inline u8 nft_reg_load8(u32 *sreg)
132 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
135 memcpy(dst, src, len);
138 static inline void nft_data_debug(const struct nft_data *data)
140 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
141 data->data[0], data->data[1],
142 data->data[2], data->data[3]);
146 * struct nft_ctx - nf_tables rule/set context
148 * @net: net namespace
149 * @table: the table the chain is contained in
150 * @chain: the chain the rule is contained in
151 * @nla: netlink attributes
152 * @portid: netlink portID of the original message
153 * @seq: netlink sequence number
154 * @family: protocol family
155 * @level: depth of the chains
156 * @report: notify via unicast netlink message
160 struct nft_table *table;
161 struct nft_chain *chain;
162 const struct nlattr * const *nla;
171 struct nft_data_desc {
172 enum nft_data_types type;
176 int nft_data_init(const struct nft_ctx *ctx,
177 struct nft_data *data, unsigned int size,
178 struct nft_data_desc *desc, const struct nlattr *nla);
179 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
180 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
181 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
182 enum nft_data_types type, unsigned int len);
184 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
186 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
189 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
191 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
194 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
195 unsigned int nft_parse_register(const struct nlattr *attr);
196 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
198 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
199 int nft_validate_register_store(const struct nft_ctx *ctx,
200 enum nft_registers reg,
201 const struct nft_data *data,
202 enum nft_data_types type, unsigned int len);
205 * struct nft_userdata - user defined data associated with an object
207 * @len: length of the data
210 * The presence of user data is indicated in an object specific fashion,
211 * so a length of zero can't occur and the value "len" indicates data
214 struct nft_userdata {
216 unsigned char data[0];
220 * struct nft_set_elem - generic representation of set elements
223 * @priv: element private data and extensions
225 struct nft_set_elem {
227 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
234 struct nft_set_iter {
239 int (*fn)(const struct nft_ctx *ctx,
241 const struct nft_set_iter *iter,
242 struct nft_set_elem *elem);
246 * struct nft_set_desc - description of set elements
250 * @size: number of set elements
252 struct nft_set_desc {
259 * enum nft_set_class - performance class
261 * @NFT_LOOKUP_O_1: constant, O(1)
262 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
263 * @NFT_LOOKUP_O_N: linear, O(N)
267 NFT_SET_CLASS_O_LOG_N,
272 * struct nft_set_estimate - estimation of memory and performance
275 * @size: required memory
276 * @lookup: lookup performance class
277 * @space: memory class
279 struct nft_set_estimate {
281 enum nft_set_class lookup;
282 enum nft_set_class space;
289 * struct nft_set_ops - nf_tables set operations
291 * @lookup: look up an element within the set
292 * @insert: insert new element into set
293 * @activate: activate new element in the next generation
294 * @deactivate: lookup for element and deactivate it in the next generation
295 * @flush: deactivate element in the next generation
296 * @remove: remove element from set
297 * @walk: iterate over all set elemeennts
298 * @get: get set elements
299 * @privsize: function to return size of set private data
300 * @init: initialize private data of new set instance
301 * @destroy: destroy private data of set instance
302 * @elemsize: element private size
305 bool (*lookup)(const struct net *net,
306 const struct nft_set *set,
308 const struct nft_set_ext **ext);
309 bool (*update)(struct nft_set *set,
311 void *(*new)(struct nft_set *,
312 const struct nft_expr *,
314 const struct nft_expr *expr,
315 struct nft_regs *regs,
316 const struct nft_set_ext **ext);
318 int (*insert)(const struct net *net,
319 const struct nft_set *set,
320 const struct nft_set_elem *elem,
321 struct nft_set_ext **ext);
322 void (*activate)(const struct net *net,
323 const struct nft_set *set,
324 const struct nft_set_elem *elem);
325 void * (*deactivate)(const struct net *net,
326 const struct nft_set *set,
327 const struct nft_set_elem *elem);
328 bool (*flush)(const struct net *net,
329 const struct nft_set *set,
331 void (*remove)(const struct net *net,
332 const struct nft_set *set,
333 const struct nft_set_elem *elem);
334 void (*walk)(const struct nft_ctx *ctx,
336 struct nft_set_iter *iter);
337 void * (*get)(const struct net *net,
338 const struct nft_set *set,
339 const struct nft_set_elem *elem,
342 u64 (*privsize)(const struct nlattr * const nla[],
343 const struct nft_set_desc *desc);
344 bool (*estimate)(const struct nft_set_desc *desc,
346 struct nft_set_estimate *est);
347 int (*init)(const struct nft_set *set,
348 const struct nft_set_desc *desc,
349 const struct nlattr * const nla[]);
350 void (*destroy)(const struct nft_set *set);
351 void (*gc_init)(const struct nft_set *set);
353 unsigned int elemsize;
357 * struct nft_set_type - nf_tables set type
359 * @ops: set ops for this type
360 * @list: used internally
361 * @owner: module reference
362 * @features: features supported by the implementation
364 struct nft_set_type {
365 const struct nft_set_ops ops;
366 struct list_head list;
367 struct module *owner;
370 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
372 int nft_register_set(struct nft_set_type *type);
373 void nft_unregister_set(struct nft_set_type *type);
376 * struct nft_set - nf_tables set instance
378 * @list: table set list node
379 * @bindings: list of set bindings
380 * @table: table this set belongs to
381 * @net: netnamespace this set belongs to
382 * @name: name of the set
383 * @handle: unique handle of the set
384 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
385 * @dtype: data type (verdict or numeric type defined by userspace)
386 * @objtype: object type (see NFT_OBJECT_* definitions)
387 * @size: maximum set size
388 * @use: number of rules references to this set
389 * @nelems: number of elements
390 * @ndeact: number of deactivated elements queued for removal
391 * @timeout: default timeout value in jiffies
392 * @gc_int: garbage collection interval in msecs
393 * @policy: set parameterization (see enum nft_set_policies)
394 * @udlen: user data length
398 * @genmask: generation mask
401 * @data: private set data
404 struct list_head list;
405 struct list_head bindings;
406 struct nft_table *table;
421 unsigned char *udata;
422 /* runtime data below here */
423 const struct nft_set_ops *ops ____cacheline_aligned;
430 __attribute__((aligned(__alignof__(u64))));
433 static inline bool nft_set_is_anonymous(const struct nft_set *set)
435 return set->flags & NFT_SET_ANONYMOUS;
438 static inline void *nft_set_priv(const struct nft_set *set)
440 return (void *)set->data;
443 static inline struct nft_set *nft_set_container_of(const void *priv)
445 return (void *)priv - offsetof(struct nft_set, data);
448 struct nft_set *nft_set_lookup_global(const struct net *net,
449 const struct nft_table *table,
450 const struct nlattr *nla_set_name,
451 const struct nlattr *nla_set_id,
454 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
456 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
460 * struct nft_set_binding - nf_tables set binding
462 * @list: set bindings list node
463 * @chain: chain containing the rule bound to the set
464 * @flags: set action flags
466 * A set binding contains all information necessary for validation
467 * of new elements added to a bound set.
469 struct nft_set_binding {
470 struct list_head list;
471 const struct nft_chain *chain;
475 enum nft_trans_phase;
476 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
477 struct nft_set_binding *binding,
478 enum nft_trans_phase phase);
479 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
480 struct nft_set_binding *binding);
481 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
484 * enum nft_set_extensions - set extension type IDs
486 * @NFT_SET_EXT_KEY: element key
487 * @NFT_SET_EXT_DATA: mapping data
488 * @NFT_SET_EXT_FLAGS: element flags
489 * @NFT_SET_EXT_TIMEOUT: element timeout
490 * @NFT_SET_EXT_EXPIRATION: element expiration time
491 * @NFT_SET_EXT_USERDATA: user data associated with the element
492 * @NFT_SET_EXT_EXPR: expression assiociated with the element
493 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
494 * @NFT_SET_EXT_NUM: number of extension types
496 enum nft_set_extensions {
501 NFT_SET_EXT_EXPIRATION,
502 NFT_SET_EXT_USERDATA,
509 * struct nft_set_ext_type - set extension type
511 * @len: fixed part length of the extension
512 * @align: alignment requirements of the extension
514 struct nft_set_ext_type {
519 extern const struct nft_set_ext_type nft_set_ext_types[];
522 * struct nft_set_ext_tmpl - set extension template
524 * @len: length of extension area
525 * @offset: offsets of individual extension types
527 struct nft_set_ext_tmpl {
529 u8 offset[NFT_SET_EXT_NUM];
533 * struct nft_set_ext - set extensions
535 * @genmask: generation mask
536 * @offset: offsets of individual extension types
537 * @data: beginning of extension data
541 u8 offset[NFT_SET_EXT_NUM];
545 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
547 memset(tmpl, 0, sizeof(*tmpl));
548 tmpl->len = sizeof(struct nft_set_ext);
551 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
554 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
555 BUG_ON(tmpl->len > U8_MAX);
556 tmpl->offset[id] = tmpl->len;
557 tmpl->len += nft_set_ext_types[id].len + len;
560 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
562 nft_set_ext_add_length(tmpl, id, 0);
565 static inline void nft_set_ext_init(struct nft_set_ext *ext,
566 const struct nft_set_ext_tmpl *tmpl)
568 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
571 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
573 return !!ext->offset[id];
576 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
578 return ext && __nft_set_ext_exists(ext, id);
581 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
583 return (void *)ext + ext->offset[id];
586 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
588 return nft_set_ext(ext, NFT_SET_EXT_KEY);
591 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
593 return nft_set_ext(ext, NFT_SET_EXT_DATA);
596 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
598 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
601 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
603 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
606 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
608 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
611 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
613 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
616 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
618 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
621 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
623 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
624 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
627 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
630 return elem + set->ops->elemsize;
633 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
635 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
638 void *nft_set_elem_init(const struct nft_set *set,
639 const struct nft_set_ext_tmpl *tmpl,
640 const u32 *key, const u32 *data,
641 u64 timeout, u64 expiration, gfp_t gfp);
642 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
646 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
649 * @set: set the elements belong to
650 * @cnt: count of elements
652 struct nft_set_gc_batch_head {
654 const struct nft_set *set;
658 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
659 sizeof(struct nft_set_gc_batch_head)) / \
663 * struct nft_set_gc_batch - nf_tables set garbage collection batch
665 * @head: GC batch head
666 * @elems: garbage collection elements
668 struct nft_set_gc_batch {
669 struct nft_set_gc_batch_head head;
670 void *elems[NFT_SET_GC_BATCH_SIZE];
673 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
675 void nft_set_gc_batch_release(struct rcu_head *rcu);
677 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
680 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
683 static inline struct nft_set_gc_batch *
684 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
688 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
690 nft_set_gc_batch_complete(gcb);
692 return nft_set_gc_batch_alloc(set, gfp);
695 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
698 gcb->elems[gcb->head.cnt++] = elem;
703 * struct nft_expr_type - nf_tables expression type
705 * @select_ops: function to select nft_expr_ops
706 * @release_ops: release nft_expr_ops
707 * @ops: default ops, used when no select_ops functions is present
708 * @list: used internally
710 * @owner: module reference
711 * @policy: netlink attribute policy
712 * @maxattr: highest netlink attribute number
713 * @family: address family for AF-specific types
714 * @flags: expression type flags
716 struct nft_expr_type {
717 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
718 const struct nlattr * const tb[]);
719 void (*release_ops)(const struct nft_expr_ops *ops);
720 const struct nft_expr_ops *ops;
721 struct list_head list;
723 struct module *owner;
724 const struct nla_policy *policy;
725 unsigned int maxattr;
730 #define NFT_EXPR_STATEFUL 0x1
731 #define NFT_EXPR_GC 0x2
733 enum nft_trans_phase {
740 struct nft_flow_rule;
741 struct nft_offload_ctx;
744 * struct nft_expr_ops - nf_tables expression operations
746 * @eval: Expression evaluation function
747 * @size: full expression size, including private data size
748 * @init: initialization function
749 * @activate: activate expression in the next generation
750 * @deactivate: deactivate expression in next generation
751 * @destroy: destruction function, called after synchronize_rcu
752 * @dump: function to dump parameters
753 * @type: expression type
754 * @validate: validate expression, called during loop detection
755 * @data: extra data to attach to this expression operation
758 struct nft_expr_ops {
759 void (*eval)(const struct nft_expr *expr,
760 struct nft_regs *regs,
761 const struct nft_pktinfo *pkt);
762 int (*clone)(struct nft_expr *dst,
763 const struct nft_expr *src);
766 int (*init)(const struct nft_ctx *ctx,
767 const struct nft_expr *expr,
768 const struct nlattr * const tb[]);
769 void (*activate)(const struct nft_ctx *ctx,
770 const struct nft_expr *expr);
771 void (*deactivate)(const struct nft_ctx *ctx,
772 const struct nft_expr *expr,
773 enum nft_trans_phase phase);
774 void (*destroy)(const struct nft_ctx *ctx,
775 const struct nft_expr *expr);
776 void (*destroy_clone)(const struct nft_ctx *ctx,
777 const struct nft_expr *expr);
778 int (*dump)(struct sk_buff *skb,
779 const struct nft_expr *expr);
780 int (*validate)(const struct nft_ctx *ctx,
781 const struct nft_expr *expr,
782 const struct nft_data **data);
783 bool (*gc)(struct net *net,
784 const struct nft_expr *expr);
785 int (*offload)(struct nft_offload_ctx *ctx,
786 struct nft_flow_rule *flow,
787 const struct nft_expr *expr);
789 const struct nft_expr_type *type;
793 #define NFT_EXPR_MAXATTR 16
794 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
795 ALIGN(size, __alignof__(struct nft_expr)))
798 * struct nft_expr - nf_tables expression
800 * @ops: expression ops
801 * @data: expression private data
804 const struct nft_expr_ops *ops;
805 unsigned char data[];
808 static inline void *nft_expr_priv(const struct nft_expr *expr)
810 return (void *)expr->data;
813 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
814 const struct nlattr *nla);
815 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
816 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
817 const struct nft_expr *expr);
820 * struct nft_rule - nf_tables rule
822 * @list: used internally
823 * @handle: rule handle
824 * @genmask: generation mask
825 * @dlen: length of expression data
826 * @udata: user data is appended to the rule
827 * @data: expression data
830 struct list_head list;
836 __attribute__((aligned(__alignof__(struct nft_expr))));
839 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
841 return (struct nft_expr *)&rule->data[0];
844 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
846 return ((void *)expr) + expr->ops->size;
849 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
851 return (struct nft_expr *)&rule->data[rule->dlen];
854 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
856 return (void *)&rule->data[rule->dlen];
860 * The last pointer isn't really necessary, but the compiler isn't able to
861 * determine that the result of nft_expr_last() is always the same since it
862 * can't assume that the dlen value wasn't changed within calls in the loop.
864 #define nft_rule_for_each_expr(expr, last, rule) \
865 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
867 (expr) = nft_expr_next(expr))
869 enum nft_chain_flags {
870 NFT_BASE_CHAIN = 0x1,
871 NFT_CHAIN_HW_OFFLOAD = 0x2,
875 * struct nft_chain - nf_tables chain
877 * @rules: list of rules in the chain
878 * @list: used internally
879 * @rhlhead: used internally
880 * @table: table that this chain belongs to
881 * @handle: chain handle
882 * @use: number of jump references to this chain
883 * @flags: bitmask of enum nft_chain_flags
884 * @name: name of the chain
887 struct nft_rule *__rcu *rules_gen_0;
888 struct nft_rule *__rcu *rules_gen_1;
889 struct list_head rules;
890 struct list_head list;
891 struct rhlist_head rhlhead;
892 struct nft_table *table;
899 /* Only used during control plane commit phase: */
900 struct nft_rule **rules_next;
903 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
905 enum nft_chain_types {
906 NFT_CHAIN_T_DEFAULT = 0,
913 * struct nft_chain_type - nf_tables chain type info
915 * @name: name of the type
916 * @type: numeric identifier
917 * @family: address family
918 * @owner: module owner
919 * @hook_mask: mask of valid hooks
920 * @hooks: array of hook functions
921 * @ops_register: base chain register function
922 * @ops_unregister: base chain unregister function
924 struct nft_chain_type {
926 enum nft_chain_types type;
928 struct module *owner;
929 unsigned int hook_mask;
930 nf_hookfn *hooks[NF_MAX_HOOKS];
931 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
932 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
935 int nft_chain_validate_dependency(const struct nft_chain *chain,
936 enum nft_chain_types type);
937 int nft_chain_validate_hooks(const struct nft_chain *chain,
938 unsigned int hook_flags);
943 struct u64_stats_sync syncp;
947 * struct nft_base_chain - nf_tables base chain
949 * @ops: netfilter hook ops
951 * @policy: default policy
952 * @stats: per-cpu chain stats
954 * @dev_name: device name that this base chain is attached to (if any)
955 * @flow_block: flow block (for hardware offload)
957 struct nft_base_chain {
958 struct nf_hook_ops ops;
959 const struct nft_chain_type *type;
962 struct nft_stats __percpu *stats;
963 struct nft_chain chain;
964 char dev_name[IFNAMSIZ];
965 struct flow_block flow_block;
968 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
970 return container_of(chain, struct nft_base_chain, chain);
973 static inline bool nft_is_base_chain(const struct nft_chain *chain)
975 return chain->flags & NFT_BASE_CHAIN;
978 int __nft_release_basechain(struct nft_ctx *ctx);
980 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
983 * struct nft_table - nf_tables table
985 * @list: used internally
986 * @chains_ht: chains in the table
987 * @chains: same, for stable walks
988 * @sets: sets in the table
989 * @objects: stateful objects in the table
990 * @flowtables: flow tables in the table
991 * @hgenerator: handle generator state
992 * @handle: table handle
993 * @use: number of chain references to this table
994 * @flags: table flag (see enum nft_table_flags)
995 * @genmask: generation mask
996 * @afinfo: address family info
997 * @name: name of the table
1000 struct list_head list;
1001 struct rhltable chains_ht;
1002 struct list_head chains;
1003 struct list_head sets;
1004 struct list_head objects;
1005 struct list_head flowtables;
1015 void nft_register_chain_type(const struct nft_chain_type *);
1016 void nft_unregister_chain_type(const struct nft_chain_type *);
1018 int nft_register_expr(struct nft_expr_type *);
1019 void nft_unregister_expr(struct nft_expr_type *);
1021 int nft_verdict_dump(struct sk_buff *skb, int type,
1022 const struct nft_verdict *v);
1025 * struct nft_object_hash_key - key to lookup nft_object
1027 * @name: name of the stateful object to look up
1028 * @table: table the object belongs to
1030 struct nft_object_hash_key {
1032 const struct nft_table *table;
1036 * struct nft_object - nf_tables stateful object
1038 * @list: table stateful object list node
1039 * @key: keys that identify this object
1040 * @rhlhead: nft_objname_ht node
1041 * @genmask: generation mask
1042 * @use: number of references to this stateful object
1043 * @handle: unique object handle
1044 * @ops: object operations
1045 * @data: object data, layout depends on type
1048 struct list_head list;
1049 struct rhlist_head rhlhead;
1050 struct nft_object_hash_key key;
1054 /* runtime data below here */
1055 const struct nft_object_ops *ops ____cacheline_aligned;
1056 unsigned char data[]
1057 __attribute__((aligned(__alignof__(u64))));
1060 static inline void *nft_obj_data(const struct nft_object *obj)
1062 return (void *)obj->data;
1065 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1067 struct nft_object *nft_obj_lookup(const struct net *net,
1068 const struct nft_table *table,
1069 const struct nlattr *nla, u32 objtype,
1072 void nft_obj_notify(struct net *net, const struct nft_table *table,
1073 struct nft_object *obj, u32 portid, u32 seq,
1074 int event, int family, int report, gfp_t gfp);
1077 * struct nft_object_type - stateful object type
1079 * @select_ops: function to select nft_object_ops
1080 * @ops: default ops, used when no select_ops functions is present
1081 * @list: list node in list of object types
1082 * @type: stateful object numeric type
1083 * @owner: module owner
1084 * @maxattr: maximum netlink attribute
1085 * @policy: netlink attribute policy
1087 struct nft_object_type {
1088 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1089 const struct nlattr * const tb[]);
1090 const struct nft_object_ops *ops;
1091 struct list_head list;
1093 unsigned int maxattr;
1094 struct module *owner;
1095 const struct nla_policy *policy;
1099 * struct nft_object_ops - stateful object operations
1101 * @eval: stateful object evaluation function
1102 * @size: stateful object size
1103 * @init: initialize object from netlink attributes
1104 * @destroy: release existing stateful object
1105 * @dump: netlink dump stateful object
1107 struct nft_object_ops {
1108 void (*eval)(struct nft_object *obj,
1109 struct nft_regs *regs,
1110 const struct nft_pktinfo *pkt);
1112 int (*init)(const struct nft_ctx *ctx,
1113 const struct nlattr *const tb[],
1114 struct nft_object *obj);
1115 void (*destroy)(const struct nft_ctx *ctx,
1116 struct nft_object *obj);
1117 int (*dump)(struct sk_buff *skb,
1118 struct nft_object *obj,
1120 const struct nft_object_type *type;
1123 int nft_register_obj(struct nft_object_type *obj_type);
1124 void nft_unregister_obj(struct nft_object_type *obj_type);
1126 #define NFT_FLOWTABLE_DEVICE_MAX 8
1129 * struct nft_flowtable - nf_tables flow table
1131 * @list: flow table list node in table list
1132 * @table: the table the flow table is contained in
1133 * @name: name of this flow table
1134 * @hooknum: hook number
1135 * @priority: hook priority
1136 * @ops_len: number of hooks in array
1137 * @genmask: generation mask
1138 * @use: number of references to this flow table
1139 * @handle: unique object handle
1140 * @dev_name: array of device names
1141 * @data: rhashtable and garbage collector
1142 * @ops: array of hooks
1144 struct nft_flowtable {
1145 struct list_head list;
1146 struct nft_table *table;
1154 /* runtime data below here */
1155 struct nf_hook_ops *ops ____cacheline_aligned;
1156 struct nf_flowtable data;
1159 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1160 const struct nlattr *nla,
1163 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1164 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1167 * struct nft_traceinfo - nft tracing information and state
1169 * @pkt: pktinfo currently processed
1170 * @basechain: base chain currently processed
1171 * @chain: chain currently processed
1172 * @rule: rule that was evaluated
1173 * @verdict: verdict given by rule
1174 * @type: event type (enum nft_trace_types)
1175 * @packet_dumped: packet headers sent in a previous traceinfo message
1176 * @trace: other struct members are initialised
1178 struct nft_traceinfo {
1179 const struct nft_pktinfo *pkt;
1180 const struct nft_base_chain *basechain;
1181 const struct nft_chain *chain;
1182 const struct nft_rule *rule;
1183 const struct nft_verdict *verdict;
1184 enum nft_trace_types type;
1189 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1190 const struct nft_verdict *verdict,
1191 const struct nft_chain *basechain);
1193 void nft_trace_notify(struct nft_traceinfo *info);
1195 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1196 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1198 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1199 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1201 #define MODULE_ALIAS_NFT_EXPR(name) \
1202 MODULE_ALIAS("nft-expr-" name)
1204 #define MODULE_ALIAS_NFT_SET() \
1205 MODULE_ALIAS("nft-set")
1207 #define MODULE_ALIAS_NFT_OBJ(type) \
1208 MODULE_ALIAS("nft-obj-" __stringify(type))
1211 * The gencursor defines two generations, the currently active and the
1212 * next one. Objects contain a bitmask of 2 bits specifying the generations
1213 * they're active in. A set bit means they're inactive in the generation
1214 * represented by that bit.
1216 * New objects start out as inactive in the current and active in the
1217 * next generation. When committing the ruleset the bitmask is cleared,
1218 * meaning they're active in all generations. When removing an object,
1219 * it is set inactive in the next generation. After committing the ruleset,
1220 * the objects are removed.
1222 static inline unsigned int nft_gencursor_next(const struct net *net)
1224 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1227 static inline u8 nft_genmask_next(const struct net *net)
1229 return 1 << nft_gencursor_next(net);
1232 static inline u8 nft_genmask_cur(const struct net *net)
1234 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1235 return 1 << READ_ONCE(net->nft.gencursor);
1238 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1241 * Generic transaction helpers
1244 /* Check if this object is currently active. */
1245 #define nft_is_active(__net, __obj) \
1246 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1248 /* Check if this object is active in the next generation. */
1249 #define nft_is_active_next(__net, __obj) \
1250 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1252 /* This object becomes active in the next generation. */
1253 #define nft_activate_next(__net, __obj) \
1254 (__obj)->genmask = nft_genmask_cur(__net)
1256 /* This object becomes inactive in the next generation. */
1257 #define nft_deactivate_next(__net, __obj) \
1258 (__obj)->genmask = nft_genmask_next(__net)
1260 /* After committing the ruleset, clear the stale generation bit. */
1261 #define nft_clear(__net, __obj) \
1262 (__obj)->genmask &= ~nft_genmask_next(__net)
1263 #define nft_active_genmask(__obj, __genmask) \
1264 !((__obj)->genmask & __genmask)
1267 * Set element transaction helpers
1270 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1273 return !(ext->genmask & genmask);
1276 static inline void nft_set_elem_change_active(const struct net *net,
1277 const struct nft_set *set,
1278 struct nft_set_ext *ext)
1280 ext->genmask ^= nft_genmask_next(net);
1284 * We use a free bit in the genmask field to indicate the element
1285 * is busy, meaning it is currently being processed either by
1286 * the netlink API or GC.
1288 * Even though the genmask is only a single byte wide, this works
1289 * because the extension structure if fully constant once initialized,
1290 * so there are no non-atomic write accesses unless it is already
1293 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1295 #if defined(__LITTLE_ENDIAN_BITFIELD)
1296 #define NFT_SET_ELEM_BUSY_BIT 2
1297 #elif defined(__BIG_ENDIAN_BITFIELD)
1298 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1303 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1305 unsigned long *word = (unsigned long *)ext;
1307 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1308 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1311 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1313 unsigned long *word = (unsigned long *)ext;
1315 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1319 * struct nft_trans - nf_tables object update in transaction
1321 * @list: used internally
1322 * @msg_type: message type
1323 * @put_net: ctx->net needs to be put
1324 * @ctx: transaction context
1325 * @data: internal information related to the transaction
1328 struct list_head list;
1335 struct nft_trans_rule {
1336 struct nft_rule *rule;
1337 struct nft_flow_rule *flow;
1341 #define nft_trans_rule(trans) \
1342 (((struct nft_trans_rule *)trans->data)->rule)
1343 #define nft_trans_flow_rule(trans) \
1344 (((struct nft_trans_rule *)trans->data)->flow)
1345 #define nft_trans_rule_id(trans) \
1346 (((struct nft_trans_rule *)trans->data)->rule_id)
1348 struct nft_trans_set {
1349 struct nft_set *set;
1353 #define nft_trans_set(trans) \
1354 (((struct nft_trans_set *)trans->data)->set)
1355 #define nft_trans_set_id(trans) \
1356 (((struct nft_trans_set *)trans->data)->set_id)
1358 struct nft_trans_chain {
1361 struct nft_stats __percpu *stats;
1365 #define nft_trans_chain_update(trans) \
1366 (((struct nft_trans_chain *)trans->data)->update)
1367 #define nft_trans_chain_name(trans) \
1368 (((struct nft_trans_chain *)trans->data)->name)
1369 #define nft_trans_chain_stats(trans) \
1370 (((struct nft_trans_chain *)trans->data)->stats)
1371 #define nft_trans_chain_policy(trans) \
1372 (((struct nft_trans_chain *)trans->data)->policy)
1374 struct nft_trans_table {
1379 #define nft_trans_table_update(trans) \
1380 (((struct nft_trans_table *)trans->data)->update)
1381 #define nft_trans_table_enable(trans) \
1382 (((struct nft_trans_table *)trans->data)->enable)
1384 struct nft_trans_elem {
1385 struct nft_set *set;
1386 struct nft_set_elem elem;
1389 #define nft_trans_elem_set(trans) \
1390 (((struct nft_trans_elem *)trans->data)->set)
1391 #define nft_trans_elem(trans) \
1392 (((struct nft_trans_elem *)trans->data)->elem)
1394 struct nft_trans_obj {
1395 struct nft_object *obj;
1398 #define nft_trans_obj(trans) \
1399 (((struct nft_trans_obj *)trans->data)->obj)
1401 struct nft_trans_flowtable {
1402 struct nft_flowtable *flowtable;
1405 #define nft_trans_flowtable(trans) \
1406 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1408 int __init nft_chain_filter_init(void);
1409 void nft_chain_filter_fini(void);
1411 void __init nft_chain_route_init(void);
1412 void nft_chain_route_fini(void);
1413 #endif /* _NET_NF_TABLES_H */