1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <linux/module.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>
16 #define NFT_JUMP_STACK_SIZE 16
22 /* for x_tables compatibility */
23 struct xt_action_param xt;
26 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
28 return pkt->xt.state->net;
31 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
33 return pkt->xt.state->hook;
36 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
38 return pkt->xt.state->pf;
41 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
43 return pkt->xt.state->in;
46 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
48 return pkt->xt.state->out;
51 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
53 const struct nf_hook_state *state)
56 pkt->xt.state = state;
59 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
62 pkt->tprot_set = false;
69 * struct nft_verdict - nf_tables verdict
71 * @code: nf_tables/netfilter verdict code
72 * @chain: destination chain for NFT_JUMP/NFT_GOTO
76 struct nft_chain *chain;
82 struct nft_verdict verdict;
84 } __attribute__((aligned(__alignof__(u64))));
87 * struct nft_regs - nf_tables register set
89 * @data: data registers
90 * @verdict: verdict register
92 * The first four data registers alias to the verdict register.
97 struct nft_verdict verdict;
101 /* Store/load an u16 or u8 integer to/from the u32 data register.
103 * Note, when using concatenations, register allocation happens at 32-bit
104 * level. So for store instruction, pad the rest part with zero to avoid
108 static inline void nft_reg_store16(u32 *dreg, u16 val)
114 static inline void nft_reg_store8(u32 *dreg, u8 val)
120 static inline u16 nft_reg_load16(u32 *sreg)
125 static inline u8 nft_reg_load8(u32 *sreg)
130 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
133 if (len % NFT_REG32_SIZE)
134 dst[len / NFT_REG32_SIZE] = 0;
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;
170 struct nft_data_desc {
171 enum nft_data_types type;
175 int nft_data_init(const struct nft_ctx *ctx,
176 struct nft_data *data, unsigned int size,
177 struct nft_data_desc *desc, const struct nlattr *nla);
178 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
179 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
180 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
181 enum nft_data_types type, unsigned int len);
183 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
185 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
188 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
190 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
193 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
194 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
196 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
197 int nft_parse_register_store(const struct nft_ctx *ctx,
198 const struct nlattr *attr, u8 *dreg,
199 const struct nft_data *data,
200 enum nft_data_types type, unsigned int len);
203 * struct nft_userdata - user defined data associated with an object
205 * @len: length of the data
208 * The presence of user data is indicated in an object specific fashion,
209 * so a length of zero can't occur and the value "len" indicates data
212 struct nft_userdata {
214 unsigned char data[0];
218 * struct nft_set_elem - generic representation of set elements
221 * @priv: element private data and extensions
223 struct nft_set_elem {
225 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
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;
431 __attribute__((aligned(__alignof__(u64))));
434 static inline bool nft_set_is_anonymous(const struct nft_set *set)
436 return set->flags & NFT_SET_ANONYMOUS;
439 static inline void *nft_set_priv(const struct nft_set *set)
441 return (void *)set->data;
444 static inline struct nft_set *nft_set_container_of(const void *priv)
446 return (void *)priv - offsetof(struct nft_set, data);
449 struct nft_set *nft_set_lookup_global(const struct net *net,
450 const struct nft_table *table,
451 const struct nlattr *nla_set_name,
452 const struct nlattr *nla_set_id,
455 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
457 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
461 * struct nft_set_binding - nf_tables set binding
463 * @list: set bindings list node
464 * @chain: chain containing the rule bound to the set
465 * @flags: set action flags
467 * A set binding contains all information necessary for validation
468 * of new elements added to a bound set.
470 struct nft_set_binding {
471 struct list_head list;
472 const struct nft_chain *chain;
476 enum nft_trans_phase;
477 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
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_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
484 struct nft_set_binding *binding, bool commit);
485 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
488 * enum nft_set_extensions - set extension type IDs
490 * @NFT_SET_EXT_KEY: element key
491 * @NFT_SET_EXT_DATA: mapping data
492 * @NFT_SET_EXT_FLAGS: element flags
493 * @NFT_SET_EXT_TIMEOUT: element timeout
494 * @NFT_SET_EXT_EXPIRATION: element expiration time
495 * @NFT_SET_EXT_USERDATA: user data associated with the element
496 * @NFT_SET_EXT_EXPR: expression assiociated with the element
497 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
498 * @NFT_SET_EXT_NUM: number of extension types
500 enum nft_set_extensions {
505 NFT_SET_EXT_EXPIRATION,
506 NFT_SET_EXT_USERDATA,
513 * struct nft_set_ext_type - set extension type
515 * @len: fixed part length of the extension
516 * @align: alignment requirements of the extension
518 struct nft_set_ext_type {
523 extern const struct nft_set_ext_type nft_set_ext_types[];
526 * struct nft_set_ext_tmpl - set extension template
528 * @len: length of extension area
529 * @offset: offsets of individual extension types
531 struct nft_set_ext_tmpl {
533 u8 offset[NFT_SET_EXT_NUM];
537 * struct nft_set_ext - set extensions
539 * @genmask: generation mask
540 * @offset: offsets of individual extension types
541 * @data: beginning of extension data
545 u8 offset[NFT_SET_EXT_NUM];
549 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
551 memset(tmpl, 0, sizeof(*tmpl));
552 tmpl->len = sizeof(struct nft_set_ext);
555 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
558 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
559 BUG_ON(tmpl->len > U8_MAX);
560 tmpl->offset[id] = tmpl->len;
561 tmpl->len += nft_set_ext_types[id].len + len;
564 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
566 nft_set_ext_add_length(tmpl, id, 0);
569 static inline void nft_set_ext_init(struct nft_set_ext *ext,
570 const struct nft_set_ext_tmpl *tmpl)
572 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
575 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
577 return !!ext->offset[id];
580 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
582 return ext && __nft_set_ext_exists(ext, id);
585 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
587 return (void *)ext + ext->offset[id];
590 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
592 return nft_set_ext(ext, NFT_SET_EXT_KEY);
595 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
597 return nft_set_ext(ext, NFT_SET_EXT_DATA);
600 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
602 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
605 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
607 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
610 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
612 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
615 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
617 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
620 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
622 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
625 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
627 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
628 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
631 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
634 return elem + set->ops->elemsize;
637 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
639 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
642 void *nft_set_elem_init(const struct nft_set *set,
643 const struct nft_set_ext_tmpl *tmpl,
644 const u32 *key, const u32 *data,
645 u64 timeout, gfp_t gfp);
646 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
650 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
653 * @set: set the elements belong to
654 * @cnt: count of elements
656 struct nft_set_gc_batch_head {
658 const struct nft_set *set;
662 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
663 sizeof(struct nft_set_gc_batch_head)) / \
667 * struct nft_set_gc_batch - nf_tables set garbage collection batch
669 * @head: GC batch head
670 * @elems: garbage collection elements
672 struct nft_set_gc_batch {
673 struct nft_set_gc_batch_head head;
674 void *elems[NFT_SET_GC_BATCH_SIZE];
677 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
679 void nft_set_gc_batch_release(struct rcu_head *rcu);
681 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
684 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
687 static inline struct nft_set_gc_batch *
688 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
692 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
694 nft_set_gc_batch_complete(gcb);
696 return nft_set_gc_batch_alloc(set, gfp);
699 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
702 gcb->elems[gcb->head.cnt++] = elem;
707 * struct nft_expr_type - nf_tables expression type
709 * @select_ops: function to select nft_expr_ops
710 * @release_ops: release nft_expr_ops
711 * @ops: default ops, used when no select_ops functions is present
712 * @list: used internally
714 * @owner: module reference
715 * @policy: netlink attribute policy
716 * @maxattr: highest netlink attribute number
717 * @family: address family for AF-specific types
718 * @flags: expression type flags
720 struct nft_expr_type {
721 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
722 const struct nlattr * const tb[]);
723 void (*release_ops)(const struct nft_expr_ops *ops);
724 const struct nft_expr_ops *ops;
725 struct list_head list;
727 struct module *owner;
728 const struct nla_policy *policy;
729 unsigned int maxattr;
734 #define NFT_EXPR_STATEFUL 0x1
735 #define NFT_EXPR_GC 0x2
737 enum nft_trans_phase {
739 NFT_TRANS_PREPARE_ERROR,
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 const struct nft_expr_type *type;
791 #define NFT_EXPR_MAXATTR 16
792 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
793 ALIGN(size, __alignof__(struct nft_expr)))
796 * struct nft_expr - nf_tables expression
798 * @ops: expression ops
799 * @data: expression private data
802 const struct nft_expr_ops *ops;
804 __attribute__((aligned(__alignof__(u64))));
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);
818 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
822 if (src->ops->clone) {
824 err = src->ops->clone(dst, src);
828 memcpy(dst, src, src->ops->size);
831 __module_get(src->ops->type->owner);
836 * struct nft_rule - nf_tables rule
838 * @list: used internally
839 * @handle: rule handle
840 * @genmask: generation mask
841 * @dlen: length of expression data
842 * @udata: user data is appended to the rule
843 * @data: expression data
846 struct list_head list;
852 __attribute__((aligned(__alignof__(struct nft_expr))));
855 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
857 return (struct nft_expr *)&rule->data[0];
860 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
862 return ((void *)expr) + expr->ops->size;
865 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
867 return (struct nft_expr *)&rule->data[rule->dlen];
870 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
872 return (void *)&rule->data[rule->dlen];
876 * The last pointer isn't really necessary, but the compiler isn't able to
877 * determine that the result of nft_expr_last() is always the same since it
878 * can't assume that the dlen value wasn't changed within calls in the loop.
880 #define nft_rule_for_each_expr(expr, last, rule) \
881 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
883 (expr) = nft_expr_next(expr))
885 enum nft_chain_flags {
886 NFT_BASE_CHAIN = 0x1,
890 * struct nft_chain - nf_tables chain
892 * @rules: list of rules in the chain
893 * @list: used internally
894 * @rhlhead: used internally
895 * @table: table that this chain belongs to
896 * @handle: chain handle
897 * @use: number of jump references to this chain
898 * @flags: bitmask of enum nft_chain_flags
899 * @name: name of the chain
902 struct nft_rule *__rcu *rules_gen_0;
903 struct nft_rule *__rcu *rules_gen_1;
904 struct list_head rules;
905 struct list_head list;
906 struct rhlist_head rhlhead;
907 struct nft_table *table;
914 /* Only used during control plane commit phase: */
915 struct nft_rule **rules_next;
918 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
920 enum nft_chain_types {
921 NFT_CHAIN_T_DEFAULT = 0,
928 * struct nft_chain_type - nf_tables chain type info
930 * @name: name of the type
931 * @type: numeric identifier
932 * @family: address family
933 * @owner: module owner
934 * @hook_mask: mask of valid hooks
935 * @hooks: array of hook functions
936 * @ops_register: base chain register function
937 * @ops_unregister: base chain unregister function
939 struct nft_chain_type {
941 enum nft_chain_types type;
943 struct module *owner;
944 unsigned int hook_mask;
945 nf_hookfn *hooks[NF_MAX_HOOKS];
946 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
947 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
950 int nft_chain_validate_dependency(const struct nft_chain *chain,
951 enum nft_chain_types type);
952 int nft_chain_validate_hooks(const struct nft_chain *chain,
953 unsigned int hook_flags);
958 struct u64_stats_sync syncp;
962 * struct nft_base_chain - nf_tables base chain
964 * @ops: netfilter hook ops
966 * @policy: default policy
967 * @stats: per-cpu chain stats
969 * @dev_name: device name that this base chain is attached to (if any)
971 struct nft_base_chain {
972 struct nf_hook_ops ops;
973 const struct nft_chain_type *type;
976 struct nft_stats __percpu *stats;
977 struct nft_chain chain;
978 char dev_name[IFNAMSIZ];
981 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
983 return container_of(chain, struct nft_base_chain, chain);
986 static inline bool nft_is_base_chain(const struct nft_chain *chain)
988 return chain->flags & NFT_BASE_CHAIN;
991 int __nft_release_basechain(struct nft_ctx *ctx);
993 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
995 static inline bool nft_use_inc(u32 *use)
997 if (*use == UINT_MAX)
1005 static inline void nft_use_dec(u32 *use)
1007 WARN_ON_ONCE((*use)-- == 0);
1010 /* For error and abort path: restore use counter to previous state. */
1011 static inline void nft_use_inc_restore(u32 *use)
1013 WARN_ON_ONCE(!nft_use_inc(use));
1016 #define nft_use_dec_restore nft_use_dec
1019 * struct nft_table - nf_tables table
1021 * @list: used internally
1022 * @chains_ht: chains in the table
1023 * @chains: same, for stable walks
1024 * @sets: sets in the table
1025 * @objects: stateful objects in the table
1026 * @flowtables: flow tables in the table
1027 * @hgenerator: handle generator state
1028 * @handle: table handle
1029 * @use: number of chain references to this table
1030 * @flags: table flag (see enum nft_table_flags)
1031 * @genmask: generation mask
1032 * @afinfo: address family info
1033 * @name: name of the table
1036 struct list_head list;
1037 struct rhltable chains_ht;
1038 struct list_head chains;
1039 struct list_head sets;
1040 struct list_head objects;
1041 struct list_head flowtables;
1051 void nft_register_chain_type(const struct nft_chain_type *);
1052 void nft_unregister_chain_type(const struct nft_chain_type *);
1054 int nft_register_expr(struct nft_expr_type *);
1055 void nft_unregister_expr(struct nft_expr_type *);
1057 int nft_verdict_dump(struct sk_buff *skb, int type,
1058 const struct nft_verdict *v);
1061 * struct nft_object - nf_tables stateful object
1063 * @list: table stateful object list node
1064 * @table: table this object belongs to
1065 * @name: name of this stateful object
1066 * @genmask: generation mask
1067 * @use: number of references to this stateful object
1068 * @handle: unique object handle
1069 * @ops: object operations
1070 * @data: object data, layout depends on type
1073 struct list_head list;
1075 struct nft_table *table;
1079 /* runtime data below here */
1080 const struct nft_object_ops *ops ____cacheline_aligned;
1081 unsigned char data[]
1082 __attribute__((aligned(__alignof__(u64))));
1085 static inline void *nft_obj_data(const struct nft_object *obj)
1087 return (void *)obj->data;
1090 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1092 struct nft_object *nft_obj_lookup(const struct nft_table *table,
1093 const struct nlattr *nla, u32 objtype,
1096 void nft_obj_notify(struct net *net, struct nft_table *table,
1097 struct nft_object *obj, u32 portid, u32 seq,
1098 int event, int family, int report, gfp_t gfp);
1101 * struct nft_object_type - stateful object type
1103 * @select_ops: function to select nft_object_ops
1104 * @ops: default ops, used when no select_ops functions is present
1105 * @list: list node in list of object types
1106 * @type: stateful object numeric type
1107 * @owner: module owner
1108 * @maxattr: maximum netlink attribute
1109 * @policy: netlink attribute policy
1111 struct nft_object_type {
1112 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1113 const struct nlattr * const tb[]);
1114 const struct nft_object_ops *ops;
1115 struct list_head list;
1117 unsigned int maxattr;
1118 struct module *owner;
1119 const struct nla_policy *policy;
1123 * struct nft_object_ops - stateful object operations
1125 * @eval: stateful object evaluation function
1126 * @size: stateful object size
1127 * @init: initialize object from netlink attributes
1128 * @destroy: release existing stateful object
1129 * @dump: netlink dump stateful object
1131 struct nft_object_ops {
1132 void (*eval)(struct nft_object *obj,
1133 struct nft_regs *regs,
1134 const struct nft_pktinfo *pkt);
1136 int (*init)(const struct nft_ctx *ctx,
1137 const struct nlattr *const tb[],
1138 struct nft_object *obj);
1139 void (*destroy)(const struct nft_ctx *ctx,
1140 struct nft_object *obj);
1141 int (*dump)(struct sk_buff *skb,
1142 struct nft_object *obj,
1144 const struct nft_object_type *type;
1147 int nft_register_obj(struct nft_object_type *obj_type);
1148 void nft_unregister_obj(struct nft_object_type *obj_type);
1150 #define NFT_FLOWTABLE_DEVICE_MAX 8
1153 * struct nft_flowtable - nf_tables flow table
1155 * @list: flow table list node in table list
1156 * @table: the table the flow table is contained in
1157 * @name: name of this flow table
1158 * @hooknum: hook number
1159 * @priority: hook priority
1160 * @ops_len: number of hooks in array
1161 * @genmask: generation mask
1162 * @use: number of references to this flow table
1163 * @handle: unique object handle
1164 * @dev_name: array of device names
1165 * @data: rhashtable and garbage collector
1166 * @ops: array of hooks
1168 struct nft_flowtable {
1169 struct list_head list;
1170 struct nft_table *table;
1178 /* runtime data below here */
1179 struct nf_hook_ops *ops ____cacheline_aligned;
1180 struct nf_flowtable data;
1183 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1184 const struct nlattr *nla,
1187 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1188 struct nft_flowtable *flowtable,
1189 enum nft_trans_phase phase);
1191 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1192 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1195 * struct nft_traceinfo - nft tracing information and state
1197 * @pkt: pktinfo currently processed
1198 * @basechain: base chain currently processed
1199 * @chain: chain currently processed
1200 * @rule: rule that was evaluated
1201 * @verdict: verdict given by rule
1202 * @type: event type (enum nft_trace_types)
1203 * @packet_dumped: packet headers sent in a previous traceinfo message
1204 * @trace: other struct members are initialised
1206 struct nft_traceinfo {
1207 const struct nft_pktinfo *pkt;
1208 const struct nft_base_chain *basechain;
1209 const struct nft_chain *chain;
1210 const struct nft_rule *rule;
1211 const struct nft_verdict *verdict;
1212 enum nft_trace_types type;
1217 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1218 const struct nft_verdict *verdict,
1219 const struct nft_chain *basechain);
1221 void nft_trace_notify(struct nft_traceinfo *info);
1223 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1224 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1226 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1227 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1229 #define MODULE_ALIAS_NFT_EXPR(name) \
1230 MODULE_ALIAS("nft-expr-" name)
1232 #define MODULE_ALIAS_NFT_SET() \
1233 MODULE_ALIAS("nft-set")
1235 #define MODULE_ALIAS_NFT_OBJ(type) \
1236 MODULE_ALIAS("nft-obj-" __stringify(type))
1239 * The gencursor defines two generations, the currently active and the
1240 * next one. Objects contain a bitmask of 2 bits specifying the generations
1241 * they're active in. A set bit means they're inactive in the generation
1242 * represented by that bit.
1244 * New objects start out as inactive in the current and active in the
1245 * next generation. When committing the ruleset the bitmask is cleared,
1246 * meaning they're active in all generations. When removing an object,
1247 * it is set inactive in the next generation. After committing the ruleset,
1248 * the objects are removed.
1250 static inline unsigned int nft_gencursor_next(const struct net *net)
1252 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1255 static inline u8 nft_genmask_next(const struct net *net)
1257 return 1 << nft_gencursor_next(net);
1260 static inline u8 nft_genmask_cur(const struct net *net)
1262 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1263 return 1 << READ_ONCE(net->nft.gencursor);
1266 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1269 * Generic transaction helpers
1272 /* Check if this object is currently active. */
1273 #define nft_is_active(__net, __obj) \
1274 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1276 /* Check if this object is active in the next generation. */
1277 #define nft_is_active_next(__net, __obj) \
1278 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1280 /* This object becomes active in the next generation. */
1281 #define nft_activate_next(__net, __obj) \
1282 (__obj)->genmask = nft_genmask_cur(__net)
1284 /* This object becomes inactive in the next generation. */
1285 #define nft_deactivate_next(__net, __obj) \
1286 (__obj)->genmask = nft_genmask_next(__net)
1288 /* After committing the ruleset, clear the stale generation bit. */
1289 #define nft_clear(__net, __obj) \
1290 (__obj)->genmask &= ~nft_genmask_next(__net)
1291 #define nft_active_genmask(__obj, __genmask) \
1292 !((__obj)->genmask & __genmask)
1295 * Set element transaction helpers
1298 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1301 return !(ext->genmask & genmask);
1304 static inline void nft_set_elem_change_active(const struct net *net,
1305 const struct nft_set *set,
1306 struct nft_set_ext *ext)
1308 ext->genmask ^= nft_genmask_next(net);
1312 * We use a free bit in the genmask field to indicate the element
1313 * is busy, meaning it is currently being processed either by
1314 * the netlink API or GC.
1316 * Even though the genmask is only a single byte wide, this works
1317 * because the extension structure if fully constant once initialized,
1318 * so there are no non-atomic write accesses unless it is already
1321 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1323 #if defined(__LITTLE_ENDIAN_BITFIELD)
1324 #define NFT_SET_ELEM_BUSY_BIT 2
1325 #elif defined(__BIG_ENDIAN_BITFIELD)
1326 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1331 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1333 unsigned long *word = (unsigned long *)ext;
1335 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1336 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1339 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1341 unsigned long *word = (unsigned long *)ext;
1343 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1347 * struct nft_trans - nf_tables object update in transaction
1349 * @list: used internally
1350 * @binding_list: list of objects with possible bindings
1351 * @msg_type: message type
1352 * @ctx: transaction context
1353 * @data: internal information related to the transaction
1356 struct list_head list;
1357 struct list_head binding_list;
1363 struct nft_trans_rule {
1364 struct nft_rule *rule;
1368 #define nft_trans_rule(trans) \
1369 (((struct nft_trans_rule *)trans->data)->rule)
1370 #define nft_trans_rule_id(trans) \
1371 (((struct nft_trans_rule *)trans->data)->rule_id)
1373 struct nft_trans_set {
1374 struct nft_set *set;
1379 #define nft_trans_set(trans) \
1380 (((struct nft_trans_set *)trans->data)->set)
1381 #define nft_trans_set_id(trans) \
1382 (((struct nft_trans_set *)trans->data)->set_id)
1383 #define nft_trans_set_bound(trans) \
1384 (((struct nft_trans_set *)trans->data)->bound)
1386 struct nft_trans_chain {
1389 struct nft_stats __percpu *stats;
1393 #define nft_trans_chain_update(trans) \
1394 (((struct nft_trans_chain *)trans->data)->update)
1395 #define nft_trans_chain_name(trans) \
1396 (((struct nft_trans_chain *)trans->data)->name)
1397 #define nft_trans_chain_stats(trans) \
1398 (((struct nft_trans_chain *)trans->data)->stats)
1399 #define nft_trans_chain_policy(trans) \
1400 (((struct nft_trans_chain *)trans->data)->policy)
1402 struct nft_trans_table {
1407 #define nft_trans_table_update(trans) \
1408 (((struct nft_trans_table *)trans->data)->update)
1409 #define nft_trans_table_enable(trans) \
1410 (((struct nft_trans_table *)trans->data)->enable)
1412 struct nft_trans_elem {
1413 struct nft_set *set;
1414 struct nft_set_elem elem;
1418 #define nft_trans_elem_set(trans) \
1419 (((struct nft_trans_elem *)trans->data)->set)
1420 #define nft_trans_elem(trans) \
1421 (((struct nft_trans_elem *)trans->data)->elem)
1422 #define nft_trans_elem_set_bound(trans) \
1423 (((struct nft_trans_elem *)trans->data)->bound)
1425 struct nft_trans_obj {
1426 struct nft_object *obj;
1429 #define nft_trans_obj(trans) \
1430 (((struct nft_trans_obj *)trans->data)->obj)
1432 struct nft_trans_flowtable {
1433 struct nft_flowtable *flowtable;
1436 #define nft_trans_flowtable(trans) \
1437 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1439 int __init nft_chain_filter_init(void);
1440 void nft_chain_filter_fini(void);
1442 struct nftables_pernet {
1443 struct list_head tables;
1444 struct list_head commit_list;
1445 struct list_head binding_list;
1446 struct list_head module_list;
1447 struct list_head notify_list;
1448 struct mutex commit_mutex;
1449 unsigned int base_seq;
1453 #endif /* _NET_NF_TABLES_H */