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;
429 __attribute__((aligned(__alignof__(u64))));
432 static inline bool nft_set_is_anonymous(const struct nft_set *set)
434 return set->flags & NFT_SET_ANONYMOUS;
437 static inline void *nft_set_priv(const struct nft_set *set)
439 return (void *)set->data;
442 static inline struct nft_set *nft_set_container_of(const void *priv)
444 return (void *)priv - offsetof(struct nft_set, data);
447 struct nft_set *nft_set_lookup_global(const struct net *net,
448 const struct nft_table *table,
449 const struct nlattr *nla_set_name,
450 const struct nlattr *nla_set_id,
453 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
455 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
459 * struct nft_set_binding - nf_tables set binding
461 * @list: set bindings list node
462 * @chain: chain containing the rule bound to the set
463 * @flags: set action flags
465 * A set binding contains all information necessary for validation
466 * of new elements added to a bound set.
468 struct nft_set_binding {
469 struct list_head list;
470 const struct nft_chain *chain;
474 enum nft_trans_phase;
475 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
476 struct nft_set_binding *binding,
477 enum nft_trans_phase phase);
478 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
479 struct nft_set_binding *binding);
480 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
483 * enum nft_set_extensions - set extension type IDs
485 * @NFT_SET_EXT_KEY: element key
486 * @NFT_SET_EXT_DATA: mapping data
487 * @NFT_SET_EXT_FLAGS: element flags
488 * @NFT_SET_EXT_TIMEOUT: element timeout
489 * @NFT_SET_EXT_EXPIRATION: element expiration time
490 * @NFT_SET_EXT_USERDATA: user data associated with the element
491 * @NFT_SET_EXT_EXPR: expression assiociated with the element
492 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
493 * @NFT_SET_EXT_NUM: number of extension types
495 enum nft_set_extensions {
500 NFT_SET_EXT_EXPIRATION,
501 NFT_SET_EXT_USERDATA,
508 * struct nft_set_ext_type - set extension type
510 * @len: fixed part length of the extension
511 * @align: alignment requirements of the extension
513 struct nft_set_ext_type {
518 extern const struct nft_set_ext_type nft_set_ext_types[];
521 * struct nft_set_ext_tmpl - set extension template
523 * @len: length of extension area
524 * @offset: offsets of individual extension types
526 struct nft_set_ext_tmpl {
528 u8 offset[NFT_SET_EXT_NUM];
532 * struct nft_set_ext - set extensions
534 * @genmask: generation mask
535 * @offset: offsets of individual extension types
536 * @data: beginning of extension data
540 u8 offset[NFT_SET_EXT_NUM];
544 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
546 memset(tmpl, 0, sizeof(*tmpl));
547 tmpl->len = sizeof(struct nft_set_ext);
550 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
553 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
554 BUG_ON(tmpl->len > U8_MAX);
555 tmpl->offset[id] = tmpl->len;
556 tmpl->len += nft_set_ext_types[id].len + len;
559 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
561 nft_set_ext_add_length(tmpl, id, 0);
564 static inline void nft_set_ext_init(struct nft_set_ext *ext,
565 const struct nft_set_ext_tmpl *tmpl)
567 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
570 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
572 return !!ext->offset[id];
575 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
577 return ext && __nft_set_ext_exists(ext, id);
580 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
582 return (void *)ext + ext->offset[id];
585 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
587 return nft_set_ext(ext, NFT_SET_EXT_KEY);
590 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
592 return nft_set_ext(ext, NFT_SET_EXT_DATA);
595 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
597 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
600 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
602 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
605 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
607 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
610 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
612 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
615 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
617 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
620 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
622 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
623 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
626 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
629 return elem + set->ops->elemsize;
632 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
634 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
637 void *nft_set_elem_init(const struct nft_set *set,
638 const struct nft_set_ext_tmpl *tmpl,
639 const u32 *key, const u32 *data,
640 u64 timeout, u64 expiration, gfp_t gfp);
641 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
645 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
648 * @set: set the elements belong to
649 * @cnt: count of elements
651 struct nft_set_gc_batch_head {
653 const struct nft_set *set;
657 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
658 sizeof(struct nft_set_gc_batch_head)) / \
662 * struct nft_set_gc_batch - nf_tables set garbage collection batch
664 * @head: GC batch head
665 * @elems: garbage collection elements
667 struct nft_set_gc_batch {
668 struct nft_set_gc_batch_head head;
669 void *elems[NFT_SET_GC_BATCH_SIZE];
672 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
674 void nft_set_gc_batch_release(struct rcu_head *rcu);
676 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
679 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
682 static inline struct nft_set_gc_batch *
683 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
687 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
689 nft_set_gc_batch_complete(gcb);
691 return nft_set_gc_batch_alloc(set, gfp);
694 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
697 gcb->elems[gcb->head.cnt++] = elem;
702 * struct nft_expr_type - nf_tables expression type
704 * @select_ops: function to select nft_expr_ops
705 * @release_ops: release nft_expr_ops
706 * @ops: default ops, used when no select_ops functions is present
707 * @list: used internally
709 * @owner: module reference
710 * @policy: netlink attribute policy
711 * @maxattr: highest netlink attribute number
712 * @family: address family for AF-specific types
713 * @flags: expression type flags
715 struct nft_expr_type {
716 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
717 const struct nlattr * const tb[]);
718 void (*release_ops)(const struct nft_expr_ops *ops);
719 const struct nft_expr_ops *ops;
720 struct list_head list;
722 struct module *owner;
723 const struct nla_policy *policy;
724 unsigned int maxattr;
729 #define NFT_EXPR_STATEFUL 0x1
730 #define NFT_EXPR_GC 0x2
732 enum nft_trans_phase {
739 struct nft_flow_rule;
740 struct nft_offload_ctx;
743 * struct nft_expr_ops - nf_tables expression operations
745 * @eval: Expression evaluation function
746 * @size: full expression size, including private data size
747 * @init: initialization function
748 * @activate: activate expression in the next generation
749 * @deactivate: deactivate expression in next generation
750 * @destroy: destruction function, called after synchronize_rcu
751 * @dump: function to dump parameters
752 * @type: expression type
753 * @validate: validate expression, called during loop detection
754 * @data: extra data to attach to this expression operation
757 struct nft_expr_ops {
758 void (*eval)(const struct nft_expr *expr,
759 struct nft_regs *regs,
760 const struct nft_pktinfo *pkt);
761 int (*clone)(struct nft_expr *dst,
762 const struct nft_expr *src);
765 int (*init)(const struct nft_ctx *ctx,
766 const struct nft_expr *expr,
767 const struct nlattr * const tb[]);
768 void (*activate)(const struct nft_ctx *ctx,
769 const struct nft_expr *expr);
770 void (*deactivate)(const struct nft_ctx *ctx,
771 const struct nft_expr *expr,
772 enum nft_trans_phase phase);
773 void (*destroy)(const struct nft_ctx *ctx,
774 const struct nft_expr *expr);
775 void (*destroy_clone)(const struct nft_ctx *ctx,
776 const struct nft_expr *expr);
777 int (*dump)(struct sk_buff *skb,
778 const struct nft_expr *expr);
779 int (*validate)(const struct nft_ctx *ctx,
780 const struct nft_expr *expr,
781 const struct nft_data **data);
782 bool (*gc)(struct net *net,
783 const struct nft_expr *expr);
784 int (*offload)(struct nft_offload_ctx *ctx,
785 struct nft_flow_rule *flow,
786 const struct nft_expr *expr);
788 const struct nft_expr_type *type;
792 #define NFT_EXPR_MAXATTR 16
793 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
794 ALIGN(size, __alignof__(struct nft_expr)))
797 * struct nft_expr - nf_tables expression
799 * @ops: expression ops
800 * @data: expression private data
803 const struct nft_expr_ops *ops;
804 unsigned char data[];
807 static inline void *nft_expr_priv(const struct nft_expr *expr)
809 return (void *)expr->data;
812 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
813 const struct nlattr *nla);
814 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
815 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
816 const struct nft_expr *expr);
819 * struct nft_rule - nf_tables rule
821 * @list: used internally
822 * @handle: rule handle
823 * @genmask: generation mask
824 * @dlen: length of expression data
825 * @udata: user data is appended to the rule
826 * @data: expression data
829 struct list_head list;
835 __attribute__((aligned(__alignof__(struct nft_expr))));
838 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
840 return (struct nft_expr *)&rule->data[0];
843 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
845 return ((void *)expr) + expr->ops->size;
848 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
850 return (struct nft_expr *)&rule->data[rule->dlen];
853 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
855 return (void *)&rule->data[rule->dlen];
859 * The last pointer isn't really necessary, but the compiler isn't able to
860 * determine that the result of nft_expr_last() is always the same since it
861 * can't assume that the dlen value wasn't changed within calls in the loop.
863 #define nft_rule_for_each_expr(expr, last, rule) \
864 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
866 (expr) = nft_expr_next(expr))
868 enum nft_chain_flags {
869 NFT_BASE_CHAIN = 0x1,
870 NFT_CHAIN_HW_OFFLOAD = 0x2,
874 * struct nft_chain - nf_tables chain
876 * @rules: list of rules in the chain
877 * @list: used internally
878 * @rhlhead: used internally
879 * @table: table that this chain belongs to
880 * @handle: chain handle
881 * @use: number of jump references to this chain
882 * @flags: bitmask of enum nft_chain_flags
883 * @name: name of the chain
886 struct nft_rule *__rcu *rules_gen_0;
887 struct nft_rule *__rcu *rules_gen_1;
888 struct list_head rules;
889 struct list_head list;
890 struct rhlist_head rhlhead;
891 struct nft_table *table;
898 /* Only used during control plane commit phase: */
899 struct nft_rule **rules_next;
902 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
904 enum nft_chain_types {
905 NFT_CHAIN_T_DEFAULT = 0,
912 * struct nft_chain_type - nf_tables chain type info
914 * @name: name of the type
915 * @type: numeric identifier
916 * @family: address family
917 * @owner: module owner
918 * @hook_mask: mask of valid hooks
919 * @hooks: array of hook functions
920 * @ops_register: base chain register function
921 * @ops_unregister: base chain unregister function
923 struct nft_chain_type {
925 enum nft_chain_types type;
927 struct module *owner;
928 unsigned int hook_mask;
929 nf_hookfn *hooks[NF_MAX_HOOKS];
930 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
931 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
934 int nft_chain_validate_dependency(const struct nft_chain *chain,
935 enum nft_chain_types type);
936 int nft_chain_validate_hooks(const struct nft_chain *chain,
937 unsigned int hook_flags);
942 struct u64_stats_sync syncp;
946 * struct nft_base_chain - nf_tables base chain
948 * @ops: netfilter hook ops
950 * @policy: default policy
951 * @stats: per-cpu chain stats
953 * @dev_name: device name that this base chain is attached to (if any)
954 * @flow_block: flow block (for hardware offload)
956 struct nft_base_chain {
957 struct nf_hook_ops ops;
958 const struct nft_chain_type *type;
961 struct nft_stats __percpu *stats;
962 struct nft_chain chain;
963 char dev_name[IFNAMSIZ];
964 struct flow_block flow_block;
967 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
969 return container_of(chain, struct nft_base_chain, chain);
972 static inline bool nft_is_base_chain(const struct nft_chain *chain)
974 return chain->flags & NFT_BASE_CHAIN;
977 int __nft_release_basechain(struct nft_ctx *ctx);
979 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
982 * struct nft_table - nf_tables table
984 * @list: used internally
985 * @chains_ht: chains in the table
986 * @chains: same, for stable walks
987 * @sets: sets in the table
988 * @objects: stateful objects in the table
989 * @flowtables: flow tables in the table
990 * @hgenerator: handle generator state
991 * @handle: table handle
992 * @use: number of chain references to this table
993 * @flags: table flag (see enum nft_table_flags)
994 * @genmask: generation mask
995 * @afinfo: address family info
996 * @name: name of the table
999 struct list_head list;
1000 struct rhltable chains_ht;
1001 struct list_head chains;
1002 struct list_head sets;
1003 struct list_head objects;
1004 struct list_head flowtables;
1014 void nft_register_chain_type(const struct nft_chain_type *);
1015 void nft_unregister_chain_type(const struct nft_chain_type *);
1017 int nft_register_expr(struct nft_expr_type *);
1018 void nft_unregister_expr(struct nft_expr_type *);
1020 int nft_verdict_dump(struct sk_buff *skb, int type,
1021 const struct nft_verdict *v);
1024 * struct nft_object_hash_key - key to lookup nft_object
1026 * @name: name of the stateful object to look up
1027 * @table: table the object belongs to
1029 struct nft_object_hash_key {
1031 const struct nft_table *table;
1035 * struct nft_object - nf_tables stateful object
1037 * @list: table stateful object list node
1038 * @key: keys that identify this object
1039 * @rhlhead: nft_objname_ht node
1040 * @genmask: generation mask
1041 * @use: number of references to this stateful object
1042 * @handle: unique object handle
1043 * @ops: object operations
1044 * @data: object data, layout depends on type
1047 struct list_head list;
1048 struct rhlist_head rhlhead;
1049 struct nft_object_hash_key key;
1053 /* runtime data below here */
1054 const struct nft_object_ops *ops ____cacheline_aligned;
1055 unsigned char data[]
1056 __attribute__((aligned(__alignof__(u64))));
1059 static inline void *nft_obj_data(const struct nft_object *obj)
1061 return (void *)obj->data;
1064 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1066 struct nft_object *nft_obj_lookup(const struct net *net,
1067 const struct nft_table *table,
1068 const struct nlattr *nla, u32 objtype,
1071 void nft_obj_notify(struct net *net, const struct nft_table *table,
1072 struct nft_object *obj, u32 portid, u32 seq,
1073 int event, int family, int report, gfp_t gfp);
1076 * struct nft_object_type - stateful object type
1078 * @select_ops: function to select nft_object_ops
1079 * @ops: default ops, used when no select_ops functions is present
1080 * @list: list node in list of object types
1081 * @type: stateful object numeric type
1082 * @owner: module owner
1083 * @maxattr: maximum netlink attribute
1084 * @policy: netlink attribute policy
1086 struct nft_object_type {
1087 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1088 const struct nlattr * const tb[]);
1089 const struct nft_object_ops *ops;
1090 struct list_head list;
1092 unsigned int maxattr;
1093 struct module *owner;
1094 const struct nla_policy *policy;
1098 * struct nft_object_ops - stateful object operations
1100 * @eval: stateful object evaluation function
1101 * @size: stateful object size
1102 * @init: initialize object from netlink attributes
1103 * @destroy: release existing stateful object
1104 * @dump: netlink dump stateful object
1106 struct nft_object_ops {
1107 void (*eval)(struct nft_object *obj,
1108 struct nft_regs *regs,
1109 const struct nft_pktinfo *pkt);
1111 int (*init)(const struct nft_ctx *ctx,
1112 const struct nlattr *const tb[],
1113 struct nft_object *obj);
1114 void (*destroy)(const struct nft_ctx *ctx,
1115 struct nft_object *obj);
1116 int (*dump)(struct sk_buff *skb,
1117 struct nft_object *obj,
1119 const struct nft_object_type *type;
1122 int nft_register_obj(struct nft_object_type *obj_type);
1123 void nft_unregister_obj(struct nft_object_type *obj_type);
1125 #define NFT_FLOWTABLE_DEVICE_MAX 8
1128 * struct nft_flowtable - nf_tables flow table
1130 * @list: flow table list node in table list
1131 * @table: the table the flow table is contained in
1132 * @name: name of this flow table
1133 * @hooknum: hook number
1134 * @priority: hook priority
1135 * @ops_len: number of hooks in array
1136 * @genmask: generation mask
1137 * @use: number of references to this flow table
1138 * @handle: unique object handle
1139 * @dev_name: array of device names
1140 * @data: rhashtable and garbage collector
1141 * @ops: array of hooks
1143 struct nft_flowtable {
1144 struct list_head list;
1145 struct nft_table *table;
1153 /* runtime data below here */
1154 struct nf_hook_ops *ops ____cacheline_aligned;
1155 struct nf_flowtable data;
1158 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1159 const struct nlattr *nla,
1162 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1163 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1166 * struct nft_traceinfo - nft tracing information and state
1168 * @pkt: pktinfo currently processed
1169 * @basechain: base chain currently processed
1170 * @chain: chain currently processed
1171 * @rule: rule that was evaluated
1172 * @verdict: verdict given by rule
1173 * @type: event type (enum nft_trace_types)
1174 * @packet_dumped: packet headers sent in a previous traceinfo message
1175 * @trace: other struct members are initialised
1177 struct nft_traceinfo {
1178 const struct nft_pktinfo *pkt;
1179 const struct nft_base_chain *basechain;
1180 const struct nft_chain *chain;
1181 const struct nft_rule *rule;
1182 const struct nft_verdict *verdict;
1183 enum nft_trace_types type;
1188 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1189 const struct nft_verdict *verdict,
1190 const struct nft_chain *basechain);
1192 void nft_trace_notify(struct nft_traceinfo *info);
1194 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1195 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1197 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1198 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1200 #define MODULE_ALIAS_NFT_EXPR(name) \
1201 MODULE_ALIAS("nft-expr-" name)
1203 #define MODULE_ALIAS_NFT_SET() \
1204 MODULE_ALIAS("nft-set")
1206 #define MODULE_ALIAS_NFT_OBJ(type) \
1207 MODULE_ALIAS("nft-obj-" __stringify(type))
1210 * The gencursor defines two generations, the currently active and the
1211 * next one. Objects contain a bitmask of 2 bits specifying the generations
1212 * they're active in. A set bit means they're inactive in the generation
1213 * represented by that bit.
1215 * New objects start out as inactive in the current and active in the
1216 * next generation. When committing the ruleset the bitmask is cleared,
1217 * meaning they're active in all generations. When removing an object,
1218 * it is set inactive in the next generation. After committing the ruleset,
1219 * the objects are removed.
1221 static inline unsigned int nft_gencursor_next(const struct net *net)
1223 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1226 static inline u8 nft_genmask_next(const struct net *net)
1228 return 1 << nft_gencursor_next(net);
1231 static inline u8 nft_genmask_cur(const struct net *net)
1233 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1234 return 1 << READ_ONCE(net->nft.gencursor);
1237 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1240 * Generic transaction helpers
1243 /* Check if this object is currently active. */
1244 #define nft_is_active(__net, __obj) \
1245 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1247 /* Check if this object is active in the next generation. */
1248 #define nft_is_active_next(__net, __obj) \
1249 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1251 /* This object becomes active in the next generation. */
1252 #define nft_activate_next(__net, __obj) \
1253 (__obj)->genmask = nft_genmask_cur(__net)
1255 /* This object becomes inactive in the next generation. */
1256 #define nft_deactivate_next(__net, __obj) \
1257 (__obj)->genmask = nft_genmask_next(__net)
1259 /* After committing the ruleset, clear the stale generation bit. */
1260 #define nft_clear(__net, __obj) \
1261 (__obj)->genmask &= ~nft_genmask_next(__net)
1262 #define nft_active_genmask(__obj, __genmask) \
1263 !((__obj)->genmask & __genmask)
1266 * Set element transaction helpers
1269 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1272 return !(ext->genmask & genmask);
1275 static inline void nft_set_elem_change_active(const struct net *net,
1276 const struct nft_set *set,
1277 struct nft_set_ext *ext)
1279 ext->genmask ^= nft_genmask_next(net);
1283 * We use a free bit in the genmask field to indicate the element
1284 * is busy, meaning it is currently being processed either by
1285 * the netlink API or GC.
1287 * Even though the genmask is only a single byte wide, this works
1288 * because the extension structure if fully constant once initialized,
1289 * so there are no non-atomic write accesses unless it is already
1292 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1294 #if defined(__LITTLE_ENDIAN_BITFIELD)
1295 #define NFT_SET_ELEM_BUSY_BIT 2
1296 #elif defined(__BIG_ENDIAN_BITFIELD)
1297 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1302 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1304 unsigned long *word = (unsigned long *)ext;
1306 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1307 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1310 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1312 unsigned long *word = (unsigned long *)ext;
1314 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1318 * struct nft_trans - nf_tables object update in transaction
1320 * @list: used internally
1321 * @msg_type: message type
1322 * @put_net: ctx->net needs to be put
1323 * @ctx: transaction context
1324 * @data: internal information related to the transaction
1327 struct list_head list;
1334 struct nft_trans_rule {
1335 struct nft_rule *rule;
1336 struct nft_flow_rule *flow;
1340 #define nft_trans_rule(trans) \
1341 (((struct nft_trans_rule *)trans->data)->rule)
1342 #define nft_trans_flow_rule(trans) \
1343 (((struct nft_trans_rule *)trans->data)->flow)
1344 #define nft_trans_rule_id(trans) \
1345 (((struct nft_trans_rule *)trans->data)->rule_id)
1347 struct nft_trans_set {
1348 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)
1357 #define nft_trans_set_bound(trans) \
1358 (((struct nft_trans_set *)trans->data)->bound)
1360 struct nft_trans_chain {
1363 struct nft_stats __percpu *stats;
1367 #define nft_trans_chain_update(trans) \
1368 (((struct nft_trans_chain *)trans->data)->update)
1369 #define nft_trans_chain_name(trans) \
1370 (((struct nft_trans_chain *)trans->data)->name)
1371 #define nft_trans_chain_stats(trans) \
1372 (((struct nft_trans_chain *)trans->data)->stats)
1373 #define nft_trans_chain_policy(trans) \
1374 (((struct nft_trans_chain *)trans->data)->policy)
1376 struct nft_trans_table {
1381 #define nft_trans_table_update(trans) \
1382 (((struct nft_trans_table *)trans->data)->update)
1383 #define nft_trans_table_enable(trans) \
1384 (((struct nft_trans_table *)trans->data)->enable)
1386 struct nft_trans_elem {
1387 struct nft_set *set;
1388 struct nft_set_elem elem;
1392 #define nft_trans_elem_set(trans) \
1393 (((struct nft_trans_elem *)trans->data)->set)
1394 #define nft_trans_elem(trans) \
1395 (((struct nft_trans_elem *)trans->data)->elem)
1396 #define nft_trans_elem_set_bound(trans) \
1397 (((struct nft_trans_elem *)trans->data)->bound)
1399 struct nft_trans_obj {
1400 struct nft_object *obj;
1403 #define nft_trans_obj(trans) \
1404 (((struct nft_trans_obj *)trans->data)->obj)
1406 struct nft_trans_flowtable {
1407 struct nft_flowtable *flowtable;
1410 #define nft_trans_flowtable(trans) \
1411 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1413 int __init nft_chain_filter_init(void);
1414 void nft_chain_filter_fini(void);
1416 void __init nft_chain_route_init(void);
1417 void nft_chain_route_fini(void);
1418 #endif /* _NET_NF_TABLES_H */