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 #if IS_ENABLED(CONFIG_NETFILTER)
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;
63 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
66 pkt->tprot_set = false;
73 * struct nft_verdict - nf_tables verdict
75 * @code: nf_tables/netfilter verdict code
76 * @chain: destination chain for NFT_JUMP/NFT_GOTO
80 struct nft_chain *chain;
86 struct nft_verdict verdict;
88 } __attribute__((aligned(__alignof__(u64))));
91 * struct nft_regs - nf_tables register set
93 * @data: data registers
94 * @verdict: verdict register
96 * The first four data registers alias to the verdict register.
101 struct nft_verdict verdict;
105 /* Store/load an u16 or u8 integer to/from the u32 data register.
107 * Note, when using concatenations, register allocation happens at 32-bit
108 * level. So for store instruction, pad the rest part with zero to avoid
112 static inline void nft_reg_store16(u32 *dreg, u16 val)
118 static inline void nft_reg_store8(u32 *dreg, u8 val)
124 static inline u16 nft_reg_load16(u32 *sreg)
129 static inline u8 nft_reg_load8(u32 *sreg)
134 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
137 memcpy(dst, src, len);
140 static inline void nft_data_debug(const struct nft_data *data)
142 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
143 data->data[0], data->data[1],
144 data->data[2], data->data[3]);
148 * struct nft_ctx - nf_tables rule/set context
150 * @net: net namespace
151 * @table: the table the chain is contained in
152 * @chain: the chain the rule is contained in
153 * @nla: netlink attributes
154 * @portid: netlink portID of the original message
155 * @seq: netlink sequence number
156 * @family: protocol family
157 * @level: depth of the chains
158 * @report: notify via unicast netlink message
162 struct nft_table *table;
163 struct nft_chain *chain;
164 const struct nlattr * const *nla;
173 struct nft_data_desc {
174 enum nft_data_types type;
178 int nft_data_init(const struct nft_ctx *ctx,
179 struct nft_data *data, unsigned int size,
180 struct nft_data_desc *desc, const struct nlattr *nla);
181 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
182 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
183 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
184 enum nft_data_types type, unsigned int len);
186 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
188 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
191 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
193 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
196 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
197 unsigned int nft_parse_register(const struct nlattr *attr);
198 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
200 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
201 int nft_validate_register_store(const struct nft_ctx *ctx,
202 enum nft_registers reg,
203 const struct nft_data *data,
204 enum nft_data_types type, unsigned int len);
207 * struct nft_userdata - user defined data associated with an object
209 * @len: length of the data
212 * The presence of user data is indicated in an object specific fashion,
213 * so a length of zero can't occur and the value "len" indicates data
216 struct nft_userdata {
218 unsigned char data[0];
222 * struct nft_set_elem - generic representation of set elements
225 * @priv: element private data and extensions
227 struct nft_set_elem {
229 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
236 struct nft_set_iter {
241 int (*fn)(const struct nft_ctx *ctx,
243 const struct nft_set_iter *iter,
244 struct nft_set_elem *elem);
248 * struct nft_set_desc - description of set elements
252 * @size: number of set elements
254 struct nft_set_desc {
261 * enum nft_set_class - performance class
263 * @NFT_LOOKUP_O_1: constant, O(1)
264 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
265 * @NFT_LOOKUP_O_N: linear, O(N)
269 NFT_SET_CLASS_O_LOG_N,
274 * struct nft_set_estimate - estimation of memory and performance
277 * @size: required memory
278 * @lookup: lookup performance class
279 * @space: memory class
281 struct nft_set_estimate {
283 enum nft_set_class lookup;
284 enum nft_set_class space;
291 * struct nft_set_ops - nf_tables set operations
293 * @lookup: look up an element within the set
294 * @insert: insert new element into set
295 * @activate: activate new element in the next generation
296 * @deactivate: lookup for element and deactivate it in the next generation
297 * @flush: deactivate element in the next generation
298 * @remove: remove element from set
299 * @walk: iterate over all set elemeennts
300 * @get: get set elements
301 * @privsize: function to return size of set private data
302 * @init: initialize private data of new set instance
303 * @destroy: destroy private data of set instance
304 * @elemsize: element private size
307 bool (*lookup)(const struct net *net,
308 const struct nft_set *set,
310 const struct nft_set_ext **ext);
311 bool (*update)(struct nft_set *set,
313 void *(*new)(struct nft_set *,
314 const struct nft_expr *,
316 const struct nft_expr *expr,
317 struct nft_regs *regs,
318 const struct nft_set_ext **ext);
320 int (*insert)(const struct net *net,
321 const struct nft_set *set,
322 const struct nft_set_elem *elem,
323 struct nft_set_ext **ext);
324 void (*activate)(const struct net *net,
325 const struct nft_set *set,
326 const struct nft_set_elem *elem);
327 void * (*deactivate)(const struct net *net,
328 const struct nft_set *set,
329 const struct nft_set_elem *elem);
330 bool (*flush)(const struct net *net,
331 const struct nft_set *set,
333 void (*remove)(const struct net *net,
334 const struct nft_set *set,
335 const struct nft_set_elem *elem);
336 void (*walk)(const struct nft_ctx *ctx,
338 struct nft_set_iter *iter);
339 void * (*get)(const struct net *net,
340 const struct nft_set *set,
341 const struct nft_set_elem *elem,
344 u64 (*privsize)(const struct nlattr * const nla[],
345 const struct nft_set_desc *desc);
346 bool (*estimate)(const struct nft_set_desc *desc,
348 struct nft_set_estimate *est);
349 int (*init)(const struct nft_set *set,
350 const struct nft_set_desc *desc,
351 const struct nlattr * const nla[]);
352 void (*destroy)(const struct nft_set *set);
353 void (*gc_init)(const struct nft_set *set);
355 unsigned int elemsize;
359 * struct nft_set_type - nf_tables set type
361 * @ops: set ops for this type
362 * @list: used internally
363 * @owner: module reference
364 * @features: features supported by the implementation
366 struct nft_set_type {
367 const struct nft_set_ops ops;
368 struct list_head list;
369 struct module *owner;
372 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
374 int nft_register_set(struct nft_set_type *type);
375 void nft_unregister_set(struct nft_set_type *type);
378 * struct nft_set - nf_tables set instance
380 * @list: table set list node
381 * @bindings: list of set bindings
382 * @table: table this set belongs to
383 * @net: netnamespace this set belongs to
384 * @name: name of the set
385 * @handle: unique handle of the set
386 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
387 * @dtype: data type (verdict or numeric type defined by userspace)
388 * @objtype: object type (see NFT_OBJECT_* definitions)
389 * @size: maximum set size
390 * @use: number of rules references to this set
391 * @nelems: number of elements
392 * @ndeact: number of deactivated elements queued for removal
393 * @timeout: default timeout value in jiffies
394 * @gc_int: garbage collection interval in msecs
395 * @policy: set parameterization (see enum nft_set_policies)
396 * @udlen: user data length
400 * @genmask: generation mask
403 * @data: private set data
406 struct list_head list;
407 struct list_head bindings;
408 struct nft_table *table;
423 unsigned char *udata;
424 /* runtime data below here */
425 const struct nft_set_ops *ops ____cacheline_aligned;
432 __attribute__((aligned(__alignof__(u64))));
435 static inline bool nft_set_is_anonymous(const struct nft_set *set)
437 return set->flags & NFT_SET_ANONYMOUS;
440 static inline void *nft_set_priv(const struct nft_set *set)
442 return (void *)set->data;
445 static inline struct nft_set *nft_set_container_of(const void *priv)
447 return (void *)priv - offsetof(struct nft_set, data);
450 struct nft_set *nft_set_lookup_global(const struct net *net,
451 const struct nft_table *table,
452 const struct nlattr *nla_set_name,
453 const struct nlattr *nla_set_id,
456 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
458 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
462 * struct nft_set_binding - nf_tables set binding
464 * @list: set bindings list node
465 * @chain: chain containing the rule bound to the set
466 * @flags: set action flags
468 * A set binding contains all information necessary for validation
469 * of new elements added to a bound set.
471 struct nft_set_binding {
472 struct list_head list;
473 const struct nft_chain *chain;
477 enum nft_trans_phase;
478 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
479 struct nft_set_binding *binding,
480 enum nft_trans_phase phase);
481 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
482 struct nft_set_binding *binding);
483 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
486 * enum nft_set_extensions - set extension type IDs
488 * @NFT_SET_EXT_KEY: element key
489 * @NFT_SET_EXT_DATA: mapping data
490 * @NFT_SET_EXT_FLAGS: element flags
491 * @NFT_SET_EXT_TIMEOUT: element timeout
492 * @NFT_SET_EXT_EXPIRATION: element expiration time
493 * @NFT_SET_EXT_USERDATA: user data associated with the element
494 * @NFT_SET_EXT_EXPR: expression assiociated with the element
495 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
496 * @NFT_SET_EXT_NUM: number of extension types
498 enum nft_set_extensions {
503 NFT_SET_EXT_EXPIRATION,
504 NFT_SET_EXT_USERDATA,
511 * struct nft_set_ext_type - set extension type
513 * @len: fixed part length of the extension
514 * @align: alignment requirements of the extension
516 struct nft_set_ext_type {
521 extern const struct nft_set_ext_type nft_set_ext_types[];
524 * struct nft_set_ext_tmpl - set extension template
526 * @len: length of extension area
527 * @offset: offsets of individual extension types
529 struct nft_set_ext_tmpl {
531 u8 offset[NFT_SET_EXT_NUM];
535 * struct nft_set_ext - set extensions
537 * @genmask: generation mask
538 * @offset: offsets of individual extension types
539 * @data: beginning of extension data
543 u8 offset[NFT_SET_EXT_NUM];
547 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
549 memset(tmpl, 0, sizeof(*tmpl));
550 tmpl->len = sizeof(struct nft_set_ext);
553 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
556 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
557 BUG_ON(tmpl->len > U8_MAX);
558 tmpl->offset[id] = tmpl->len;
559 tmpl->len += nft_set_ext_types[id].len + len;
562 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
564 nft_set_ext_add_length(tmpl, id, 0);
567 static inline void nft_set_ext_init(struct nft_set_ext *ext,
568 const struct nft_set_ext_tmpl *tmpl)
570 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
573 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
575 return !!ext->offset[id];
578 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
580 return ext && __nft_set_ext_exists(ext, id);
583 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
585 return (void *)ext + ext->offset[id];
588 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
590 return nft_set_ext(ext, NFT_SET_EXT_KEY);
593 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
595 return nft_set_ext(ext, NFT_SET_EXT_DATA);
598 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
600 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
603 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
605 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
608 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
610 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
613 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
615 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
618 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
620 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
623 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
625 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
626 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
629 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
632 return elem + set->ops->elemsize;
635 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
637 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
640 void *nft_set_elem_init(const struct nft_set *set,
641 const struct nft_set_ext_tmpl *tmpl,
642 const u32 *key, const u32 *data,
643 u64 timeout, u64 expiration, gfp_t gfp);
644 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
648 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
651 * @set: set the elements belong to
652 * @cnt: count of elements
654 struct nft_set_gc_batch_head {
656 const struct nft_set *set;
660 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
661 sizeof(struct nft_set_gc_batch_head)) / \
665 * struct nft_set_gc_batch - nf_tables set garbage collection batch
667 * @head: GC batch head
668 * @elems: garbage collection elements
670 struct nft_set_gc_batch {
671 struct nft_set_gc_batch_head head;
672 void *elems[NFT_SET_GC_BATCH_SIZE];
675 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
677 void nft_set_gc_batch_release(struct rcu_head *rcu);
679 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
682 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
685 static inline struct nft_set_gc_batch *
686 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
690 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
692 nft_set_gc_batch_complete(gcb);
694 return nft_set_gc_batch_alloc(set, gfp);
697 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
700 gcb->elems[gcb->head.cnt++] = elem;
705 * struct nft_expr_type - nf_tables expression type
707 * @select_ops: function to select nft_expr_ops
708 * @release_ops: release nft_expr_ops
709 * @ops: default ops, used when no select_ops functions is present
710 * @list: used internally
712 * @owner: module reference
713 * @policy: netlink attribute policy
714 * @maxattr: highest netlink attribute number
715 * @family: address family for AF-specific types
716 * @flags: expression type flags
718 struct nft_expr_type {
719 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
720 const struct nlattr * const tb[]);
721 void (*release_ops)(const struct nft_expr_ops *ops);
722 const struct nft_expr_ops *ops;
723 struct list_head list;
725 struct module *owner;
726 const struct nla_policy *policy;
727 unsigned int maxattr;
732 #define NFT_EXPR_STATEFUL 0x1
733 #define NFT_EXPR_GC 0x2
735 enum nft_trans_phase {
742 struct nft_flow_rule;
743 struct nft_offload_ctx;
746 * struct nft_expr_ops - nf_tables expression operations
748 * @eval: Expression evaluation function
749 * @size: full expression size, including private data size
750 * @init: initialization function
751 * @activate: activate expression in the next generation
752 * @deactivate: deactivate expression in next generation
753 * @destroy: destruction function, called after synchronize_rcu
754 * @dump: function to dump parameters
755 * @type: expression type
756 * @validate: validate expression, called during loop detection
757 * @data: extra data to attach to this expression operation
760 struct nft_expr_ops {
761 void (*eval)(const struct nft_expr *expr,
762 struct nft_regs *regs,
763 const struct nft_pktinfo *pkt);
764 int (*clone)(struct nft_expr *dst,
765 const struct nft_expr *src);
768 int (*init)(const struct nft_ctx *ctx,
769 const struct nft_expr *expr,
770 const struct nlattr * const tb[]);
771 void (*activate)(const struct nft_ctx *ctx,
772 const struct nft_expr *expr);
773 void (*deactivate)(const struct nft_ctx *ctx,
774 const struct nft_expr *expr,
775 enum nft_trans_phase phase);
776 void (*destroy)(const struct nft_ctx *ctx,
777 const struct nft_expr *expr);
778 void (*destroy_clone)(const struct nft_ctx *ctx,
779 const struct nft_expr *expr);
780 int (*dump)(struct sk_buff *skb,
781 const struct nft_expr *expr);
782 int (*validate)(const struct nft_ctx *ctx,
783 const struct nft_expr *expr,
784 const struct nft_data **data);
785 bool (*gc)(struct net *net,
786 const struct nft_expr *expr);
787 int (*offload)(struct nft_offload_ctx *ctx,
788 struct nft_flow_rule *flow,
789 const struct nft_expr *expr);
791 const struct nft_expr_type *type;
795 #define NFT_EXPR_MAXATTR 16
796 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
797 ALIGN(size, __alignof__(struct nft_expr)))
800 * struct nft_expr - nf_tables expression
802 * @ops: expression ops
803 * @data: expression private data
806 const struct nft_expr_ops *ops;
807 unsigned char data[];
810 static inline void *nft_expr_priv(const struct nft_expr *expr)
812 return (void *)expr->data;
815 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
816 const struct nlattr *nla);
817 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
818 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
819 const struct nft_expr *expr);
822 * struct nft_rule - nf_tables rule
824 * @list: used internally
825 * @handle: rule handle
826 * @genmask: generation mask
827 * @dlen: length of expression data
828 * @udata: user data is appended to the rule
829 * @data: expression data
832 struct list_head list;
838 __attribute__((aligned(__alignof__(struct nft_expr))));
841 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
843 return (struct nft_expr *)&rule->data[0];
846 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
848 return ((void *)expr) + expr->ops->size;
851 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
853 return (struct nft_expr *)&rule->data[rule->dlen];
856 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
858 return (void *)&rule->data[rule->dlen];
862 * The last pointer isn't really necessary, but the compiler isn't able to
863 * determine that the result of nft_expr_last() is always the same since it
864 * can't assume that the dlen value wasn't changed within calls in the loop.
866 #define nft_rule_for_each_expr(expr, last, rule) \
867 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
869 (expr) = nft_expr_next(expr))
871 enum nft_chain_flags {
872 NFT_BASE_CHAIN = 0x1,
873 NFT_CHAIN_HW_OFFLOAD = 0x2,
877 * struct nft_chain - nf_tables chain
879 * @rules: list of rules in the chain
880 * @list: used internally
881 * @rhlhead: used internally
882 * @table: table that this chain belongs to
883 * @handle: chain handle
884 * @use: number of jump references to this chain
885 * @flags: bitmask of enum nft_chain_flags
886 * @name: name of the chain
889 struct nft_rule *__rcu *rules_gen_0;
890 struct nft_rule *__rcu *rules_gen_1;
891 struct list_head rules;
892 struct list_head list;
893 struct rhlist_head rhlhead;
894 struct nft_table *table;
901 /* Only used during control plane commit phase: */
902 struct nft_rule **rules_next;
905 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
907 enum nft_chain_types {
908 NFT_CHAIN_T_DEFAULT = 0,
915 * struct nft_chain_type - nf_tables chain type info
917 * @name: name of the type
918 * @type: numeric identifier
919 * @family: address family
920 * @owner: module owner
921 * @hook_mask: mask of valid hooks
922 * @hooks: array of hook functions
923 * @ops_register: base chain register function
924 * @ops_unregister: base chain unregister function
926 struct nft_chain_type {
928 enum nft_chain_types type;
930 struct module *owner;
931 unsigned int hook_mask;
932 #if IS_ENABLED(CONFIG_NETFILTER)
933 nf_hookfn *hooks[NF_MAX_HOOKS];
934 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
935 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
939 int nft_chain_validate_dependency(const struct nft_chain *chain,
940 enum nft_chain_types type);
941 int nft_chain_validate_hooks(const struct nft_chain *chain,
942 unsigned int hook_flags);
947 struct u64_stats_sync syncp;
951 * struct nft_base_chain - nf_tables base chain
953 * @ops: netfilter hook ops
955 * @policy: default policy
956 * @stats: per-cpu chain stats
958 * @dev_name: device name that this base chain is attached to (if any)
959 * @flow_block: flow block (for hardware offload)
961 struct nft_base_chain {
962 #if IS_ENABLED(CONFIG_NETFILTER)
963 struct nf_hook_ops ops;
965 const struct nft_chain_type *type;
968 struct nft_stats __percpu *stats;
969 struct nft_chain chain;
970 char dev_name[IFNAMSIZ];
971 struct flow_block flow_block;
974 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
976 return container_of(chain, struct nft_base_chain, chain);
979 static inline bool nft_is_base_chain(const struct nft_chain *chain)
981 return chain->flags & NFT_BASE_CHAIN;
984 int __nft_release_basechain(struct nft_ctx *ctx);
986 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
989 * struct nft_table - nf_tables table
991 * @list: used internally
992 * @chains_ht: chains in the table
993 * @chains: same, for stable walks
994 * @sets: sets in the table
995 * @objects: stateful objects in the table
996 * @flowtables: flow tables in the table
997 * @hgenerator: handle generator state
998 * @handle: table handle
999 * @use: number of chain references to this table
1000 * @flags: table flag (see enum nft_table_flags)
1001 * @genmask: generation mask
1002 * @afinfo: address family info
1003 * @name: name of the table
1006 struct list_head list;
1007 struct rhltable chains_ht;
1008 struct list_head chains;
1009 struct list_head sets;
1010 struct list_head objects;
1011 struct list_head flowtables;
1021 void nft_register_chain_type(const struct nft_chain_type *);
1022 void nft_unregister_chain_type(const struct nft_chain_type *);
1024 int nft_register_expr(struct nft_expr_type *);
1025 void nft_unregister_expr(struct nft_expr_type *);
1027 int nft_verdict_dump(struct sk_buff *skb, int type,
1028 const struct nft_verdict *v);
1031 * struct nft_object_hash_key - key to lookup nft_object
1033 * @name: name of the stateful object to look up
1034 * @table: table the object belongs to
1036 struct nft_object_hash_key {
1038 const struct nft_table *table;
1042 * struct nft_object - nf_tables stateful object
1044 * @list: table stateful object list node
1045 * @key: keys that identify this object
1046 * @rhlhead: nft_objname_ht node
1047 * @genmask: generation mask
1048 * @use: number of references to this stateful object
1049 * @handle: unique object handle
1050 * @ops: object operations
1051 * @data: object data, layout depends on type
1054 struct list_head list;
1055 struct rhlist_head rhlhead;
1056 struct nft_object_hash_key key;
1060 /* runtime data below here */
1061 const struct nft_object_ops *ops ____cacheline_aligned;
1062 unsigned char data[]
1063 __attribute__((aligned(__alignof__(u64))));
1066 static inline void *nft_obj_data(const struct nft_object *obj)
1068 return (void *)obj->data;
1071 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1073 struct nft_object *nft_obj_lookup(const struct net *net,
1074 const struct nft_table *table,
1075 const struct nlattr *nla, u32 objtype,
1078 void nft_obj_notify(struct net *net, const struct nft_table *table,
1079 struct nft_object *obj, u32 portid, u32 seq,
1080 int event, int family, int report, gfp_t gfp);
1083 * struct nft_object_type - stateful object type
1085 * @select_ops: function to select nft_object_ops
1086 * @ops: default ops, used when no select_ops functions is present
1087 * @list: list node in list of object types
1088 * @type: stateful object numeric type
1089 * @owner: module owner
1090 * @maxattr: maximum netlink attribute
1091 * @policy: netlink attribute policy
1093 struct nft_object_type {
1094 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1095 const struct nlattr * const tb[]);
1096 const struct nft_object_ops *ops;
1097 struct list_head list;
1099 unsigned int maxattr;
1100 struct module *owner;
1101 const struct nla_policy *policy;
1105 * struct nft_object_ops - stateful object operations
1107 * @eval: stateful object evaluation function
1108 * @size: stateful object size
1109 * @init: initialize object from netlink attributes
1110 * @destroy: release existing stateful object
1111 * @dump: netlink dump stateful object
1113 struct nft_object_ops {
1114 void (*eval)(struct nft_object *obj,
1115 struct nft_regs *regs,
1116 const struct nft_pktinfo *pkt);
1118 int (*init)(const struct nft_ctx *ctx,
1119 const struct nlattr *const tb[],
1120 struct nft_object *obj);
1121 void (*destroy)(const struct nft_ctx *ctx,
1122 struct nft_object *obj);
1123 int (*dump)(struct sk_buff *skb,
1124 struct nft_object *obj,
1126 const struct nft_object_type *type;
1129 int nft_register_obj(struct nft_object_type *obj_type);
1130 void nft_unregister_obj(struct nft_object_type *obj_type);
1132 #define NFT_FLOWTABLE_DEVICE_MAX 8
1135 * struct nft_flowtable - nf_tables flow table
1137 * @list: flow table list node in table list
1138 * @table: the table the flow table is contained in
1139 * @name: name of this flow table
1140 * @hooknum: hook number
1141 * @priority: hook priority
1142 * @ops_len: number of hooks in array
1143 * @genmask: generation mask
1144 * @use: number of references to this flow table
1145 * @handle: unique object handle
1146 * @dev_name: array of device names
1147 * @data: rhashtable and garbage collector
1148 * @ops: array of hooks
1150 struct nft_flowtable {
1151 struct list_head list;
1152 struct nft_table *table;
1160 /* runtime data below here */
1161 #if IS_ENABLED(CONFIG_NETFILTER)
1162 struct nf_hook_ops *ops ____cacheline_aligned;
1164 struct nf_flowtable data;
1167 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1168 const struct nlattr *nla,
1171 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1172 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1175 * struct nft_traceinfo - nft tracing information and state
1177 * @pkt: pktinfo currently processed
1178 * @basechain: base chain currently processed
1179 * @chain: chain currently processed
1180 * @rule: rule that was evaluated
1181 * @verdict: verdict given by rule
1182 * @type: event type (enum nft_trace_types)
1183 * @packet_dumped: packet headers sent in a previous traceinfo message
1184 * @trace: other struct members are initialised
1186 struct nft_traceinfo {
1187 const struct nft_pktinfo *pkt;
1188 const struct nft_base_chain *basechain;
1189 const struct nft_chain *chain;
1190 const struct nft_rule *rule;
1191 const struct nft_verdict *verdict;
1192 enum nft_trace_types type;
1197 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1198 const struct nft_verdict *verdict,
1199 const struct nft_chain *basechain);
1201 void nft_trace_notify(struct nft_traceinfo *info);
1203 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1204 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1206 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1207 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1209 #define MODULE_ALIAS_NFT_EXPR(name) \
1210 MODULE_ALIAS("nft-expr-" name)
1212 #define MODULE_ALIAS_NFT_SET() \
1213 MODULE_ALIAS("nft-set")
1215 #define MODULE_ALIAS_NFT_OBJ(type) \
1216 MODULE_ALIAS("nft-obj-" __stringify(type))
1218 #if IS_ENABLED(CONFIG_NF_TABLES)
1221 * The gencursor defines two generations, the currently active and the
1222 * next one. Objects contain a bitmask of 2 bits specifying the generations
1223 * they're active in. A set bit means they're inactive in the generation
1224 * represented by that bit.
1226 * New objects start out as inactive in the current and active in the
1227 * next generation. When committing the ruleset the bitmask is cleared,
1228 * meaning they're active in all generations. When removing an object,
1229 * it is set inactive in the next generation. After committing the ruleset,
1230 * the objects are removed.
1232 static inline unsigned int nft_gencursor_next(const struct net *net)
1234 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1237 static inline u8 nft_genmask_next(const struct net *net)
1239 return 1 << nft_gencursor_next(net);
1242 static inline u8 nft_genmask_cur(const struct net *net)
1244 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1245 return 1 << READ_ONCE(net->nft.gencursor);
1248 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1251 * Generic transaction helpers
1254 /* Check if this object is currently active. */
1255 #define nft_is_active(__net, __obj) \
1256 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1258 /* Check if this object is active in the next generation. */
1259 #define nft_is_active_next(__net, __obj) \
1260 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1262 /* This object becomes active in the next generation. */
1263 #define nft_activate_next(__net, __obj) \
1264 (__obj)->genmask = nft_genmask_cur(__net)
1266 /* This object becomes inactive in the next generation. */
1267 #define nft_deactivate_next(__net, __obj) \
1268 (__obj)->genmask = nft_genmask_next(__net)
1270 /* After committing the ruleset, clear the stale generation bit. */
1271 #define nft_clear(__net, __obj) \
1272 (__obj)->genmask &= ~nft_genmask_next(__net)
1273 #define nft_active_genmask(__obj, __genmask) \
1274 !((__obj)->genmask & __genmask)
1277 * Set element transaction helpers
1280 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1283 return !(ext->genmask & genmask);
1286 static inline void nft_set_elem_change_active(const struct net *net,
1287 const struct nft_set *set,
1288 struct nft_set_ext *ext)
1290 ext->genmask ^= nft_genmask_next(net);
1293 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1296 * We use a free bit in the genmask field to indicate the element
1297 * is busy, meaning it is currently being processed either by
1298 * the netlink API or GC.
1300 * Even though the genmask is only a single byte wide, this works
1301 * because the extension structure if fully constant once initialized,
1302 * so there are no non-atomic write accesses unless it is already
1305 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1307 #if defined(__LITTLE_ENDIAN_BITFIELD)
1308 #define NFT_SET_ELEM_BUSY_BIT 2
1309 #elif defined(__BIG_ENDIAN_BITFIELD)
1310 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1315 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1317 unsigned long *word = (unsigned long *)ext;
1319 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1320 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1323 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1325 unsigned long *word = (unsigned long *)ext;
1327 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1331 * struct nft_trans - nf_tables object update in transaction
1333 * @list: used internally
1334 * @msg_type: message type
1335 * @put_net: ctx->net needs to be put
1336 * @ctx: transaction context
1337 * @data: internal information related to the transaction
1340 struct list_head list;
1347 struct nft_trans_rule {
1348 struct nft_rule *rule;
1349 struct nft_flow_rule *flow;
1353 #define nft_trans_rule(trans) \
1354 (((struct nft_trans_rule *)trans->data)->rule)
1355 #define nft_trans_flow_rule(trans) \
1356 (((struct nft_trans_rule *)trans->data)->flow)
1357 #define nft_trans_rule_id(trans) \
1358 (((struct nft_trans_rule *)trans->data)->rule_id)
1360 struct nft_trans_set {
1361 struct nft_set *set;
1365 #define nft_trans_set(trans) \
1366 (((struct nft_trans_set *)trans->data)->set)
1367 #define nft_trans_set_id(trans) \
1368 (((struct nft_trans_set *)trans->data)->set_id)
1370 struct nft_trans_chain {
1373 struct nft_stats __percpu *stats;
1377 #define nft_trans_chain_update(trans) \
1378 (((struct nft_trans_chain *)trans->data)->update)
1379 #define nft_trans_chain_name(trans) \
1380 (((struct nft_trans_chain *)trans->data)->name)
1381 #define nft_trans_chain_stats(trans) \
1382 (((struct nft_trans_chain *)trans->data)->stats)
1383 #define nft_trans_chain_policy(trans) \
1384 (((struct nft_trans_chain *)trans->data)->policy)
1386 struct nft_trans_table {
1391 #define nft_trans_table_update(trans) \
1392 (((struct nft_trans_table *)trans->data)->update)
1393 #define nft_trans_table_enable(trans) \
1394 (((struct nft_trans_table *)trans->data)->enable)
1396 struct nft_trans_elem {
1397 struct nft_set *set;
1398 struct nft_set_elem elem;
1401 #define nft_trans_elem_set(trans) \
1402 (((struct nft_trans_elem *)trans->data)->set)
1403 #define nft_trans_elem(trans) \
1404 (((struct nft_trans_elem *)trans->data)->elem)
1406 struct nft_trans_obj {
1407 struct nft_object *obj;
1410 #define nft_trans_obj(trans) \
1411 (((struct nft_trans_obj *)trans->data)->obj)
1413 struct nft_trans_flowtable {
1414 struct nft_flowtable *flowtable;
1417 #define nft_trans_flowtable(trans) \
1418 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1420 int __init nft_chain_filter_init(void);
1421 void nft_chain_filter_fini(void);
1423 void __init nft_chain_route_init(void);
1424 void nft_chain_route_fini(void);
1425 #endif /* _NET_NF_TABLES_H */