1 #ifndef _NET_NF_TABLES_H
2 #define _NET_NF_TABLES_H
4 #include <linux/module.h>
5 #include <linux/list.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter/nfnetlink.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/nf_tables.h>
10 #include <linux/u64_stats_sync.h>
11 #include <net/netlink.h>
13 #define NFT_JUMP_STACK_SIZE 16
18 const struct net_device *in;
19 const struct net_device *out;
24 /* for x_tables compatibility */
25 struct xt_action_param xt;
28 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
30 const struct nf_hook_state *state)
33 pkt->net = pkt->xt.net = state->net;
34 pkt->in = pkt->xt.in = state->in;
35 pkt->out = pkt->xt.out = state->out;
36 pkt->hook = pkt->xt.hooknum = state->hook;
37 pkt->pf = pkt->xt.family = state->pf;
40 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
43 pkt->tprot_set = false;
49 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
51 const struct nf_hook_state *state)
53 nft_set_pktinfo(pkt, skb, state);
54 nft_set_pktinfo_proto_unspec(pkt, skb);
58 * struct nft_verdict - nf_tables verdict
60 * @code: nf_tables/netfilter verdict code
61 * @chain: destination chain for NFT_JUMP/NFT_GOTO
65 struct nft_chain *chain;
71 struct nft_verdict verdict;
73 } __attribute__((aligned(__alignof__(u64))));
76 * struct nft_regs - nf_tables register set
78 * @data: data registers
79 * @verdict: verdict register
81 * The first four data registers alias to the verdict register.
86 struct nft_verdict verdict;
90 /* Store/load an u16 or u8 integer to/from the u32 data register.
92 * Note, when using concatenations, register allocation happens at 32-bit
93 * level. So for store instruction, pad the rest part with zero to avoid
97 static inline void nft_reg_store16(u32 *dreg, u16 val)
103 static inline void nft_reg_store8(u32 *dreg, u8 val)
109 static inline u16 nft_reg_load16(u32 *sreg)
114 static inline u8 nft_reg_load8(u32 *sreg)
119 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
122 if (len % NFT_REG32_SIZE)
123 dst[len / NFT_REG32_SIZE] = 0;
124 memcpy(dst, src, len);
127 static inline void nft_data_debug(const struct nft_data *data)
129 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
130 data->data[0], data->data[1],
131 data->data[2], data->data[3]);
135 * struct nft_ctx - nf_tables rule/set context
137 * @net: net namespace
138 * @afi: address family info
139 * @table: the table the chain is contained in
140 * @chain: the chain the rule is contained in
141 * @nla: netlink attributes
142 * @portid: netlink portID of the original message
143 * @seq: netlink sequence number
144 * @report: notify via unicast netlink message
148 struct nft_af_info *afi;
149 struct nft_table *table;
150 struct nft_chain *chain;
151 const struct nlattr * const *nla;
157 struct nft_data_desc {
158 enum nft_data_types type;
162 int nft_data_init(const struct nft_ctx *ctx,
163 struct nft_data *data, unsigned int size,
164 struct nft_data_desc *desc, const struct nlattr *nla);
165 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
166 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
167 enum nft_data_types type, unsigned int len);
169 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
171 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
174 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
176 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
179 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
180 unsigned int nft_parse_register(const struct nlattr *attr);
181 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
183 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
184 int nft_validate_register_store(const struct nft_ctx *ctx,
185 enum nft_registers reg,
186 const struct nft_data *data,
187 enum nft_data_types type, unsigned int len);
190 * struct nft_userdata - user defined data associated with an object
192 * @len: length of the data
195 * The presence of user data is indicated in an object specific fashion,
196 * so a length of zero can't occur and the value "len" indicates data
199 struct nft_userdata {
201 unsigned char data[0];
205 * struct nft_set_elem - generic representation of set elements
208 * @priv: element private data and extensions
210 struct nft_set_elem {
212 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
219 struct nft_set_iter {
224 int (*fn)(const struct nft_ctx *ctx,
225 const struct nft_set *set,
226 const struct nft_set_iter *iter,
227 const struct nft_set_elem *elem);
231 * struct nft_set_desc - description of set elements
235 * @size: number of set elements
237 struct nft_set_desc {
244 * enum nft_set_class - performance class
246 * @NFT_LOOKUP_O_1: constant, O(1)
247 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
248 * @NFT_LOOKUP_O_N: linear, O(N)
252 NFT_SET_CLASS_O_LOG_N,
257 * struct nft_set_estimate - estimation of memory and performance
260 * @size: required memory
261 * @class: lookup performance class
263 struct nft_set_estimate {
265 enum nft_set_class class;
272 * struct nft_set_ops - nf_tables set operations
274 * @lookup: look up an element within the set
275 * @insert: insert new element into set
276 * @activate: activate new element in the next generation
277 * @deactivate: deactivate element in the next generation
278 * @remove: remove element from set
279 * @walk: iterate over all set elemeennts
280 * @privsize: function to return size of set private data
281 * @init: initialize private data of new set instance
282 * @destroy: destroy private data of set instance
283 * @list: nf_tables_set_ops list node
284 * @owner: module reference
285 * @elemsize: element private size
286 * @features: features supported by the implementation
289 bool (*lookup)(const struct net *net,
290 const struct nft_set *set,
292 const struct nft_set_ext **ext);
293 bool (*update)(struct nft_set *set,
295 void *(*new)(struct nft_set *,
296 const struct nft_expr *,
298 const struct nft_expr *expr,
299 struct nft_regs *regs,
300 const struct nft_set_ext **ext);
302 int (*insert)(const struct net *net,
303 const struct nft_set *set,
304 const struct nft_set_elem *elem,
305 struct nft_set_ext **ext);
306 void (*activate)(const struct net *net,
307 const struct nft_set *set,
308 const struct nft_set_elem *elem);
309 void * (*deactivate)(const struct net *net,
310 const struct nft_set *set,
311 const struct nft_set_elem *elem);
312 void (*remove)(const struct nft_set *set,
313 const struct nft_set_elem *elem);
314 void (*walk)(const struct nft_ctx *ctx,
315 const struct nft_set *set,
316 struct nft_set_iter *iter);
318 unsigned int (*privsize)(const struct nlattr * const nla[]);
319 bool (*estimate)(const struct nft_set_desc *desc,
321 struct nft_set_estimate *est);
322 int (*init)(const struct nft_set *set,
323 const struct nft_set_desc *desc,
324 const struct nlattr * const nla[]);
325 void (*destroy)(const struct nft_set *set);
327 struct list_head list;
328 struct module *owner;
329 unsigned int elemsize;
333 int nft_register_set(struct nft_set_ops *ops);
334 void nft_unregister_set(struct nft_set_ops *ops);
337 * struct nft_set - nf_tables set instance
339 * @list: table set list node
340 * @bindings: list of set bindings
341 * @name: name of the set
342 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
343 * @dtype: data type (verdict or numeric type defined by userspace)
344 * @size: maximum set size
345 * @nelems: number of elements
346 * @ndeact: number of deactivated elements queued for removal
347 * @timeout: default timeout value in jiffies
348 * @gc_int: garbage collection interval in msecs
349 * @policy: set parameterization (see enum nft_set_policies)
350 * @udlen: user data length
354 * @genmask: generation mask
357 * @data: private set data
360 struct list_head list;
361 struct list_head bindings;
362 char name[NFT_SET_MAXNAMELEN];
372 unsigned char *udata;
373 /* runtime data below here */
374 const struct nft_set_ops *ops ____cacheline_aligned;
380 __attribute__((aligned(__alignof__(u64))));
383 static inline void *nft_set_priv(const struct nft_set *set)
385 return (void *)set->data;
388 static inline struct nft_set *nft_set_container_of(const void *priv)
390 return (void *)priv - offsetof(struct nft_set, data);
393 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
394 const struct nlattr *nla, u8 genmask);
395 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
396 const struct nlattr *nla, u8 genmask);
398 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
400 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
404 * struct nft_set_binding - nf_tables set binding
406 * @list: set bindings list node
407 * @chain: chain containing the rule bound to the set
408 * @flags: set action flags
410 * A set binding contains all information necessary for validation
411 * of new elements added to a bound set.
413 struct nft_set_binding {
414 struct list_head list;
415 const struct nft_chain *chain;
419 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
420 struct nft_set_binding *binding);
421 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
422 struct nft_set_binding *binding);
425 * enum nft_set_extensions - set extension type IDs
427 * @NFT_SET_EXT_KEY: element key
428 * @NFT_SET_EXT_DATA: mapping data
429 * @NFT_SET_EXT_FLAGS: element flags
430 * @NFT_SET_EXT_TIMEOUT: element timeout
431 * @NFT_SET_EXT_EXPIRATION: element expiration time
432 * @NFT_SET_EXT_USERDATA: user data associated with the element
433 * @NFT_SET_EXT_EXPR: expression assiociated with the element
434 * @NFT_SET_EXT_NUM: number of extension types
436 enum nft_set_extensions {
441 NFT_SET_EXT_EXPIRATION,
442 NFT_SET_EXT_USERDATA,
448 * struct nft_set_ext_type - set extension type
450 * @len: fixed part length of the extension
451 * @align: alignment requirements of the extension
453 struct nft_set_ext_type {
458 extern const struct nft_set_ext_type nft_set_ext_types[];
461 * struct nft_set_ext_tmpl - set extension template
463 * @len: length of extension area
464 * @offset: offsets of individual extension types
466 struct nft_set_ext_tmpl {
468 u8 offset[NFT_SET_EXT_NUM];
472 * struct nft_set_ext - set extensions
474 * @genmask: generation mask
475 * @offset: offsets of individual extension types
476 * @data: beginning of extension data
480 u8 offset[NFT_SET_EXT_NUM];
484 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
486 memset(tmpl, 0, sizeof(*tmpl));
487 tmpl->len = sizeof(struct nft_set_ext);
490 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
493 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
494 BUG_ON(tmpl->len > U8_MAX);
495 tmpl->offset[id] = tmpl->len;
496 tmpl->len += nft_set_ext_types[id].len + len;
499 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
501 nft_set_ext_add_length(tmpl, id, 0);
504 static inline void nft_set_ext_init(struct nft_set_ext *ext,
505 const struct nft_set_ext_tmpl *tmpl)
507 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
510 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
512 return !!ext->offset[id];
515 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
517 return ext && __nft_set_ext_exists(ext, id);
520 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
522 return (void *)ext + ext->offset[id];
525 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
527 return nft_set_ext(ext, NFT_SET_EXT_KEY);
530 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
532 return nft_set_ext(ext, NFT_SET_EXT_DATA);
535 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
537 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
540 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
542 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
545 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
547 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
550 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
552 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
555 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
557 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
560 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
562 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
563 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
566 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
569 return elem + set->ops->elemsize;
572 void *nft_set_elem_init(const struct nft_set *set,
573 const struct nft_set_ext_tmpl *tmpl,
574 const u32 *key, const u32 *data,
575 u64 timeout, gfp_t gfp);
576 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
580 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
583 * @set: set the elements belong to
584 * @cnt: count of elements
586 struct nft_set_gc_batch_head {
588 const struct nft_set *set;
592 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
593 sizeof(struct nft_set_gc_batch_head)) / \
597 * struct nft_set_gc_batch - nf_tables set garbage collection batch
599 * @head: GC batch head
600 * @elems: garbage collection elements
602 struct nft_set_gc_batch {
603 struct nft_set_gc_batch_head head;
604 void *elems[NFT_SET_GC_BATCH_SIZE];
607 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
609 void nft_set_gc_batch_release(struct rcu_head *rcu);
611 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
614 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
617 static inline struct nft_set_gc_batch *
618 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
622 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
624 nft_set_gc_batch_complete(gcb);
626 return nft_set_gc_batch_alloc(set, gfp);
629 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
632 gcb->elems[gcb->head.cnt++] = elem;
636 * struct nft_expr_type - nf_tables expression type
638 * @select_ops: function to select nft_expr_ops
639 * @ops: default ops, used when no select_ops functions is present
640 * @list: used internally
642 * @owner: module reference
643 * @policy: netlink attribute policy
644 * @maxattr: highest netlink attribute number
645 * @family: address family for AF-specific types
646 * @flags: expression type flags
648 struct nft_expr_type {
649 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
650 const struct nlattr * const tb[]);
651 const struct nft_expr_ops *ops;
652 struct list_head list;
654 struct module *owner;
655 const struct nla_policy *policy;
656 unsigned int maxattr;
661 #define NFT_EXPR_STATEFUL 0x1
664 * struct nft_expr_ops - nf_tables expression operations
666 * @eval: Expression evaluation function
667 * @size: full expression size, including private data size
668 * @init: initialization function
669 * @destroy: destruction function
670 * @dump: function to dump parameters
671 * @type: expression type
672 * @validate: validate expression, called during loop detection
673 * @data: extra data to attach to this expression operation
676 struct nft_expr_ops {
677 void (*eval)(const struct nft_expr *expr,
678 struct nft_regs *regs,
679 const struct nft_pktinfo *pkt);
680 int (*clone)(struct nft_expr *dst,
681 const struct nft_expr *src);
684 int (*init)(const struct nft_ctx *ctx,
685 const struct nft_expr *expr,
686 const struct nlattr * const tb[]);
687 void (*destroy)(const struct nft_ctx *ctx,
688 const struct nft_expr *expr);
689 int (*dump)(struct sk_buff *skb,
690 const struct nft_expr *expr);
691 int (*validate)(const struct nft_ctx *ctx,
692 const struct nft_expr *expr,
693 const struct nft_data **data);
694 const struct nft_expr_type *type;
698 #define NFT_EXPR_MAXATTR 16
699 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
700 ALIGN(size, __alignof__(struct nft_expr)))
703 * struct nft_expr - nf_tables expression
705 * @ops: expression ops
706 * @data: expression private data
709 const struct nft_expr_ops *ops;
711 __attribute__((aligned(__alignof__(u64))));
714 static inline void *nft_expr_priv(const struct nft_expr *expr)
716 return (void *)expr->data;
719 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
720 const struct nlattr *nla);
721 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
722 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
723 const struct nft_expr *expr);
725 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
729 if (src->ops->clone) {
731 err = src->ops->clone(dst, src);
735 memcpy(dst, src, src->ops->size);
738 __module_get(src->ops->type->owner);
743 * struct nft_rule - nf_tables rule
745 * @list: used internally
746 * @handle: rule handle
747 * @genmask: generation mask
748 * @dlen: length of expression data
749 * @udata: user data is appended to the rule
750 * @data: expression data
753 struct list_head list;
759 __attribute__((aligned(__alignof__(struct nft_expr))));
762 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
764 return (struct nft_expr *)&rule->data[0];
767 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
769 return ((void *)expr) + expr->ops->size;
772 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
774 return (struct nft_expr *)&rule->data[rule->dlen];
777 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
779 return (void *)&rule->data[rule->dlen];
783 * The last pointer isn't really necessary, but the compiler isn't able to
784 * determine that the result of nft_expr_last() is always the same since it
785 * can't assume that the dlen value wasn't changed within calls in the loop.
787 #define nft_rule_for_each_expr(expr, last, rule) \
788 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
790 (expr) = nft_expr_next(expr))
792 enum nft_chain_flags {
793 NFT_BASE_CHAIN = 0x1,
797 * struct nft_chain - nf_tables chain
799 * @rules: list of rules in the chain
800 * @list: used internally
801 * @table: table that this chain belongs to
802 * @handle: chain handle
803 * @use: number of jump references to this chain
804 * @level: length of longest path to this chain
805 * @flags: bitmask of enum nft_chain_flags
806 * @name: name of the chain
809 struct list_head rules;
810 struct list_head list;
811 struct nft_table *table;
817 char name[NFT_CHAIN_MAXNAMELEN];
820 enum nft_chain_type {
821 NFT_CHAIN_T_DEFAULT = 0,
828 * struct nf_chain_type - nf_tables chain type info
830 * @name: name of the type
831 * @type: numeric identifier
832 * @family: address family
833 * @owner: module owner
834 * @hook_mask: mask of valid hooks
835 * @hooks: hookfn overrides
837 struct nf_chain_type {
839 enum nft_chain_type type;
841 struct module *owner;
842 unsigned int hook_mask;
843 nf_hookfn *hooks[NF_MAX_HOOKS];
846 int nft_chain_validate_dependency(const struct nft_chain *chain,
847 enum nft_chain_type type);
848 int nft_chain_validate_hooks(const struct nft_chain *chain,
849 unsigned int hook_flags);
854 struct u64_stats_sync syncp;
857 #define NFT_HOOK_OPS_MAX 2
860 * struct nft_base_chain - nf_tables base chain
862 * @ops: netfilter hook ops
864 * @policy: default policy
865 * @stats: per-cpu chain stats
867 * @dev_name: device name that this base chain is attached to (if any)
869 struct nft_base_chain {
870 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
871 const struct nf_chain_type *type;
874 struct nft_stats __percpu *stats;
875 struct nft_chain chain;
876 char dev_name[IFNAMSIZ];
879 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
881 return container_of(chain, struct nft_base_chain, chain);
884 int __nft_release_basechain(struct nft_ctx *ctx);
886 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
889 * struct nft_table - nf_tables table
891 * @list: used internally
892 * @chains: chains in the table
893 * @sets: sets in the table
894 * @hgenerator: handle generator state
895 * @use: number of chain references to this table
896 * @flags: table flag (see enum nft_table_flags)
897 * @genmask: generation mask
898 * @name: name of the table
901 struct list_head list;
902 struct list_head chains;
903 struct list_head sets;
908 char name[NFT_TABLE_MAXNAMELEN];
912 NFT_AF_NEEDS_DEV = (1 << 0),
916 * struct nft_af_info - nf_tables address family info
918 * @list: used internally
919 * @family: address family
920 * @nhooks: number of hooks in this family
921 * @owner: module owner
922 * @tables: used internally
923 * @flags: family flags
924 * @nops: number of hook ops in this family
925 * @hook_ops_init: initialization function for chain hook ops
926 * @hooks: hookfn overrides for packet validation
929 struct list_head list;
932 struct module *owner;
933 struct list_head tables;
936 void (*hook_ops_init)(struct nf_hook_ops *,
938 nf_hookfn *hooks[NF_MAX_HOOKS];
941 int nft_register_afinfo(struct net *, struct nft_af_info *);
942 void nft_unregister_afinfo(struct net *, struct nft_af_info *);
944 int nft_register_chain_type(const struct nf_chain_type *);
945 void nft_unregister_chain_type(const struct nf_chain_type *);
947 int nft_register_expr(struct nft_expr_type *);
948 void nft_unregister_expr(struct nft_expr_type *);
950 int nft_verdict_dump(struct sk_buff *skb, int type,
951 const struct nft_verdict *v);
954 * struct nft_traceinfo - nft tracing information and state
956 * @pkt: pktinfo currently processed
957 * @basechain: base chain currently processed
958 * @chain: chain currently processed
959 * @rule: rule that was evaluated
960 * @verdict: verdict given by rule
961 * @type: event type (enum nft_trace_types)
962 * @packet_dumped: packet headers sent in a previous traceinfo message
963 * @trace: other struct members are initialised
965 struct nft_traceinfo {
966 const struct nft_pktinfo *pkt;
967 const struct nft_base_chain *basechain;
968 const struct nft_chain *chain;
969 const struct nft_rule *rule;
970 const struct nft_verdict *verdict;
971 enum nft_trace_types type;
976 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
977 const struct nft_verdict *verdict,
978 const struct nft_chain *basechain);
980 void nft_trace_notify(struct nft_traceinfo *info);
982 #define nft_dereference(p) \
983 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
985 #define MODULE_ALIAS_NFT_FAMILY(family) \
986 MODULE_ALIAS("nft-afinfo-" __stringify(family))
988 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
989 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
991 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
992 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
994 #define MODULE_ALIAS_NFT_EXPR(name) \
995 MODULE_ALIAS("nft-expr-" name)
997 #define MODULE_ALIAS_NFT_SET() \
998 MODULE_ALIAS("nft-set")
1001 * The gencursor defines two generations, the currently active and the
1002 * next one. Objects contain a bitmask of 2 bits specifying the generations
1003 * they're active in. A set bit means they're inactive in the generation
1004 * represented by that bit.
1006 * New objects start out as inactive in the current and active in the
1007 * next generation. When committing the ruleset the bitmask is cleared,
1008 * meaning they're active in all generations. When removing an object,
1009 * it is set inactive in the next generation. After committing the ruleset,
1010 * the objects are removed.
1012 static inline unsigned int nft_gencursor_next(const struct net *net)
1014 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1017 static inline u8 nft_genmask_next(const struct net *net)
1019 return 1 << nft_gencursor_next(net);
1022 static inline u8 nft_genmask_cur(const struct net *net)
1024 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1025 return 1 << ACCESS_ONCE(net->nft.gencursor);
1028 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1031 * Generic transaction helpers
1034 /* Check if this object is currently active. */
1035 #define nft_is_active(__net, __obj) \
1036 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1038 /* Check if this object is active in the next generation. */
1039 #define nft_is_active_next(__net, __obj) \
1040 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1042 /* This object becomes active in the next generation. */
1043 #define nft_activate_next(__net, __obj) \
1044 (__obj)->genmask = nft_genmask_cur(__net)
1046 /* This object becomes inactive in the next generation. */
1047 #define nft_deactivate_next(__net, __obj) \
1048 (__obj)->genmask = nft_genmask_next(__net)
1050 /* After committing the ruleset, clear the stale generation bit. */
1051 #define nft_clear(__net, __obj) \
1052 (__obj)->genmask &= ~nft_genmask_next(__net)
1053 #define nft_active_genmask(__obj, __genmask) \
1054 !((__obj)->genmask & __genmask)
1057 * Set element transaction helpers
1060 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1063 return !(ext->genmask & genmask);
1066 static inline void nft_set_elem_change_active(const struct net *net,
1067 const struct nft_set *set,
1068 struct nft_set_ext *ext)
1070 ext->genmask ^= nft_genmask_next(net);
1074 * We use a free bit in the genmask field to indicate the element
1075 * is busy, meaning it is currently being processed either by
1076 * the netlink API or GC.
1078 * Even though the genmask is only a single byte wide, this works
1079 * because the extension structure if fully constant once initialized,
1080 * so there are no non-atomic write accesses unless it is already
1083 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1085 #if defined(__LITTLE_ENDIAN_BITFIELD)
1086 #define NFT_SET_ELEM_BUSY_BIT 2
1087 #elif defined(__BIG_ENDIAN_BITFIELD)
1088 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1093 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1095 unsigned long *word = (unsigned long *)ext;
1097 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1098 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1101 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1103 unsigned long *word = (unsigned long *)ext;
1105 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1109 * struct nft_trans - nf_tables object update in transaction
1111 * @list: used internally
1112 * @msg_type: message type
1113 * @ctx: transaction context
1114 * @data: internal information related to the transaction
1117 struct list_head list;
1123 struct nft_trans_rule {
1124 struct nft_rule *rule;
1127 #define nft_trans_rule(trans) \
1128 (((struct nft_trans_rule *)trans->data)->rule)
1130 struct nft_trans_set {
1131 struct nft_set *set;
1135 #define nft_trans_set(trans) \
1136 (((struct nft_trans_set *)trans->data)->set)
1137 #define nft_trans_set_id(trans) \
1138 (((struct nft_trans_set *)trans->data)->set_id)
1140 struct nft_trans_chain {
1142 char name[NFT_CHAIN_MAXNAMELEN];
1143 struct nft_stats __percpu *stats;
1147 #define nft_trans_chain_update(trans) \
1148 (((struct nft_trans_chain *)trans->data)->update)
1149 #define nft_trans_chain_name(trans) \
1150 (((struct nft_trans_chain *)trans->data)->name)
1151 #define nft_trans_chain_stats(trans) \
1152 (((struct nft_trans_chain *)trans->data)->stats)
1153 #define nft_trans_chain_policy(trans) \
1154 (((struct nft_trans_chain *)trans->data)->policy)
1156 struct nft_trans_table {
1161 #define nft_trans_table_update(trans) \
1162 (((struct nft_trans_table *)trans->data)->update)
1163 #define nft_trans_table_enable(trans) \
1164 (((struct nft_trans_table *)trans->data)->enable)
1166 struct nft_trans_elem {
1167 struct nft_set *set;
1168 struct nft_set_elem elem;
1171 #define nft_trans_elem_set(trans) \
1172 (((struct nft_trans_elem *)trans->data)->set)
1173 #define nft_trans_elem(trans) \
1174 (((struct nft_trans_elem *)trans->data)->elem)
1176 #endif /* _NET_NF_TABLES_H */