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 <net/netlink.h>
14 #define NFT_JUMP_STACK_SIZE 16
20 /* for x_tables compatibility */
21 struct xt_action_param xt;
24 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
26 return pkt->xt.state->net;
29 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
31 return pkt->xt.state->hook;
34 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
36 return pkt->xt.state->pf;
39 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
41 return pkt->xt.state->in;
44 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
46 return pkt->xt.state->out;
49 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
51 const struct nf_hook_state *state)
54 pkt->xt.state = state;
57 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
60 pkt->tprot_set = false;
66 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
68 const struct nf_hook_state *state)
70 nft_set_pktinfo(pkt, skb, state);
71 nft_set_pktinfo_proto_unspec(pkt, skb);
75 * struct nft_verdict - nf_tables verdict
77 * @code: nf_tables/netfilter verdict code
78 * @chain: destination chain for NFT_JUMP/NFT_GOTO
82 struct nft_chain *chain;
88 struct nft_verdict verdict;
90 } __attribute__((aligned(__alignof__(u64))));
93 * struct nft_regs - nf_tables register set
95 * @data: data registers
96 * @verdict: verdict register
98 * The first four data registers alias to the verdict register.
103 struct nft_verdict verdict;
107 /* Store/load an u16 or u8 integer to/from the u32 data register.
109 * Note, when using concatenations, register allocation happens at 32-bit
110 * level. So for store instruction, pad the rest part with zero to avoid
114 static inline void nft_reg_store16(u32 *dreg, u16 val)
120 static inline void nft_reg_store8(u32 *dreg, u8 val)
126 static inline u16 nft_reg_load16(u32 *sreg)
131 static inline u8 nft_reg_load8(u32 *sreg)
136 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
139 if (len % NFT_REG32_SIZE)
140 dst[len / NFT_REG32_SIZE] = 0;
141 memcpy(dst, src, len);
144 static inline void nft_data_debug(const struct nft_data *data)
146 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
147 data->data[0], data->data[1],
148 data->data[2], data->data[3]);
152 * struct nft_ctx - nf_tables rule/set context
154 * @net: net namespace
155 * @afi: address family info
156 * @table: the table the chain is contained in
157 * @chain: the chain the rule is contained in
158 * @nla: netlink attributes
159 * @portid: netlink portID of the original message
160 * @seq: netlink sequence number
161 * @report: notify via unicast netlink message
165 struct nft_af_info *afi;
166 struct nft_table *table;
167 struct nft_chain *chain;
168 const struct nlattr * const *nla;
174 struct nft_data_desc {
175 enum nft_data_types type;
179 int nft_data_init(const struct nft_ctx *ctx,
180 struct nft_data *data, unsigned int size,
181 struct nft_data_desc *desc, const struct nlattr *nla);
182 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
183 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
184 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
185 enum nft_data_types type, unsigned int len);
187 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
189 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
192 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
194 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
197 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
198 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
200 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
201 int nft_parse_register_store(const struct nft_ctx *ctx,
202 const struct nlattr *attr, u8 *dreg,
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)];
233 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
240 struct nft_set_iter {
245 int (*fn)(const struct nft_ctx *ctx,
247 const struct nft_set_iter *iter,
248 struct nft_set_elem *elem);
252 * struct nft_set_desc - description of set elements
256 * @size: number of set elements
258 struct nft_set_desc {
265 * enum nft_set_class - performance class
267 * @NFT_LOOKUP_O_1: constant, O(1)
268 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
269 * @NFT_LOOKUP_O_N: linear, O(N)
273 NFT_SET_CLASS_O_LOG_N,
278 * struct nft_set_estimate - estimation of memory and performance
281 * @size: required memory
282 * @lookup: lookup performance class
283 * @space: memory class
285 struct nft_set_estimate {
287 enum nft_set_class lookup;
288 enum nft_set_class space;
292 * struct nft_set_type - nf_tables set type
294 * @select_ops: function to select nft_set_ops
295 * @ops: default ops, used when no select_ops functions is present
296 * @list: used internally
297 * @owner: module reference
299 struct nft_set_type {
300 const struct nft_set_ops *(*select_ops)(const struct nft_ctx *,
301 const struct nft_set_desc *desc,
303 const struct nft_set_ops *ops;
304 struct list_head list;
305 struct module *owner;
312 * struct nft_set_ops - nf_tables set operations
314 * @lookup: look up an element within the set
315 * @insert: insert new element into set
316 * @activate: activate new element in the next generation
317 * @deactivate: lookup for element and deactivate it in the next generation
318 * @flush: deactivate element in the next generation
319 * @remove: remove element from set
320 * @walk: iterate over all set elemeennts
321 * @privsize: function to return size of set private data
322 * @init: initialize private data of new set instance
323 * @destroy: destroy private data of set instance
324 * @elemsize: element private size
325 * @features: features supported by the implementation
328 bool (*lookup)(const struct net *net,
329 const struct nft_set *set,
331 const struct nft_set_ext **ext);
332 bool (*update)(struct nft_set *set,
334 void *(*new)(struct nft_set *,
335 const struct nft_expr *,
337 const struct nft_expr *expr,
338 struct nft_regs *regs,
339 const struct nft_set_ext **ext);
341 int (*insert)(const struct net *net,
342 const struct nft_set *set,
343 const struct nft_set_elem *elem,
344 struct nft_set_ext **ext);
345 void (*activate)(const struct net *net,
346 const struct nft_set *set,
347 const struct nft_set_elem *elem);
348 void * (*deactivate)(const struct net *net,
349 const struct nft_set *set,
350 const struct nft_set_elem *elem);
351 bool (*flush)(const struct net *net,
352 const struct nft_set *set,
354 void (*remove)(const struct net *net,
355 const struct nft_set *set,
356 const struct nft_set_elem *elem);
357 void (*walk)(const struct nft_ctx *ctx,
359 struct nft_set_iter *iter);
361 unsigned int (*privsize)(const struct nlattr * const nla[],
362 const struct nft_set_desc *desc);
363 bool (*estimate)(const struct nft_set_desc *desc,
365 struct nft_set_estimate *est);
366 int (*init)(const struct nft_set *set,
367 const struct nft_set_desc *desc,
368 const struct nlattr * const nla[]);
369 void (*destroy)(const struct nft_set *set);
371 unsigned int elemsize;
373 const struct nft_set_type *type;
376 int nft_register_set(struct nft_set_type *type);
377 void nft_unregister_set(struct nft_set_type *type);
380 * struct nft_set - nf_tables set instance
382 * @list: table set list node
383 * @bindings: list of set bindings
384 * @table: table this set belongs to
385 * @name: name 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;
421 unsigned char *udata;
422 /* runtime data below here */
423 const struct nft_set_ops *ops ____cacheline_aligned;
429 __attribute__((aligned(__alignof__(u64))));
432 static inline void *nft_set_priv(const struct nft_set *set)
434 return (void *)set->data;
437 static inline struct nft_set *nft_set_container_of(const void *priv)
439 return (void *)priv - offsetof(struct nft_set, data);
442 struct nft_set *nft_set_lookup(const struct net *net,
443 const struct nft_table *table,
444 const struct nlattr *nla_set_name,
445 const struct nlattr *nla_set_id,
448 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
450 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
454 * struct nft_set_binding - nf_tables set binding
456 * @list: set bindings list node
457 * @chain: chain containing the rule bound to the set
458 * @flags: set action flags
460 * A set binding contains all information necessary for validation
461 * of new elements added to a bound set.
463 struct nft_set_binding {
464 struct list_head list;
465 const struct nft_chain *chain;
469 enum nft_trans_phase;
470 void nf_tables_activate_set(const struct nft_ctx *ctx, struct nft_set *set);
471 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
472 struct nft_set_binding *binding,
473 enum nft_trans_phase phase);
474 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
475 struct nft_set_binding *binding);
476 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
477 struct nft_set_binding *binding, bool commit);
478 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
481 * enum nft_set_extensions - set extension type IDs
483 * @NFT_SET_EXT_KEY: element key
484 * @NFT_SET_EXT_DATA: mapping data
485 * @NFT_SET_EXT_FLAGS: element flags
486 * @NFT_SET_EXT_TIMEOUT: element timeout
487 * @NFT_SET_EXT_EXPIRATION: element expiration time
488 * @NFT_SET_EXT_USERDATA: user data associated with the element
489 * @NFT_SET_EXT_EXPR: expression assiociated with the element
490 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
491 * @NFT_SET_EXT_NUM: number of extension types
493 enum nft_set_extensions {
498 NFT_SET_EXT_EXPIRATION,
499 NFT_SET_EXT_USERDATA,
506 * struct nft_set_ext_type - set extension type
508 * @len: fixed part length of the extension
509 * @align: alignment requirements of the extension
511 struct nft_set_ext_type {
516 extern const struct nft_set_ext_type nft_set_ext_types[];
519 * struct nft_set_ext_tmpl - set extension template
521 * @len: length of extension area
522 * @offset: offsets of individual extension types
524 struct nft_set_ext_tmpl {
526 u8 offset[NFT_SET_EXT_NUM];
530 * struct nft_set_ext - set extensions
532 * @genmask: generation mask
533 * @offset: offsets of individual extension types
534 * @data: beginning of extension data
538 u8 offset[NFT_SET_EXT_NUM];
542 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
544 memset(tmpl, 0, sizeof(*tmpl));
545 tmpl->len = sizeof(struct nft_set_ext);
548 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
551 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
552 BUG_ON(tmpl->len > U8_MAX);
553 tmpl->offset[id] = tmpl->len;
554 tmpl->len += nft_set_ext_types[id].len + len;
557 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
559 nft_set_ext_add_length(tmpl, id, 0);
562 static inline void nft_set_ext_init(struct nft_set_ext *ext,
563 const struct nft_set_ext_tmpl *tmpl)
565 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
568 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
570 return !!ext->offset[id];
573 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
575 return ext && __nft_set_ext_exists(ext, id);
578 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
580 return (void *)ext + ext->offset[id];
583 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
585 return nft_set_ext(ext, NFT_SET_EXT_KEY);
588 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
590 return nft_set_ext(ext, NFT_SET_EXT_DATA);
593 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
595 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
598 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
600 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
603 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
605 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
608 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
610 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
613 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
615 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
618 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
620 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
621 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
624 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
627 return elem + set->ops->elemsize;
630 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
632 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
635 void *nft_set_elem_init(const struct nft_set *set,
636 const struct nft_set_ext_tmpl *tmpl,
637 const u32 *key, const u32 *data,
638 u64 timeout, gfp_t gfp);
639 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
643 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
646 * @set: set the elements belong to
647 * @cnt: count of elements
649 struct nft_set_gc_batch_head {
651 const struct nft_set *set;
655 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
656 sizeof(struct nft_set_gc_batch_head)) / \
660 * struct nft_set_gc_batch - nf_tables set garbage collection batch
662 * @head: GC batch head
663 * @elems: garbage collection elements
665 struct nft_set_gc_batch {
666 struct nft_set_gc_batch_head head;
667 void *elems[NFT_SET_GC_BATCH_SIZE];
670 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
672 void nft_set_gc_batch_release(struct rcu_head *rcu);
674 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
677 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
680 static inline struct nft_set_gc_batch *
681 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
685 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
687 nft_set_gc_batch_complete(gcb);
689 return nft_set_gc_batch_alloc(set, gfp);
692 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
695 gcb->elems[gcb->head.cnt++] = elem;
699 * struct nft_expr_type - nf_tables expression type
701 * @select_ops: function to select nft_expr_ops
702 * @ops: default ops, used when no select_ops functions is present
703 * @list: used internally
705 * @owner: module reference
706 * @policy: netlink attribute policy
707 * @maxattr: highest netlink attribute number
708 * @family: address family for AF-specific types
709 * @flags: expression type flags
711 struct nft_expr_type {
712 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
713 const struct nlattr * const tb[]);
714 const struct nft_expr_ops *ops;
715 struct list_head list;
717 struct module *owner;
718 const struct nla_policy *policy;
719 unsigned int maxattr;
724 #define NFT_EXPR_STATEFUL 0x1
726 enum nft_trans_phase {
728 NFT_TRANS_PREPARE_ERROR,
735 * struct nft_expr_ops - nf_tables expression operations
737 * @eval: Expression evaluation function
738 * @size: full expression size, including private data size
739 * @init: initialization function
740 * @activate: activate expression in the next generation
741 * @deactivate: deactivate expression in next generation
742 * @destroy: destruction function, called after synchronize_rcu
743 * @dump: function to dump parameters
744 * @type: expression type
745 * @validate: validate expression, called during loop detection
746 * @data: extra data to attach to this expression operation
749 struct nft_expr_ops {
750 void (*eval)(const struct nft_expr *expr,
751 struct nft_regs *regs,
752 const struct nft_pktinfo *pkt);
753 int (*clone)(struct nft_expr *dst,
754 const struct nft_expr *src);
757 int (*init)(const struct nft_ctx *ctx,
758 const struct nft_expr *expr,
759 const struct nlattr * const tb[]);
760 void (*activate)(const struct nft_ctx *ctx,
761 const struct nft_expr *expr);
762 void (*deactivate)(const struct nft_ctx *ctx,
763 const struct nft_expr *expr,
764 enum nft_trans_phase phase);
765 void (*destroy)(const struct nft_ctx *ctx,
766 const struct nft_expr *expr);
767 int (*dump)(struct sk_buff *skb,
768 const struct nft_expr *expr);
769 int (*validate)(const struct nft_ctx *ctx,
770 const struct nft_expr *expr,
771 const struct nft_data **data);
772 const struct nft_expr_type *type;
776 #define NFT_EXPR_MAXATTR 16
777 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
778 ALIGN(size, __alignof__(struct nft_expr)))
781 * struct nft_expr - nf_tables expression
783 * @ops: expression ops
784 * @data: expression private data
787 const struct nft_expr_ops *ops;
789 __attribute__((aligned(__alignof__(u64))));
792 static inline void *nft_expr_priv(const struct nft_expr *expr)
794 return (void *)expr->data;
797 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
798 const struct nlattr *nla);
799 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
800 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
801 const struct nft_expr *expr);
803 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
807 if (src->ops->clone) {
809 err = src->ops->clone(dst, src);
813 memcpy(dst, src, src->ops->size);
816 __module_get(src->ops->type->owner);
821 * struct nft_rule - nf_tables rule
823 * @list: used internally
824 * @handle: rule handle
825 * @genmask: generation mask
826 * @dlen: length of expression data
827 * @udata: user data is appended to the rule
828 * @data: expression data
831 struct list_head list;
837 __attribute__((aligned(__alignof__(struct nft_expr))));
840 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
842 return (struct nft_expr *)&rule->data[0];
845 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
847 return ((void *)expr) + expr->ops->size;
850 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
852 return (struct nft_expr *)&rule->data[rule->dlen];
855 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
857 return (void *)&rule->data[rule->dlen];
861 * The last pointer isn't really necessary, but the compiler isn't able to
862 * determine that the result of nft_expr_last() is always the same since it
863 * can't assume that the dlen value wasn't changed within calls in the loop.
865 #define nft_rule_for_each_expr(expr, last, rule) \
866 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
868 (expr) = nft_expr_next(expr))
870 enum nft_chain_flags {
871 NFT_BASE_CHAIN = 0x1,
875 * struct nft_chain - nf_tables chain
877 * @rules: list of rules in the chain
878 * @list: used internally
879 * @table: table that this chain belongs to
880 * @handle: chain handle
881 * @use: number of jump references to this chain
882 * @level: length of longest path to this chain
883 * @flags: bitmask of enum nft_chain_flags
884 * @name: name of the chain
887 struct list_head rules;
888 struct list_head list;
889 struct nft_table *table;
898 enum nft_chain_type {
899 NFT_CHAIN_T_DEFAULT = 0,
906 * struct nf_chain_type - nf_tables chain type info
908 * @name: name of the type
909 * @type: numeric identifier
910 * @family: address family
911 * @owner: module owner
912 * @hook_mask: mask of valid hooks
913 * @hooks: hookfn overrides
915 struct nf_chain_type {
917 enum nft_chain_type type;
919 struct module *owner;
920 unsigned int hook_mask;
921 nf_hookfn *hooks[NF_MAX_HOOKS];
924 int nft_chain_validate_dependency(const struct nft_chain *chain,
925 enum nft_chain_type type);
926 int nft_chain_validate_hooks(const struct nft_chain *chain,
927 unsigned int hook_flags);
932 struct u64_stats_sync syncp;
935 #define NFT_HOOK_OPS_MAX 2
938 * struct nft_base_chain - nf_tables base chain
940 * @ops: netfilter hook ops
942 * @policy: default policy
943 * @stats: per-cpu chain stats
945 * @dev_name: device name that this base chain is attached to (if any)
947 struct nft_base_chain {
948 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
949 const struct nf_chain_type *type;
952 struct nft_stats __percpu *stats;
953 struct nft_chain chain;
954 char dev_name[IFNAMSIZ];
957 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
959 return container_of(chain, struct nft_base_chain, chain);
962 static inline bool nft_is_base_chain(const struct nft_chain *chain)
964 return chain->flags & NFT_BASE_CHAIN;
967 int __nft_release_basechain(struct nft_ctx *ctx);
969 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
971 static inline bool nft_use_inc(u32 *use)
973 if (*use == UINT_MAX)
981 static inline void nft_use_dec(u32 *use)
983 WARN_ON_ONCE((*use)-- == 0);
986 /* For error and abort path: restore use counter to previous state. */
987 static inline void nft_use_inc_restore(u32 *use)
989 WARN_ON_ONCE(!nft_use_inc(use));
992 #define nft_use_dec_restore nft_use_dec
995 * struct nft_table - nf_tables table
997 * @list: used internally
998 * @chains: chains in the table
999 * @sets: sets in the table
1000 * @objects: stateful objects in the table
1001 * @hgenerator: handle generator state
1002 * @use: number of chain references to this table
1003 * @flags: table flag (see enum nft_table_flags)
1004 * @genmask: generation mask
1005 * @name: name of the table
1008 struct list_head list;
1009 struct list_head chains;
1010 struct list_head sets;
1011 struct list_head objects;
1020 NFT_AF_NEEDS_DEV = (1 << 0),
1024 * struct nft_af_info - nf_tables address family info
1026 * @list: used internally
1027 * @family: address family
1028 * @nhooks: number of hooks in this family
1029 * @owner: module owner
1030 * @tables: used internally
1031 * @flags: family flags
1032 * @nops: number of hook ops in this family
1033 * @hook_ops_init: initialization function for chain hook ops
1034 * @hooks: hookfn overrides for packet validation
1036 struct nft_af_info {
1037 struct list_head list;
1039 unsigned int nhooks;
1040 struct module *owner;
1041 struct list_head tables;
1044 void (*hook_ops_init)(struct nf_hook_ops *,
1046 nf_hookfn *hooks[NF_MAX_HOOKS];
1049 int nft_register_afinfo(struct net *, struct nft_af_info *);
1050 void nft_unregister_afinfo(struct net *, struct nft_af_info *);
1052 int nft_register_chain_type(const struct nf_chain_type *);
1053 void nft_unregister_chain_type(const struct nf_chain_type *);
1055 int nft_register_expr(struct nft_expr_type *);
1056 void nft_unregister_expr(struct nft_expr_type *);
1058 int nft_verdict_dump(struct sk_buff *skb, int type,
1059 const struct nft_verdict *v);
1062 * struct nft_object - nf_tables stateful object
1064 * @list: table stateful object list node
1065 * @table: table this object belongs to
1066 * @name: name of this stateful object
1067 * @genmask: generation mask
1068 * @use: number of references to this stateful object
1069 * @data: object data, layout depends on type
1070 * @ops: object operations
1071 * @data: pointer to object data
1074 struct list_head list;
1076 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 *nf_tables_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)(struct nft_object *obj);
1140 int (*dump)(struct sk_buff *skb,
1141 struct nft_object *obj,
1143 const struct nft_object_type *type;
1146 int nft_register_obj(struct nft_object_type *obj_type);
1147 void nft_unregister_obj(struct nft_object_type *obj_type);
1150 * struct nft_traceinfo - nft tracing information and state
1152 * @pkt: pktinfo currently processed
1153 * @basechain: base chain currently processed
1154 * @chain: chain currently processed
1155 * @rule: rule that was evaluated
1156 * @verdict: verdict given by rule
1157 * @type: event type (enum nft_trace_types)
1158 * @packet_dumped: packet headers sent in a previous traceinfo message
1159 * @trace: other struct members are initialised
1161 struct nft_traceinfo {
1162 const struct nft_pktinfo *pkt;
1163 const struct nft_base_chain *basechain;
1164 const struct nft_chain *chain;
1165 const struct nft_rule *rule;
1166 const struct nft_verdict *verdict;
1167 enum nft_trace_types type;
1172 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1173 const struct nft_verdict *verdict,
1174 const struct nft_chain *basechain);
1176 void nft_trace_notify(struct nft_traceinfo *info);
1178 #define nft_dereference(p) \
1179 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
1181 #define MODULE_ALIAS_NFT_FAMILY(family) \
1182 MODULE_ALIAS("nft-afinfo-" __stringify(family))
1184 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1185 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1187 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1188 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1190 #define MODULE_ALIAS_NFT_EXPR(name) \
1191 MODULE_ALIAS("nft-expr-" name)
1193 #define MODULE_ALIAS_NFT_SET() \
1194 MODULE_ALIAS("nft-set")
1196 #define MODULE_ALIAS_NFT_OBJ(type) \
1197 MODULE_ALIAS("nft-obj-" __stringify(type))
1200 * The gencursor defines two generations, the currently active and the
1201 * next one. Objects contain a bitmask of 2 bits specifying the generations
1202 * they're active in. A set bit means they're inactive in the generation
1203 * represented by that bit.
1205 * New objects start out as inactive in the current and active in the
1206 * next generation. When committing the ruleset the bitmask is cleared,
1207 * meaning they're active in all generations. When removing an object,
1208 * it is set inactive in the next generation. After committing the ruleset,
1209 * the objects are removed.
1211 static inline unsigned int nft_gencursor_next(const struct net *net)
1213 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1216 static inline u8 nft_genmask_next(const struct net *net)
1218 return 1 << nft_gencursor_next(net);
1221 static inline u8 nft_genmask_cur(const struct net *net)
1223 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1224 return 1 << ACCESS_ONCE(net->nft.gencursor);
1227 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1230 * Generic transaction helpers
1233 /* Check if this object is currently active. */
1234 #define nft_is_active(__net, __obj) \
1235 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1237 /* Check if this object is active in the next generation. */
1238 #define nft_is_active_next(__net, __obj) \
1239 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1241 /* This object becomes active in the next generation. */
1242 #define nft_activate_next(__net, __obj) \
1243 (__obj)->genmask = nft_genmask_cur(__net)
1245 /* This object becomes inactive in the next generation. */
1246 #define nft_deactivate_next(__net, __obj) \
1247 (__obj)->genmask = nft_genmask_next(__net)
1249 /* After committing the ruleset, clear the stale generation bit. */
1250 #define nft_clear(__net, __obj) \
1251 (__obj)->genmask &= ~nft_genmask_next(__net)
1252 #define nft_active_genmask(__obj, __genmask) \
1253 !((__obj)->genmask & __genmask)
1256 * Set element transaction helpers
1259 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1262 return !(ext->genmask & genmask);
1265 static inline void nft_set_elem_change_active(const struct net *net,
1266 const struct nft_set *set,
1267 struct nft_set_ext *ext)
1269 ext->genmask ^= nft_genmask_next(net);
1273 * We use a free bit in the genmask field to indicate the element
1274 * is busy, meaning it is currently being processed either by
1275 * the netlink API or GC.
1277 * Even though the genmask is only a single byte wide, this works
1278 * because the extension structure if fully constant once initialized,
1279 * so there are no non-atomic write accesses unless it is already
1282 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1284 #if defined(__LITTLE_ENDIAN_BITFIELD)
1285 #define NFT_SET_ELEM_BUSY_BIT 2
1286 #elif defined(__BIG_ENDIAN_BITFIELD)
1287 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1292 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1294 unsigned long *word = (unsigned long *)ext;
1296 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1297 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1300 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1302 unsigned long *word = (unsigned long *)ext;
1304 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1308 * struct nft_trans - nf_tables object update in transaction
1310 * @list: used internally
1311 * @msg_type: message type
1312 * @ctx: transaction context
1313 * @data: internal information related to the transaction
1316 struct list_head list;
1322 struct nft_trans_rule {
1323 struct nft_rule *rule;
1327 #define nft_trans_rule(trans) \
1328 (((struct nft_trans_rule *)trans->data)->rule)
1329 #define nft_trans_rule_id(trans) \
1330 (((struct nft_trans_rule *)trans->data)->rule_id)
1332 struct nft_trans_set {
1333 struct nft_set *set;
1338 #define nft_trans_set(trans) \
1339 (((struct nft_trans_set *)trans->data)->set)
1340 #define nft_trans_set_id(trans) \
1341 (((struct nft_trans_set *)trans->data)->set_id)
1342 #define nft_trans_set_bound(trans) \
1343 (((struct nft_trans_set *)trans->data)->bound)
1345 struct nft_trans_chain {
1348 struct nft_stats __percpu *stats;
1352 #define nft_trans_chain_update(trans) \
1353 (((struct nft_trans_chain *)trans->data)->update)
1354 #define nft_trans_chain_name(trans) \
1355 (((struct nft_trans_chain *)trans->data)->name)
1356 #define nft_trans_chain_stats(trans) \
1357 (((struct nft_trans_chain *)trans->data)->stats)
1358 #define nft_trans_chain_policy(trans) \
1359 (((struct nft_trans_chain *)trans->data)->policy)
1361 struct nft_trans_table {
1366 #define nft_trans_table_update(trans) \
1367 (((struct nft_trans_table *)trans->data)->update)
1368 #define nft_trans_table_enable(trans) \
1369 (((struct nft_trans_table *)trans->data)->enable)
1371 struct nft_trans_elem {
1372 struct nft_set *set;
1373 struct nft_set_elem elem;
1377 #define nft_trans_elem_set(trans) \
1378 (((struct nft_trans_elem *)trans->data)->set)
1379 #define nft_trans_elem(trans) \
1380 (((struct nft_trans_elem *)trans->data)->elem)
1381 #define nft_trans_elem_set_bound(trans) \
1382 (((struct nft_trans_elem *)trans->data)->bound)
1384 struct nft_trans_obj {
1385 struct nft_object *obj;
1388 #define nft_trans_obj(trans) \
1389 (((struct nft_trans_obj *)trans->data)->obj)
1391 #endif /* _NET_NF_TABLES_H */