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 unsigned int nft_parse_register(const struct nlattr *attr);
195 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
197 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
198 int nft_validate_register_store(const struct nft_ctx *ctx,
199 enum nft_registers reg,
200 const struct nft_data *data,
201 enum nft_data_types type, unsigned int len);
204 * struct nft_userdata - user defined data associated with an object
206 * @len: length of the data
209 * The presence of user data is indicated in an object specific fashion,
210 * so a length of zero can't occur and the value "len" indicates data
213 struct nft_userdata {
215 unsigned char data[0];
219 * struct nft_set_elem - generic representation of set elements
222 * @priv: element private data and extensions
224 struct nft_set_elem {
226 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
233 struct nft_set_iter {
238 int (*fn)(const struct nft_ctx *ctx,
240 const struct nft_set_iter *iter,
241 struct nft_set_elem *elem);
245 * struct nft_set_desc - description of set elements
249 * @size: number of set elements
251 struct nft_set_desc {
258 * enum nft_set_class - performance class
260 * @NFT_LOOKUP_O_1: constant, O(1)
261 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
262 * @NFT_LOOKUP_O_N: linear, O(N)
266 NFT_SET_CLASS_O_LOG_N,
271 * struct nft_set_estimate - estimation of memory and performance
274 * @size: required memory
275 * @lookup: lookup performance class
276 * @space: memory class
278 struct nft_set_estimate {
280 enum nft_set_class lookup;
281 enum nft_set_class space;
288 * struct nft_set_ops - nf_tables set operations
290 * @lookup: look up an element within the set
291 * @insert: insert new element into set
292 * @activate: activate new element in the next generation
293 * @deactivate: lookup for element and deactivate it in the next generation
294 * @flush: deactivate element in the next generation
295 * @remove: remove element from set
296 * @walk: iterate over all set elemeennts
297 * @get: get set elements
298 * @privsize: function to return size of set private data
299 * @init: initialize private data of new set instance
300 * @destroy: destroy private data of set instance
301 * @elemsize: element private size
304 bool (*lookup)(const struct net *net,
305 const struct nft_set *set,
307 const struct nft_set_ext **ext);
308 bool (*update)(struct nft_set *set,
310 void *(*new)(struct nft_set *,
311 const struct nft_expr *,
313 const struct nft_expr *expr,
314 struct nft_regs *regs,
315 const struct nft_set_ext **ext);
317 int (*insert)(const struct net *net,
318 const struct nft_set *set,
319 const struct nft_set_elem *elem,
320 struct nft_set_ext **ext);
321 void (*activate)(const struct net *net,
322 const struct nft_set *set,
323 const struct nft_set_elem *elem);
324 void * (*deactivate)(const struct net *net,
325 const struct nft_set *set,
326 const struct nft_set_elem *elem);
327 bool (*flush)(const struct net *net,
328 const struct nft_set *set,
330 void (*remove)(const struct net *net,
331 const struct nft_set *set,
332 const struct nft_set_elem *elem);
333 void (*walk)(const struct nft_ctx *ctx,
335 struct nft_set_iter *iter);
336 void * (*get)(const struct net *net,
337 const struct nft_set *set,
338 const struct nft_set_elem *elem,
341 u64 (*privsize)(const struct nlattr * const nla[],
342 const struct nft_set_desc *desc);
343 bool (*estimate)(const struct nft_set_desc *desc,
345 struct nft_set_estimate *est);
346 int (*init)(const struct nft_set *set,
347 const struct nft_set_desc *desc,
348 const struct nlattr * const nla[]);
349 void (*destroy)(const struct nft_set *set);
350 void (*gc_init)(const struct nft_set *set);
352 unsigned int elemsize;
356 * struct nft_set_type - nf_tables set type
358 * @ops: set ops for this type
359 * @list: used internally
360 * @owner: module reference
361 * @features: features supported by the implementation
363 struct nft_set_type {
364 const struct nft_set_ops ops;
365 struct list_head list;
366 struct module *owner;
369 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
371 int nft_register_set(struct nft_set_type *type);
372 void nft_unregister_set(struct nft_set_type *type);
375 * struct nft_set - nf_tables set instance
377 * @list: table set list node
378 * @bindings: list of set bindings
379 * @table: table this set belongs to
380 * @net: netnamespace this set belongs to
381 * @name: name of the set
382 * @handle: unique handle of the set
383 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
384 * @dtype: data type (verdict or numeric type defined by userspace)
385 * @objtype: object type (see NFT_OBJECT_* definitions)
386 * @size: maximum set size
387 * @use: number of rules references to this set
388 * @nelems: number of elements
389 * @ndeact: number of deactivated elements queued for removal
390 * @timeout: default timeout value in jiffies
391 * @gc_int: garbage collection interval in msecs
392 * @policy: set parameterization (see enum nft_set_policies)
393 * @udlen: user data length
397 * @genmask: generation mask
400 * @data: private set data
403 struct list_head list;
404 struct list_head bindings;
405 struct nft_table *table;
420 unsigned char *udata;
421 /* runtime data below here */
422 const struct nft_set_ops *ops ____cacheline_aligned;
428 __attribute__((aligned(__alignof__(u64))));
431 static inline bool nft_set_is_anonymous(const struct nft_set *set)
433 return set->flags & NFT_SET_ANONYMOUS;
436 static inline void *nft_set_priv(const struct nft_set *set)
438 return (void *)set->data;
441 static inline struct nft_set *nft_set_container_of(const void *priv)
443 return (void *)priv - offsetof(struct nft_set, data);
446 struct nft_set *nft_set_lookup_global(const struct net *net,
447 const struct nft_table *table,
448 const struct nlattr *nla_set_name,
449 const struct nlattr *nla_set_id,
452 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
454 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
458 * struct nft_set_binding - nf_tables set binding
460 * @list: set bindings list node
461 * @chain: chain containing the rule bound to the set
462 * @flags: set action flags
464 * A set binding contains all information necessary for validation
465 * of new elements added to a bound set.
467 struct nft_set_binding {
468 struct list_head list;
469 const struct nft_chain *chain;
473 enum nft_trans_phase;
474 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
475 struct nft_set_binding *binding,
476 enum nft_trans_phase phase);
477 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
478 struct nft_set_binding *binding);
479 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
480 struct nft_set_binding *binding, bool commit);
481 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
484 * enum nft_set_extensions - set extension type IDs
486 * @NFT_SET_EXT_KEY: element key
487 * @NFT_SET_EXT_DATA: mapping data
488 * @NFT_SET_EXT_FLAGS: element flags
489 * @NFT_SET_EXT_TIMEOUT: element timeout
490 * @NFT_SET_EXT_EXPIRATION: element expiration time
491 * @NFT_SET_EXT_USERDATA: user data associated with the element
492 * @NFT_SET_EXT_EXPR: expression assiociated with the element
493 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
494 * @NFT_SET_EXT_NUM: number of extension types
496 enum nft_set_extensions {
501 NFT_SET_EXT_EXPIRATION,
502 NFT_SET_EXT_USERDATA,
509 * struct nft_set_ext_type - set extension type
511 * @len: fixed part length of the extension
512 * @align: alignment requirements of the extension
514 struct nft_set_ext_type {
519 extern const struct nft_set_ext_type nft_set_ext_types[];
522 * struct nft_set_ext_tmpl - set extension template
524 * @len: length of extension area
525 * @offset: offsets of individual extension types
527 struct nft_set_ext_tmpl {
529 u8 offset[NFT_SET_EXT_NUM];
533 * struct nft_set_ext - set extensions
535 * @genmask: generation mask
536 * @offset: offsets of individual extension types
537 * @data: beginning of extension data
541 u8 offset[NFT_SET_EXT_NUM];
545 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
547 memset(tmpl, 0, sizeof(*tmpl));
548 tmpl->len = sizeof(struct nft_set_ext);
551 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
554 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
555 BUG_ON(tmpl->len > U8_MAX);
556 tmpl->offset[id] = tmpl->len;
557 tmpl->len += nft_set_ext_types[id].len + len;
560 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
562 nft_set_ext_add_length(tmpl, id, 0);
565 static inline void nft_set_ext_init(struct nft_set_ext *ext,
566 const struct nft_set_ext_tmpl *tmpl)
568 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
571 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
573 return !!ext->offset[id];
576 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
578 return ext && __nft_set_ext_exists(ext, id);
581 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
583 return (void *)ext + ext->offset[id];
586 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
588 return nft_set_ext(ext, NFT_SET_EXT_KEY);
591 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
593 return nft_set_ext(ext, NFT_SET_EXT_DATA);
596 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
598 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
601 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
603 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
606 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
608 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
611 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
613 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
616 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
618 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
621 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
623 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
624 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
627 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
630 return elem + set->ops->elemsize;
633 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
635 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
638 void *nft_set_elem_init(const struct nft_set *set,
639 const struct nft_set_ext_tmpl *tmpl,
640 const u32 *key, const u32 *data,
641 u64 timeout, gfp_t gfp);
642 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
646 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
649 * @set: set the elements belong to
650 * @cnt: count of elements
652 struct nft_set_gc_batch_head {
654 const struct nft_set *set;
658 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
659 sizeof(struct nft_set_gc_batch_head)) / \
663 * struct nft_set_gc_batch - nf_tables set garbage collection batch
665 * @head: GC batch head
666 * @elems: garbage collection elements
668 struct nft_set_gc_batch {
669 struct nft_set_gc_batch_head head;
670 void *elems[NFT_SET_GC_BATCH_SIZE];
673 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
675 void nft_set_gc_batch_release(struct rcu_head *rcu);
677 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
680 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
683 static inline struct nft_set_gc_batch *
684 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
688 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
690 nft_set_gc_batch_complete(gcb);
692 return nft_set_gc_batch_alloc(set, gfp);
695 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
698 gcb->elems[gcb->head.cnt++] = elem;
703 * struct nft_expr_type - nf_tables expression type
705 * @select_ops: function to select nft_expr_ops
706 * @release_ops: release nft_expr_ops
707 * @ops: default ops, used when no select_ops functions is present
708 * @list: used internally
710 * @owner: module reference
711 * @policy: netlink attribute policy
712 * @maxattr: highest netlink attribute number
713 * @family: address family for AF-specific types
714 * @flags: expression type flags
716 struct nft_expr_type {
717 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
718 const struct nlattr * const tb[]);
719 void (*release_ops)(const struct nft_expr_ops *ops);
720 const struct nft_expr_ops *ops;
721 struct list_head list;
723 struct module *owner;
724 const struct nla_policy *policy;
725 unsigned int maxattr;
730 #define NFT_EXPR_STATEFUL 0x1
731 #define NFT_EXPR_GC 0x2
733 enum nft_trans_phase {
741 * struct nft_expr_ops - nf_tables expression operations
743 * @eval: Expression evaluation function
744 * @size: full expression size, including private data size
745 * @init: initialization function
746 * @activate: activate expression in the next generation
747 * @deactivate: deactivate expression in next generation
748 * @destroy: destruction function, called after synchronize_rcu
749 * @dump: function to dump parameters
750 * @type: expression type
751 * @validate: validate expression, called during loop detection
752 * @data: extra data to attach to this expression operation
755 struct nft_expr_ops {
756 void (*eval)(const struct nft_expr *expr,
757 struct nft_regs *regs,
758 const struct nft_pktinfo *pkt);
759 int (*clone)(struct nft_expr *dst,
760 const struct nft_expr *src);
763 int (*init)(const struct nft_ctx *ctx,
764 const struct nft_expr *expr,
765 const struct nlattr * const tb[]);
766 void (*activate)(const struct nft_ctx *ctx,
767 const struct nft_expr *expr);
768 void (*deactivate)(const struct nft_ctx *ctx,
769 const struct nft_expr *expr,
770 enum nft_trans_phase phase);
771 void (*destroy)(const struct nft_ctx *ctx,
772 const struct nft_expr *expr);
773 void (*destroy_clone)(const struct nft_ctx *ctx,
774 const struct nft_expr *expr);
775 int (*dump)(struct sk_buff *skb,
776 const struct nft_expr *expr);
777 int (*validate)(const struct nft_ctx *ctx,
778 const struct nft_expr *expr,
779 const struct nft_data **data);
780 bool (*gc)(struct net *net,
781 const struct nft_expr *expr);
782 const struct nft_expr_type *type;
786 #define NFT_EXPR_MAXATTR 16
787 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
788 ALIGN(size, __alignof__(struct nft_expr)))
791 * struct nft_expr - nf_tables expression
793 * @ops: expression ops
794 * @data: expression private data
797 const struct nft_expr_ops *ops;
799 __attribute__((aligned(__alignof__(u64))));
802 static inline void *nft_expr_priv(const struct nft_expr *expr)
804 return (void *)expr->data;
807 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
808 const struct nlattr *nla);
809 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
810 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
811 const struct nft_expr *expr);
813 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
817 if (src->ops->clone) {
819 err = src->ops->clone(dst, src);
823 memcpy(dst, src, src->ops->size);
826 __module_get(src->ops->type->owner);
831 * struct nft_rule - nf_tables rule
833 * @list: used internally
834 * @handle: rule handle
835 * @genmask: generation mask
836 * @dlen: length of expression data
837 * @udata: user data is appended to the rule
838 * @data: expression data
841 struct list_head list;
847 __attribute__((aligned(__alignof__(struct nft_expr))));
850 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
852 return (struct nft_expr *)&rule->data[0];
855 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
857 return ((void *)expr) + expr->ops->size;
860 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
862 return (struct nft_expr *)&rule->data[rule->dlen];
865 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
867 return (void *)&rule->data[rule->dlen];
871 * The last pointer isn't really necessary, but the compiler isn't able to
872 * determine that the result of nft_expr_last() is always the same since it
873 * can't assume that the dlen value wasn't changed within calls in the loop.
875 #define nft_rule_for_each_expr(expr, last, rule) \
876 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
878 (expr) = nft_expr_next(expr))
880 enum nft_chain_flags {
881 NFT_BASE_CHAIN = 0x1,
885 * struct nft_chain - nf_tables chain
887 * @rules: list of rules in the chain
888 * @list: used internally
889 * @rhlhead: used internally
890 * @table: table that this chain belongs to
891 * @handle: chain handle
892 * @use: number of jump references to this chain
893 * @flags: bitmask of enum nft_chain_flags
894 * @name: name of the chain
897 struct nft_rule *__rcu *rules_gen_0;
898 struct nft_rule *__rcu *rules_gen_1;
899 struct list_head rules;
900 struct list_head list;
901 struct rhlist_head rhlhead;
902 struct nft_table *table;
909 /* Only used during control plane commit phase: */
910 struct nft_rule **rules_next;
913 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
915 enum nft_chain_types {
916 NFT_CHAIN_T_DEFAULT = 0,
923 * struct nft_chain_type - nf_tables chain type info
925 * @name: name of the type
926 * @type: numeric identifier
927 * @family: address family
928 * @owner: module owner
929 * @hook_mask: mask of valid hooks
930 * @hooks: array of hook functions
931 * @ops_register: base chain register function
932 * @ops_unregister: base chain unregister function
934 struct nft_chain_type {
936 enum nft_chain_types type;
938 struct module *owner;
939 unsigned int hook_mask;
940 nf_hookfn *hooks[NF_MAX_HOOKS];
941 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
942 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
945 int nft_chain_validate_dependency(const struct nft_chain *chain,
946 enum nft_chain_types type);
947 int nft_chain_validate_hooks(const struct nft_chain *chain,
948 unsigned int hook_flags);
953 struct u64_stats_sync syncp;
957 * struct nft_base_chain - nf_tables base chain
959 * @ops: netfilter hook ops
961 * @policy: default policy
962 * @stats: per-cpu chain stats
964 * @dev_name: device name that this base chain is attached to (if any)
966 struct nft_base_chain {
967 struct nf_hook_ops ops;
968 const struct nft_chain_type *type;
971 struct nft_stats __percpu *stats;
972 struct nft_chain chain;
973 char dev_name[IFNAMSIZ];
976 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
978 return container_of(chain, struct nft_base_chain, chain);
981 static inline bool nft_is_base_chain(const struct nft_chain *chain)
983 return chain->flags & NFT_BASE_CHAIN;
986 int __nft_release_basechain(struct nft_ctx *ctx);
988 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
991 * struct nft_table - nf_tables table
993 * @list: used internally
994 * @chains_ht: chains in the table
995 * @chains: same, for stable walks
996 * @sets: sets in the table
997 * @objects: stateful objects in the table
998 * @flowtables: flow tables in the table
999 * @hgenerator: handle generator state
1000 * @handle: table handle
1001 * @use: number of chain references to this table
1002 * @flags: table flag (see enum nft_table_flags)
1003 * @genmask: generation mask
1004 * @afinfo: address family info
1005 * @name: name of the table
1008 struct list_head list;
1009 struct rhltable chains_ht;
1010 struct list_head chains;
1011 struct list_head sets;
1012 struct list_head objects;
1013 struct list_head flowtables;
1023 void nft_register_chain_type(const struct nft_chain_type *);
1024 void nft_unregister_chain_type(const struct nft_chain_type *);
1026 int nft_register_expr(struct nft_expr_type *);
1027 void nft_unregister_expr(struct nft_expr_type *);
1029 int nft_verdict_dump(struct sk_buff *skb, int type,
1030 const struct nft_verdict *v);
1033 * struct nft_object - nf_tables stateful object
1035 * @list: table stateful object list node
1036 * @table: table this object belongs to
1037 * @name: name of this stateful object
1038 * @genmask: generation mask
1039 * @use: number of references to this stateful object
1040 * @handle: unique object handle
1041 * @ops: object operations
1042 * @data: object data, layout depends on type
1045 struct list_head list;
1047 struct nft_table *table;
1051 /* runtime data below here */
1052 const struct nft_object_ops *ops ____cacheline_aligned;
1053 unsigned char data[]
1054 __attribute__((aligned(__alignof__(u64))));
1057 static inline void *nft_obj_data(const struct nft_object *obj)
1059 return (void *)obj->data;
1062 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1064 struct nft_object *nft_obj_lookup(const struct nft_table *table,
1065 const struct nlattr *nla, u32 objtype,
1068 void nft_obj_notify(struct net *net, struct nft_table *table,
1069 struct nft_object *obj, u32 portid, u32 seq,
1070 int event, int family, int report, gfp_t gfp);
1073 * struct nft_object_type - stateful object type
1075 * @select_ops: function to select nft_object_ops
1076 * @ops: default ops, used when no select_ops functions is present
1077 * @list: list node in list of object types
1078 * @type: stateful object numeric type
1079 * @owner: module owner
1080 * @maxattr: maximum netlink attribute
1081 * @policy: netlink attribute policy
1083 struct nft_object_type {
1084 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1085 const struct nlattr * const tb[]);
1086 const struct nft_object_ops *ops;
1087 struct list_head list;
1089 unsigned int maxattr;
1090 struct module *owner;
1091 const struct nla_policy *policy;
1095 * struct nft_object_ops - stateful object operations
1097 * @eval: stateful object evaluation function
1098 * @size: stateful object size
1099 * @init: initialize object from netlink attributes
1100 * @destroy: release existing stateful object
1101 * @dump: netlink dump stateful object
1103 struct nft_object_ops {
1104 void (*eval)(struct nft_object *obj,
1105 struct nft_regs *regs,
1106 const struct nft_pktinfo *pkt);
1108 int (*init)(const struct nft_ctx *ctx,
1109 const struct nlattr *const tb[],
1110 struct nft_object *obj);
1111 void (*destroy)(const struct nft_ctx *ctx,
1112 struct nft_object *obj);
1113 int (*dump)(struct sk_buff *skb,
1114 struct nft_object *obj,
1116 const struct nft_object_type *type;
1119 int nft_register_obj(struct nft_object_type *obj_type);
1120 void nft_unregister_obj(struct nft_object_type *obj_type);
1122 #define NFT_FLOWTABLE_DEVICE_MAX 8
1125 * struct nft_flowtable - nf_tables flow table
1127 * @list: flow table list node in table list
1128 * @table: the table the flow table is contained in
1129 * @name: name of this flow table
1130 * @hooknum: hook number
1131 * @priority: hook priority
1132 * @ops_len: number of hooks in array
1133 * @genmask: generation mask
1134 * @use: number of references to this flow table
1135 * @handle: unique object handle
1136 * @dev_name: array of device names
1137 * @data: rhashtable and garbage collector
1138 * @ops: array of hooks
1140 struct nft_flowtable {
1141 struct list_head list;
1142 struct nft_table *table;
1150 /* runtime data below here */
1151 struct nf_hook_ops *ops ____cacheline_aligned;
1152 struct nf_flowtable data;
1155 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1156 const struct nlattr *nla,
1159 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1160 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1163 * struct nft_traceinfo - nft tracing information and state
1165 * @pkt: pktinfo currently processed
1166 * @basechain: base chain currently processed
1167 * @chain: chain currently processed
1168 * @rule: rule that was evaluated
1169 * @verdict: verdict given by rule
1170 * @type: event type (enum nft_trace_types)
1171 * @packet_dumped: packet headers sent in a previous traceinfo message
1172 * @trace: other struct members are initialised
1174 struct nft_traceinfo {
1175 const struct nft_pktinfo *pkt;
1176 const struct nft_base_chain *basechain;
1177 const struct nft_chain *chain;
1178 const struct nft_rule *rule;
1179 const struct nft_verdict *verdict;
1180 enum nft_trace_types type;
1185 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1186 const struct nft_verdict *verdict,
1187 const struct nft_chain *basechain);
1189 void nft_trace_notify(struct nft_traceinfo *info);
1191 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1192 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1194 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1195 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1197 #define MODULE_ALIAS_NFT_EXPR(name) \
1198 MODULE_ALIAS("nft-expr-" name)
1200 #define MODULE_ALIAS_NFT_SET() \
1201 MODULE_ALIAS("nft-set")
1203 #define MODULE_ALIAS_NFT_OBJ(type) \
1204 MODULE_ALIAS("nft-obj-" __stringify(type))
1207 * The gencursor defines two generations, the currently active and the
1208 * next one. Objects contain a bitmask of 2 bits specifying the generations
1209 * they're active in. A set bit means they're inactive in the generation
1210 * represented by that bit.
1212 * New objects start out as inactive in the current and active in the
1213 * next generation. When committing the ruleset the bitmask is cleared,
1214 * meaning they're active in all generations. When removing an object,
1215 * it is set inactive in the next generation. After committing the ruleset,
1216 * the objects are removed.
1218 static inline unsigned int nft_gencursor_next(const struct net *net)
1220 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1223 static inline u8 nft_genmask_next(const struct net *net)
1225 return 1 << nft_gencursor_next(net);
1228 static inline u8 nft_genmask_cur(const struct net *net)
1230 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1231 return 1 << READ_ONCE(net->nft.gencursor);
1234 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1237 * Generic transaction helpers
1240 /* Check if this object is currently active. */
1241 #define nft_is_active(__net, __obj) \
1242 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1244 /* Check if this object is active in the next generation. */
1245 #define nft_is_active_next(__net, __obj) \
1246 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1248 /* This object becomes active in the next generation. */
1249 #define nft_activate_next(__net, __obj) \
1250 (__obj)->genmask = nft_genmask_cur(__net)
1252 /* This object becomes inactive in the next generation. */
1253 #define nft_deactivate_next(__net, __obj) \
1254 (__obj)->genmask = nft_genmask_next(__net)
1256 /* After committing the ruleset, clear the stale generation bit. */
1257 #define nft_clear(__net, __obj) \
1258 (__obj)->genmask &= ~nft_genmask_next(__net)
1259 #define nft_active_genmask(__obj, __genmask) \
1260 !((__obj)->genmask & __genmask)
1263 * Set element transaction helpers
1266 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1269 return !(ext->genmask & genmask);
1272 static inline void nft_set_elem_change_active(const struct net *net,
1273 const struct nft_set *set,
1274 struct nft_set_ext *ext)
1276 ext->genmask ^= nft_genmask_next(net);
1280 * We use a free bit in the genmask field to indicate the element
1281 * is busy, meaning it is currently being processed either by
1282 * the netlink API or GC.
1284 * Even though the genmask is only a single byte wide, this works
1285 * because the extension structure if fully constant once initialized,
1286 * so there are no non-atomic write accesses unless it is already
1289 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1291 #if defined(__LITTLE_ENDIAN_BITFIELD)
1292 #define NFT_SET_ELEM_BUSY_BIT 2
1293 #elif defined(__BIG_ENDIAN_BITFIELD)
1294 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1299 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1301 unsigned long *word = (unsigned long *)ext;
1303 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1304 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1307 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1309 unsigned long *word = (unsigned long *)ext;
1311 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1315 * struct nft_trans - nf_tables object update in transaction
1317 * @list: used internally
1318 * @msg_type: message type
1319 * @ctx: transaction context
1320 * @data: internal information related to the transaction
1323 struct list_head list;
1329 struct nft_trans_rule {
1330 struct nft_rule *rule;
1334 #define nft_trans_rule(trans) \
1335 (((struct nft_trans_rule *)trans->data)->rule)
1336 #define nft_trans_rule_id(trans) \
1337 (((struct nft_trans_rule *)trans->data)->rule_id)
1339 struct nft_trans_set {
1340 struct nft_set *set;
1345 #define nft_trans_set(trans) \
1346 (((struct nft_trans_set *)trans->data)->set)
1347 #define nft_trans_set_id(trans) \
1348 (((struct nft_trans_set *)trans->data)->set_id)
1349 #define nft_trans_set_bound(trans) \
1350 (((struct nft_trans_set *)trans->data)->bound)
1352 struct nft_trans_chain {
1355 struct nft_stats __percpu *stats;
1359 #define nft_trans_chain_update(trans) \
1360 (((struct nft_trans_chain *)trans->data)->update)
1361 #define nft_trans_chain_name(trans) \
1362 (((struct nft_trans_chain *)trans->data)->name)
1363 #define nft_trans_chain_stats(trans) \
1364 (((struct nft_trans_chain *)trans->data)->stats)
1365 #define nft_trans_chain_policy(trans) \
1366 (((struct nft_trans_chain *)trans->data)->policy)
1368 struct nft_trans_table {
1373 #define nft_trans_table_update(trans) \
1374 (((struct nft_trans_table *)trans->data)->update)
1375 #define nft_trans_table_enable(trans) \
1376 (((struct nft_trans_table *)trans->data)->enable)
1378 struct nft_trans_elem {
1379 struct nft_set *set;
1380 struct nft_set_elem elem;
1384 #define nft_trans_elem_set(trans) \
1385 (((struct nft_trans_elem *)trans->data)->set)
1386 #define nft_trans_elem(trans) \
1387 (((struct nft_trans_elem *)trans->data)->elem)
1388 #define nft_trans_elem_set_bound(trans) \
1389 (((struct nft_trans_elem *)trans->data)->bound)
1391 struct nft_trans_obj {
1392 struct nft_object *obj;
1395 #define nft_trans_obj(trans) \
1396 (((struct nft_trans_obj *)trans->data)->obj)
1398 struct nft_trans_flowtable {
1399 struct nft_flowtable *flowtable;
1402 #define nft_trans_flowtable(trans) \
1403 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1405 int __init nft_chain_filter_init(void);
1406 void nft_chain_filter_fini(void);
1408 #endif /* _NET_NF_TABLES_H */