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(u32 *sreg)
122 static inline void nft_reg_store16(u32 *dreg, u16 val)
128 static inline u16 nft_reg_load16(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(u32 *sreg)
140 return get_unaligned((u64 *)sreg);
143 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
146 memcpy(dst, src, len);
149 static inline void nft_data_debug(const struct nft_data *data)
151 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
152 data->data[0], data->data[1],
153 data->data[2], data->data[3]);
157 * struct nft_ctx - nf_tables rule/set context
159 * @net: net namespace
160 * @table: the table the chain is contained in
161 * @chain: the chain the rule is contained in
162 * @nla: netlink attributes
163 * @portid: netlink portID of the original message
164 * @seq: netlink sequence number
165 * @family: protocol family
166 * @level: depth of the chains
167 * @report: notify via unicast netlink message
171 struct nft_table *table;
172 struct nft_chain *chain;
173 const struct nlattr * const *nla;
182 struct nft_data_desc {
183 enum nft_data_types type;
187 int nft_data_init(const struct nft_ctx *ctx,
188 struct nft_data *data, unsigned int size,
189 struct nft_data_desc *desc, const struct nlattr *nla);
190 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
191 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
192 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
193 enum nft_data_types type, unsigned int len);
195 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
197 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
200 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
202 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
205 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
206 unsigned int nft_parse_register(const struct nlattr *attr);
207 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
209 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
210 int nft_validate_register_store(const struct nft_ctx *ctx,
211 enum nft_registers reg,
212 const struct nft_data *data,
213 enum nft_data_types type, unsigned int len);
216 * struct nft_userdata - user defined data associated with an object
218 * @len: length of the data
221 * The presence of user data is indicated in an object specific fashion,
222 * so a length of zero can't occur and the value "len" indicates data
225 struct nft_userdata {
227 unsigned char data[0];
231 * struct nft_set_elem - generic representation of set elements
234 * @priv: element private data and extensions
236 struct nft_set_elem {
238 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
245 struct nft_set_iter {
250 int (*fn)(const struct nft_ctx *ctx,
252 const struct nft_set_iter *iter,
253 struct nft_set_elem *elem);
257 * struct nft_set_desc - description of set elements
261 * @size: number of set elements
263 struct nft_set_desc {
270 * enum nft_set_class - performance class
272 * @NFT_LOOKUP_O_1: constant, O(1)
273 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
274 * @NFT_LOOKUP_O_N: linear, O(N)
278 NFT_SET_CLASS_O_LOG_N,
283 * struct nft_set_estimate - estimation of memory and performance
286 * @size: required memory
287 * @lookup: lookup performance class
288 * @space: memory class
290 struct nft_set_estimate {
292 enum nft_set_class lookup;
293 enum nft_set_class space;
300 * struct nft_set_ops - nf_tables set operations
302 * @lookup: look up an element within the set
303 * @update: update an element if exists, add it if doesn't exist
304 * @delete: delete an element
305 * @insert: insert new element into set
306 * @activate: activate new element in the next generation
307 * @deactivate: lookup for element and deactivate it in the next generation
308 * @flush: deactivate element in the next generation
309 * @remove: remove element from set
310 * @walk: iterate over all set elements
311 * @get: get set elements
312 * @privsize: function to return size of set private data
313 * @init: initialize private data of new set instance
314 * @destroy: destroy private data of set instance
315 * @elemsize: element private size
317 * Operations lookup, update and delete have simpler interfaces, are faster
318 * and currently only used in the packet path. All the rest are slower,
319 * control plane functions.
322 bool (*lookup)(const struct net *net,
323 const struct nft_set *set,
325 const struct nft_set_ext **ext);
326 bool (*update)(struct nft_set *set,
328 void *(*new)(struct nft_set *,
329 const struct nft_expr *,
331 const struct nft_expr *expr,
332 struct nft_regs *regs,
333 const struct nft_set_ext **ext);
334 bool (*delete)(const struct nft_set *set,
337 int (*insert)(const struct net *net,
338 const struct nft_set *set,
339 const struct nft_set_elem *elem,
340 struct nft_set_ext **ext);
341 void (*activate)(const struct net *net,
342 const struct nft_set *set,
343 const struct nft_set_elem *elem);
344 void * (*deactivate)(const struct net *net,
345 const struct nft_set *set,
346 const struct nft_set_elem *elem);
347 bool (*flush)(const struct net *net,
348 const struct nft_set *set,
350 void (*remove)(const struct net *net,
351 const struct nft_set *set,
352 const struct nft_set_elem *elem);
353 void (*walk)(const struct nft_ctx *ctx,
355 struct nft_set_iter *iter);
356 void * (*get)(const struct net *net,
357 const struct nft_set *set,
358 const struct nft_set_elem *elem,
361 u64 (*privsize)(const struct nlattr * const nla[],
362 const struct nft_set_desc *desc);
363 bool (*estimate)(const struct nft_set_desc *desc,
365 struct nft_set_estimate *est);
366 int (*init)(const struct nft_set *set,
367 const struct nft_set_desc *desc,
368 const struct nlattr * const nla[]);
369 void (*destroy)(const struct nft_set *set);
370 void (*gc_init)(const struct nft_set *set);
372 unsigned int elemsize;
376 * struct nft_set_type - nf_tables set type
378 * @ops: set ops for this type
379 * @list: used internally
380 * @owner: module reference
381 * @features: features supported by the implementation
383 struct nft_set_type {
384 const struct nft_set_ops ops;
385 struct list_head list;
386 struct module *owner;
389 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
391 int nft_register_set(struct nft_set_type *type);
392 void nft_unregister_set(struct nft_set_type *type);
395 * struct nft_set - nf_tables set instance
397 * @list: table set list node
398 * @bindings: list of set bindings
399 * @table: table this set belongs to
400 * @net: netnamespace this set belongs to
401 * @name: name of the set
402 * @handle: unique handle of the set
403 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
404 * @dtype: data type (verdict or numeric type defined by userspace)
405 * @objtype: object type (see NFT_OBJECT_* definitions)
406 * @size: maximum set size
407 * @use: number of rules references to this set
408 * @nelems: number of elements
409 * @ndeact: number of deactivated elements queued for removal
410 * @timeout: default timeout value in jiffies
411 * @gc_int: garbage collection interval in msecs
412 * @policy: set parameterization (see enum nft_set_policies)
413 * @udlen: user data length
417 * @genmask: generation mask
420 * @data: private set data
423 struct list_head list;
424 struct list_head bindings;
425 struct nft_table *table;
440 unsigned char *udata;
441 /* runtime data below here */
442 const struct nft_set_ops *ops ____cacheline_aligned;
448 __attribute__((aligned(__alignof__(u64))));
451 static inline bool nft_set_is_anonymous(const struct nft_set *set)
453 return set->flags & NFT_SET_ANONYMOUS;
456 static inline void *nft_set_priv(const struct nft_set *set)
458 return (void *)set->data;
461 static inline struct nft_set *nft_set_container_of(const void *priv)
463 return (void *)priv - offsetof(struct nft_set, data);
466 struct nft_set *nft_set_lookup_global(const struct net *net,
467 const struct nft_table *table,
468 const struct nlattr *nla_set_name,
469 const struct nlattr *nla_set_id,
472 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
474 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
478 * struct nft_set_binding - nf_tables set binding
480 * @list: set bindings list node
481 * @chain: chain containing the rule bound to the set
482 * @flags: set action flags
484 * A set binding contains all information necessary for validation
485 * of new elements added to a bound set.
487 struct nft_set_binding {
488 struct list_head list;
489 const struct nft_chain *chain;
493 enum nft_trans_phase;
494 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
495 struct nft_set_binding *binding,
496 enum nft_trans_phase phase);
497 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
498 struct nft_set_binding *binding);
499 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
502 * enum nft_set_extensions - set extension type IDs
504 * @NFT_SET_EXT_KEY: element key
505 * @NFT_SET_EXT_DATA: mapping data
506 * @NFT_SET_EXT_FLAGS: element flags
507 * @NFT_SET_EXT_TIMEOUT: element timeout
508 * @NFT_SET_EXT_EXPIRATION: element expiration time
509 * @NFT_SET_EXT_USERDATA: user data associated with the element
510 * @NFT_SET_EXT_EXPR: expression assiociated with the element
511 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
512 * @NFT_SET_EXT_NUM: number of extension types
514 enum nft_set_extensions {
519 NFT_SET_EXT_EXPIRATION,
520 NFT_SET_EXT_USERDATA,
527 * struct nft_set_ext_type - set extension type
529 * @len: fixed part length of the extension
530 * @align: alignment requirements of the extension
532 struct nft_set_ext_type {
537 extern const struct nft_set_ext_type nft_set_ext_types[];
540 * struct nft_set_ext_tmpl - set extension template
542 * @len: length of extension area
543 * @offset: offsets of individual extension types
545 struct nft_set_ext_tmpl {
547 u8 offset[NFT_SET_EXT_NUM];
551 * struct nft_set_ext - set extensions
553 * @genmask: generation mask
554 * @offset: offsets of individual extension types
555 * @data: beginning of extension data
559 u8 offset[NFT_SET_EXT_NUM];
563 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
565 memset(tmpl, 0, sizeof(*tmpl));
566 tmpl->len = sizeof(struct nft_set_ext);
569 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
572 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
573 BUG_ON(tmpl->len > U8_MAX);
574 tmpl->offset[id] = tmpl->len;
575 tmpl->len += nft_set_ext_types[id].len + len;
578 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
580 nft_set_ext_add_length(tmpl, id, 0);
583 static inline void nft_set_ext_init(struct nft_set_ext *ext,
584 const struct nft_set_ext_tmpl *tmpl)
586 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
589 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
591 return !!ext->offset[id];
594 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
596 return ext && __nft_set_ext_exists(ext, id);
599 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
601 return (void *)ext + ext->offset[id];
604 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
606 return nft_set_ext(ext, NFT_SET_EXT_KEY);
609 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
611 return nft_set_ext(ext, NFT_SET_EXT_DATA);
614 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
616 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
619 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
621 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
624 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
626 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
629 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
631 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
634 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
636 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
639 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
641 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
642 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
645 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
648 return elem + set->ops->elemsize;
651 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
653 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
656 void *nft_set_elem_init(const struct nft_set *set,
657 const struct nft_set_ext_tmpl *tmpl,
658 const u32 *key, const u32 *data,
659 u64 timeout, u64 expiration, gfp_t gfp);
660 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
664 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
667 * @set: set the elements belong to
668 * @cnt: count of elements
670 struct nft_set_gc_batch_head {
672 const struct nft_set *set;
676 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
677 sizeof(struct nft_set_gc_batch_head)) / \
681 * struct nft_set_gc_batch - nf_tables set garbage collection batch
683 * @head: GC batch head
684 * @elems: garbage collection elements
686 struct nft_set_gc_batch {
687 struct nft_set_gc_batch_head head;
688 void *elems[NFT_SET_GC_BATCH_SIZE];
691 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
693 void nft_set_gc_batch_release(struct rcu_head *rcu);
695 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
698 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
701 static inline struct nft_set_gc_batch *
702 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
706 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
708 nft_set_gc_batch_complete(gcb);
710 return nft_set_gc_batch_alloc(set, gfp);
713 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
716 gcb->elems[gcb->head.cnt++] = elem;
721 * struct nft_expr_type - nf_tables expression type
723 * @select_ops: function to select nft_expr_ops
724 * @release_ops: release nft_expr_ops
725 * @ops: default ops, used when no select_ops functions is present
726 * @list: used internally
728 * @owner: module reference
729 * @policy: netlink attribute policy
730 * @maxattr: highest netlink attribute number
731 * @family: address family for AF-specific types
732 * @flags: expression type flags
734 struct nft_expr_type {
735 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
736 const struct nlattr * const tb[]);
737 void (*release_ops)(const struct nft_expr_ops *ops);
738 const struct nft_expr_ops *ops;
739 struct list_head list;
741 struct module *owner;
742 const struct nla_policy *policy;
743 unsigned int maxattr;
748 #define NFT_EXPR_STATEFUL 0x1
749 #define NFT_EXPR_GC 0x2
751 enum nft_trans_phase {
758 struct nft_flow_rule;
759 struct nft_offload_ctx;
762 * struct nft_expr_ops - nf_tables expression operations
764 * @eval: Expression evaluation function
765 * @size: full expression size, including private data size
766 * @init: initialization function
767 * @activate: activate expression in the next generation
768 * @deactivate: deactivate expression in next generation
769 * @destroy: destruction function, called after synchronize_rcu
770 * @dump: function to dump parameters
771 * @type: expression type
772 * @validate: validate expression, called during loop detection
773 * @data: extra data to attach to this expression operation
776 struct nft_expr_ops {
777 void (*eval)(const struct nft_expr *expr,
778 struct nft_regs *regs,
779 const struct nft_pktinfo *pkt);
780 int (*clone)(struct nft_expr *dst,
781 const struct nft_expr *src);
784 int (*init)(const struct nft_ctx *ctx,
785 const struct nft_expr *expr,
786 const struct nlattr * const tb[]);
787 void (*activate)(const struct nft_ctx *ctx,
788 const struct nft_expr *expr);
789 void (*deactivate)(const struct nft_ctx *ctx,
790 const struct nft_expr *expr,
791 enum nft_trans_phase phase);
792 void (*destroy)(const struct nft_ctx *ctx,
793 const struct nft_expr *expr);
794 void (*destroy_clone)(const struct nft_ctx *ctx,
795 const struct nft_expr *expr);
796 int (*dump)(struct sk_buff *skb,
797 const struct nft_expr *expr);
798 int (*validate)(const struct nft_ctx *ctx,
799 const struct nft_expr *expr,
800 const struct nft_data **data);
801 bool (*gc)(struct net *net,
802 const struct nft_expr *expr);
803 int (*offload)(struct nft_offload_ctx *ctx,
804 struct nft_flow_rule *flow,
805 const struct nft_expr *expr);
807 const struct nft_expr_type *type;
811 #define NFT_EXPR_MAXATTR 16
812 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
813 ALIGN(size, __alignof__(struct nft_expr)))
816 * struct nft_expr - nf_tables expression
818 * @ops: expression ops
819 * @data: expression private data
822 const struct nft_expr_ops *ops;
823 unsigned char data[];
826 static inline void *nft_expr_priv(const struct nft_expr *expr)
828 return (void *)expr->data;
831 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
832 const struct nlattr *nla);
833 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
834 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
835 const struct nft_expr *expr);
838 * struct nft_rule - nf_tables rule
840 * @list: used internally
841 * @handle: rule handle
842 * @genmask: generation mask
843 * @dlen: length of expression data
844 * @udata: user data is appended to the rule
845 * @data: expression data
848 struct list_head list;
854 __attribute__((aligned(__alignof__(struct nft_expr))));
857 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
859 return (struct nft_expr *)&rule->data[0];
862 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
864 return ((void *)expr) + expr->ops->size;
867 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
869 return (struct nft_expr *)&rule->data[rule->dlen];
872 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
874 return (void *)&rule->data[rule->dlen];
878 * The last pointer isn't really necessary, but the compiler isn't able to
879 * determine that the result of nft_expr_last() is always the same since it
880 * can't assume that the dlen value wasn't changed within calls in the loop.
882 #define nft_rule_for_each_expr(expr, last, rule) \
883 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
885 (expr) = nft_expr_next(expr))
887 enum nft_chain_flags {
888 NFT_BASE_CHAIN = 0x1,
889 NFT_CHAIN_HW_OFFLOAD = 0x2,
892 #define NFT_CHAIN_POLICY_UNSET U8_MAX
895 * struct nft_chain - nf_tables chain
897 * @rules: list of rules in the chain
898 * @list: used internally
899 * @rhlhead: used internally
900 * @table: table that this chain belongs to
901 * @handle: chain handle
902 * @use: number of jump references to this chain
903 * @flags: bitmask of enum nft_chain_flags
904 * @name: name of the chain
907 struct nft_rule *__rcu *rules_gen_0;
908 struct nft_rule *__rcu *rules_gen_1;
909 struct list_head rules;
910 struct list_head list;
911 struct rhlist_head rhlhead;
912 struct nft_table *table;
919 /* Only used during control plane commit phase: */
920 struct nft_rule **rules_next;
923 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
925 enum nft_chain_types {
926 NFT_CHAIN_T_DEFAULT = 0,
933 * struct nft_chain_type - nf_tables chain type info
935 * @name: name of the type
936 * @type: numeric identifier
937 * @family: address family
938 * @owner: module owner
939 * @hook_mask: mask of valid hooks
940 * @hooks: array of hook functions
941 * @ops_register: base chain register function
942 * @ops_unregister: base chain unregister function
944 struct nft_chain_type {
946 enum nft_chain_types type;
948 struct module *owner;
949 unsigned int hook_mask;
950 nf_hookfn *hooks[NF_MAX_HOOKS];
951 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
952 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
955 int nft_chain_validate_dependency(const struct nft_chain *chain,
956 enum nft_chain_types type);
957 int nft_chain_validate_hooks(const struct nft_chain *chain,
958 unsigned int hook_flags);
963 struct u64_stats_sync syncp;
967 * struct nft_base_chain - nf_tables base chain
969 * @ops: netfilter hook ops
971 * @policy: default policy
972 * @stats: per-cpu chain stats
974 * @dev_name: device name that this base chain is attached to (if any)
975 * @flow_block: flow block (for hardware offload)
977 struct nft_base_chain {
978 struct nf_hook_ops ops;
979 const struct nft_chain_type *type;
982 struct nft_stats __percpu *stats;
983 struct nft_chain chain;
984 char dev_name[IFNAMSIZ];
985 struct flow_block flow_block;
988 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
990 return container_of(chain, struct nft_base_chain, chain);
993 static inline bool nft_is_base_chain(const struct nft_chain *chain)
995 return chain->flags & NFT_BASE_CHAIN;
998 int __nft_release_basechain(struct nft_ctx *ctx);
1000 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1003 * struct nft_table - nf_tables table
1005 * @list: used internally
1006 * @chains_ht: chains in the table
1007 * @chains: same, for stable walks
1008 * @sets: sets in the table
1009 * @objects: stateful objects in the table
1010 * @flowtables: flow tables in the table
1011 * @hgenerator: handle generator state
1012 * @handle: table handle
1013 * @use: number of chain references to this table
1014 * @flags: table flag (see enum nft_table_flags)
1015 * @genmask: generation mask
1016 * @afinfo: address family info
1017 * @name: name of the table
1020 struct list_head list;
1021 struct rhltable chains_ht;
1022 struct list_head chains;
1023 struct list_head sets;
1024 struct list_head objects;
1025 struct list_head flowtables;
1035 void nft_register_chain_type(const struct nft_chain_type *);
1036 void nft_unregister_chain_type(const struct nft_chain_type *);
1038 int nft_register_expr(struct nft_expr_type *);
1039 void nft_unregister_expr(struct nft_expr_type *);
1041 int nft_verdict_dump(struct sk_buff *skb, int type,
1042 const struct nft_verdict *v);
1045 * struct nft_object_hash_key - key to lookup nft_object
1047 * @name: name of the stateful object to look up
1048 * @table: table the object belongs to
1050 struct nft_object_hash_key {
1052 const struct nft_table *table;
1056 * struct nft_object - nf_tables stateful object
1058 * @list: table stateful object list node
1059 * @key: keys that identify this object
1060 * @rhlhead: nft_objname_ht node
1061 * @genmask: generation mask
1062 * @use: number of references to this stateful object
1063 * @handle: unique object handle
1064 * @ops: object operations
1065 * @data: object data, layout depends on type
1068 struct list_head list;
1069 struct rhlist_head rhlhead;
1070 struct nft_object_hash_key key;
1074 /* runtime data below here */
1075 const struct nft_object_ops *ops ____cacheline_aligned;
1076 unsigned char data[]
1077 __attribute__((aligned(__alignof__(u64))));
1080 static inline void *nft_obj_data(const struct nft_object *obj)
1082 return (void *)obj->data;
1085 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1087 struct nft_object *nft_obj_lookup(const struct net *net,
1088 const struct nft_table *table,
1089 const struct nlattr *nla, u32 objtype,
1092 void nft_obj_notify(struct net *net, const struct nft_table *table,
1093 struct nft_object *obj, u32 portid, u32 seq,
1094 int event, int family, int report, gfp_t gfp);
1097 * struct nft_object_type - stateful object type
1099 * @select_ops: function to select nft_object_ops
1100 * @ops: default ops, used when no select_ops functions is present
1101 * @list: list node in list of object types
1102 * @type: stateful object numeric type
1103 * @owner: module owner
1104 * @maxattr: maximum netlink attribute
1105 * @policy: netlink attribute policy
1107 struct nft_object_type {
1108 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1109 const struct nlattr * const tb[]);
1110 const struct nft_object_ops *ops;
1111 struct list_head list;
1113 unsigned int maxattr;
1114 struct module *owner;
1115 const struct nla_policy *policy;
1119 * struct nft_object_ops - stateful object operations
1121 * @eval: stateful object evaluation function
1122 * @size: stateful object size
1123 * @init: initialize object from netlink attributes
1124 * @destroy: release existing stateful object
1125 * @dump: netlink dump stateful object
1126 * @update: update stateful object
1128 struct nft_object_ops {
1129 void (*eval)(struct nft_object *obj,
1130 struct nft_regs *regs,
1131 const struct nft_pktinfo *pkt);
1133 int (*init)(const struct nft_ctx *ctx,
1134 const struct nlattr *const tb[],
1135 struct nft_object *obj);
1136 void (*destroy)(const struct nft_ctx *ctx,
1137 struct nft_object *obj);
1138 int (*dump)(struct sk_buff *skb,
1139 struct nft_object *obj,
1141 void (*update)(struct nft_object *obj,
1142 struct nft_object *newobj);
1143 const struct nft_object_type *type;
1146 int nft_register_obj(struct nft_object_type *obj_type);
1147 void nft_unregister_obj(struct nft_object_type *obj_type);
1149 #define NFT_FLOWTABLE_DEVICE_MAX 8
1152 * struct nft_flowtable - nf_tables flow table
1154 * @list: flow table list node in table list
1155 * @table: the table the flow table is contained in
1156 * @name: name of this flow table
1157 * @hooknum: hook number
1158 * @priority: hook priority
1159 * @ops_len: number of hooks in array
1160 * @genmask: generation mask
1161 * @use: number of references to this flow table
1162 * @handle: unique object handle
1163 * @dev_name: array of device names
1164 * @data: rhashtable and garbage collector
1165 * @ops: array of hooks
1167 struct nft_flowtable {
1168 struct list_head list;
1169 struct nft_table *table;
1177 /* runtime data below here */
1178 struct nf_hook_ops *ops ____cacheline_aligned;
1179 struct nf_flowtable data;
1182 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1183 const struct nlattr *nla,
1186 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1187 struct nft_flowtable *flowtable,
1188 enum nft_trans_phase phase);
1190 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1191 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1194 * struct nft_traceinfo - nft tracing information and state
1196 * @pkt: pktinfo currently processed
1197 * @basechain: base chain currently processed
1198 * @chain: chain currently processed
1199 * @rule: rule that was evaluated
1200 * @verdict: verdict given by rule
1201 * @type: event type (enum nft_trace_types)
1202 * @packet_dumped: packet headers sent in a previous traceinfo message
1203 * @trace: other struct members are initialised
1205 struct nft_traceinfo {
1206 const struct nft_pktinfo *pkt;
1207 const struct nft_base_chain *basechain;
1208 const struct nft_chain *chain;
1209 const struct nft_rule *rule;
1210 const struct nft_verdict *verdict;
1211 enum nft_trace_types type;
1216 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1217 const struct nft_verdict *verdict,
1218 const struct nft_chain *basechain);
1220 void nft_trace_notify(struct nft_traceinfo *info);
1222 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1223 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1225 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1226 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1228 #define MODULE_ALIAS_NFT_EXPR(name) \
1229 MODULE_ALIAS("nft-expr-" name)
1231 #define MODULE_ALIAS_NFT_SET() \
1232 MODULE_ALIAS("nft-set")
1234 #define MODULE_ALIAS_NFT_OBJ(type) \
1235 MODULE_ALIAS("nft-obj-" __stringify(type))
1237 #if IS_ENABLED(CONFIG_NF_TABLES)
1240 * The gencursor defines two generations, the currently active and the
1241 * next one. Objects contain a bitmask of 2 bits specifying the generations
1242 * they're active in. A set bit means they're inactive in the generation
1243 * represented by that bit.
1245 * New objects start out as inactive in the current and active in the
1246 * next generation. When committing the ruleset the bitmask is cleared,
1247 * meaning they're active in all generations. When removing an object,
1248 * it is set inactive in the next generation. After committing the ruleset,
1249 * the objects are removed.
1251 static inline unsigned int nft_gencursor_next(const struct net *net)
1253 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1256 static inline u8 nft_genmask_next(const struct net *net)
1258 return 1 << nft_gencursor_next(net);
1261 static inline u8 nft_genmask_cur(const struct net *net)
1263 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1264 return 1 << READ_ONCE(net->nft.gencursor);
1267 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1270 * Generic transaction helpers
1273 /* Check if this object is currently active. */
1274 #define nft_is_active(__net, __obj) \
1275 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1277 /* Check if this object is active in the next generation. */
1278 #define nft_is_active_next(__net, __obj) \
1279 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1281 /* This object becomes active in the next generation. */
1282 #define nft_activate_next(__net, __obj) \
1283 (__obj)->genmask = nft_genmask_cur(__net)
1285 /* This object becomes inactive in the next generation. */
1286 #define nft_deactivate_next(__net, __obj) \
1287 (__obj)->genmask = nft_genmask_next(__net)
1289 /* After committing the ruleset, clear the stale generation bit. */
1290 #define nft_clear(__net, __obj) \
1291 (__obj)->genmask &= ~nft_genmask_next(__net)
1292 #define nft_active_genmask(__obj, __genmask) \
1293 !((__obj)->genmask & __genmask)
1296 * Set element transaction helpers
1299 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1302 return !(ext->genmask & genmask);
1305 static inline void nft_set_elem_change_active(const struct net *net,
1306 const struct nft_set *set,
1307 struct nft_set_ext *ext)
1309 ext->genmask ^= nft_genmask_next(net);
1312 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1315 * We use a free bit in the genmask field to indicate the element
1316 * is busy, meaning it is currently being processed either by
1317 * the netlink API or GC.
1319 * Even though the genmask is only a single byte wide, this works
1320 * because the extension structure if fully constant once initialized,
1321 * so there are no non-atomic write accesses unless it is already
1324 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1326 #if defined(__LITTLE_ENDIAN_BITFIELD)
1327 #define NFT_SET_ELEM_BUSY_BIT 2
1328 #elif defined(__BIG_ENDIAN_BITFIELD)
1329 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1334 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1336 unsigned long *word = (unsigned long *)ext;
1338 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1339 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1342 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1344 unsigned long *word = (unsigned long *)ext;
1346 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1350 * struct nft_trans - nf_tables object update in transaction
1352 * @list: used internally
1353 * @msg_type: message type
1354 * @put_net: ctx->net needs to be put
1355 * @ctx: transaction context
1356 * @data: internal information related to the transaction
1359 struct list_head list;
1366 struct nft_trans_rule {
1367 struct nft_rule *rule;
1368 struct nft_flow_rule *flow;
1372 #define nft_trans_rule(trans) \
1373 (((struct nft_trans_rule *)trans->data)->rule)
1374 #define nft_trans_flow_rule(trans) \
1375 (((struct nft_trans_rule *)trans->data)->flow)
1376 #define nft_trans_rule_id(trans) \
1377 (((struct nft_trans_rule *)trans->data)->rule_id)
1379 struct nft_trans_set {
1380 struct nft_set *set;
1385 #define nft_trans_set(trans) \
1386 (((struct nft_trans_set *)trans->data)->set)
1387 #define nft_trans_set_id(trans) \
1388 (((struct nft_trans_set *)trans->data)->set_id)
1389 #define nft_trans_set_bound(trans) \
1390 (((struct nft_trans_set *)trans->data)->bound)
1392 struct nft_trans_chain {
1395 struct nft_stats __percpu *stats;
1399 #define nft_trans_chain_update(trans) \
1400 (((struct nft_trans_chain *)trans->data)->update)
1401 #define nft_trans_chain_name(trans) \
1402 (((struct nft_trans_chain *)trans->data)->name)
1403 #define nft_trans_chain_stats(trans) \
1404 (((struct nft_trans_chain *)trans->data)->stats)
1405 #define nft_trans_chain_policy(trans) \
1406 (((struct nft_trans_chain *)trans->data)->policy)
1408 struct nft_trans_table {
1413 #define nft_trans_table_update(trans) \
1414 (((struct nft_trans_table *)trans->data)->update)
1415 #define nft_trans_table_enable(trans) \
1416 (((struct nft_trans_table *)trans->data)->enable)
1418 struct nft_trans_elem {
1419 struct nft_set *set;
1420 struct nft_set_elem elem;
1424 #define nft_trans_elem_set(trans) \
1425 (((struct nft_trans_elem *)trans->data)->set)
1426 #define nft_trans_elem(trans) \
1427 (((struct nft_trans_elem *)trans->data)->elem)
1428 #define nft_trans_elem_set_bound(trans) \
1429 (((struct nft_trans_elem *)trans->data)->bound)
1431 struct nft_trans_obj {
1432 struct nft_object *obj;
1433 struct nft_object *newobj;
1437 #define nft_trans_obj(trans) \
1438 (((struct nft_trans_obj *)trans->data)->obj)
1439 #define nft_trans_obj_newobj(trans) \
1440 (((struct nft_trans_obj *)trans->data)->newobj)
1441 #define nft_trans_obj_update(trans) \
1442 (((struct nft_trans_obj *)trans->data)->update)
1444 struct nft_trans_flowtable {
1445 struct nft_flowtable *flowtable;
1448 #define nft_trans_flowtable(trans) \
1449 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1451 int __init nft_chain_filter_init(void);
1452 void nft_chain_filter_fini(void);
1454 void __init nft_chain_route_init(void);
1455 void nft_chain_route_fini(void);
1456 #endif /* _NET_NF_TABLES_H */