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
19 /* for x_tables compatibility */
20 struct xt_action_param xt;
23 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
25 return pkt->xt.state->net;
28 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
30 return pkt->xt.state->hook;
33 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
35 return pkt->xt.state->pf;
38 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
40 return pkt->xt.state->in;
43 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
45 return pkt->xt.state->out;
48 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
50 const struct nf_hook_state *state)
53 pkt->xt.state = state;
56 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo *pkt,
59 pkt->tprot_set = false;
65 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
67 const struct nf_hook_state *state)
69 nft_set_pktinfo(pkt, skb, state);
70 nft_set_pktinfo_proto_unspec(pkt, skb);
74 * struct nft_verdict - nf_tables verdict
76 * @code: nf_tables/netfilter verdict code
77 * @chain: destination chain for NFT_JUMP/NFT_GOTO
81 struct nft_chain *chain;
87 struct nft_verdict verdict;
89 } __attribute__((aligned(__alignof__(u64))));
92 * struct nft_regs - nf_tables register set
94 * @data: data registers
95 * @verdict: verdict register
97 * The first four data registers alias to the verdict register.
102 struct nft_verdict verdict;
106 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
109 memcpy(dst, src, len);
112 static inline void nft_data_debug(const struct nft_data *data)
114 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
115 data->data[0], data->data[1],
116 data->data[2], data->data[3]);
120 * struct nft_ctx - nf_tables rule/set context
122 * @net: net namespace
123 * @afi: address family info
124 * @table: the table the chain is contained in
125 * @chain: the chain the rule is contained in
126 * @nla: netlink attributes
127 * @portid: netlink portID of the original message
128 * @seq: netlink sequence number
129 * @report: notify via unicast netlink message
133 struct nft_af_info *afi;
134 struct nft_table *table;
135 struct nft_chain *chain;
136 const struct nlattr * const *nla;
142 struct nft_data_desc {
143 enum nft_data_types type;
147 int nft_data_init(const struct nft_ctx *ctx,
148 struct nft_data *data, unsigned int size,
149 struct nft_data_desc *desc, const struct nlattr *nla);
150 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
151 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
152 enum nft_data_types type, unsigned int len);
154 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
156 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
159 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
161 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
164 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
165 unsigned int nft_parse_register(const struct nlattr *attr);
166 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
168 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
169 int nft_validate_register_store(const struct nft_ctx *ctx,
170 enum nft_registers reg,
171 const struct nft_data *data,
172 enum nft_data_types type, unsigned int len);
175 * struct nft_userdata - user defined data associated with an object
177 * @len: length of the data
180 * The presence of user data is indicated in an object specific fashion,
181 * so a length of zero can't occur and the value "len" indicates data
184 struct nft_userdata {
186 unsigned char data[0];
190 * struct nft_set_elem - generic representation of set elements
193 * @priv: element private data and extensions
195 struct nft_set_elem {
197 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
204 struct nft_set_iter {
209 int (*fn)(const struct nft_ctx *ctx,
210 const struct nft_set *set,
211 const struct nft_set_iter *iter,
212 const struct nft_set_elem *elem);
216 * struct nft_set_desc - description of set elements
220 * @size: number of set elements
222 struct nft_set_desc {
229 * enum nft_set_class - performance class
231 * @NFT_LOOKUP_O_1: constant, O(1)
232 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
233 * @NFT_LOOKUP_O_N: linear, O(N)
237 NFT_SET_CLASS_O_LOG_N,
242 * struct nft_set_estimate - estimation of memory and performance
245 * @size: required memory
246 * @class: lookup performance class
248 struct nft_set_estimate {
250 enum nft_set_class class;
257 * struct nft_set_ops - nf_tables set operations
259 * @lookup: look up an element within the set
260 * @insert: insert new element into set
261 * @activate: activate new element in the next generation
262 * @deactivate: deactivate element in the next generation
263 * @remove: remove element from set
264 * @walk: iterate over all set elemeennts
265 * @privsize: function to return size of set private data
266 * @init: initialize private data of new set instance
267 * @destroy: destroy private data of set instance
268 * @list: nf_tables_set_ops list node
269 * @owner: module reference
270 * @elemsize: element private size
271 * @features: features supported by the implementation
274 bool (*lookup)(const struct net *net,
275 const struct nft_set *set,
277 const struct nft_set_ext **ext);
278 bool (*update)(struct nft_set *set,
280 void *(*new)(struct nft_set *,
281 const struct nft_expr *,
283 const struct nft_expr *expr,
284 struct nft_regs *regs,
285 const struct nft_set_ext **ext);
287 int (*insert)(const struct net *net,
288 const struct nft_set *set,
289 const struct nft_set_elem *elem,
290 struct nft_set_ext **ext);
291 void (*activate)(const struct net *net,
292 const struct nft_set *set,
293 const struct nft_set_elem *elem);
294 void * (*deactivate)(const struct net *net,
295 const struct nft_set *set,
296 const struct nft_set_elem *elem);
297 void (*remove)(const struct nft_set *set,
298 const struct nft_set_elem *elem);
299 void (*walk)(const struct nft_ctx *ctx,
300 const struct nft_set *set,
301 struct nft_set_iter *iter);
303 unsigned int (*privsize)(const struct nlattr * const nla[]);
304 bool (*estimate)(const struct nft_set_desc *desc,
306 struct nft_set_estimate *est);
307 int (*init)(const struct nft_set *set,
308 const struct nft_set_desc *desc,
309 const struct nlattr * const nla[]);
310 void (*destroy)(const struct nft_set *set);
312 struct list_head list;
313 struct module *owner;
314 unsigned int elemsize;
318 int nft_register_set(struct nft_set_ops *ops);
319 void nft_unregister_set(struct nft_set_ops *ops);
322 * struct nft_set - nf_tables set instance
324 * @list: table set list node
325 * @bindings: list of set bindings
326 * @name: name of the set
327 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
328 * @dtype: data type (verdict or numeric type defined by userspace)
329 * @size: maximum set size
330 * @nelems: number of elements
331 * @ndeact: number of deactivated elements queued for removal
332 * @timeout: default timeout value in jiffies
333 * @gc_int: garbage collection interval in msecs
334 * @policy: set parameterization (see enum nft_set_policies)
335 * @udlen: user data length
339 * @genmask: generation mask
342 * @data: private set data
345 struct list_head list;
346 struct list_head bindings;
347 char name[NFT_SET_MAXNAMELEN];
357 unsigned char *udata;
358 /* runtime data below here */
359 const struct nft_set_ops *ops ____cacheline_aligned;
365 __attribute__((aligned(__alignof__(u64))));
368 static inline void *nft_set_priv(const struct nft_set *set)
370 return (void *)set->data;
373 static inline struct nft_set *nft_set_container_of(const void *priv)
375 return (void *)priv - offsetof(struct nft_set, data);
378 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
379 const struct nlattr *nla, u8 genmask);
380 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
381 const struct nlattr *nla, u8 genmask);
383 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
385 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
389 * struct nft_set_binding - nf_tables set binding
391 * @list: set bindings list node
392 * @chain: chain containing the rule bound to the set
393 * @flags: set action flags
395 * A set binding contains all information necessary for validation
396 * of new elements added to a bound set.
398 struct nft_set_binding {
399 struct list_head list;
400 const struct nft_chain *chain;
404 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
405 struct nft_set_binding *binding);
406 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
407 struct nft_set_binding *binding);
410 * enum nft_set_extensions - set extension type IDs
412 * @NFT_SET_EXT_KEY: element key
413 * @NFT_SET_EXT_DATA: mapping data
414 * @NFT_SET_EXT_FLAGS: element flags
415 * @NFT_SET_EXT_TIMEOUT: element timeout
416 * @NFT_SET_EXT_EXPIRATION: element expiration time
417 * @NFT_SET_EXT_USERDATA: user data associated with the element
418 * @NFT_SET_EXT_EXPR: expression assiociated with the element
419 * @NFT_SET_EXT_NUM: number of extension types
421 enum nft_set_extensions {
426 NFT_SET_EXT_EXPIRATION,
427 NFT_SET_EXT_USERDATA,
433 * struct nft_set_ext_type - set extension type
435 * @len: fixed part length of the extension
436 * @align: alignment requirements of the extension
438 struct nft_set_ext_type {
443 extern const struct nft_set_ext_type nft_set_ext_types[];
446 * struct nft_set_ext_tmpl - set extension template
448 * @len: length of extension area
449 * @offset: offsets of individual extension types
451 struct nft_set_ext_tmpl {
453 u8 offset[NFT_SET_EXT_NUM];
457 * struct nft_set_ext - set extensions
459 * @genmask: generation mask
460 * @offset: offsets of individual extension types
461 * @data: beginning of extension data
465 u8 offset[NFT_SET_EXT_NUM];
469 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
471 memset(tmpl, 0, sizeof(*tmpl));
472 tmpl->len = sizeof(struct nft_set_ext);
475 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
478 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
479 BUG_ON(tmpl->len > U8_MAX);
480 tmpl->offset[id] = tmpl->len;
481 tmpl->len += nft_set_ext_types[id].len + len;
484 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
486 nft_set_ext_add_length(tmpl, id, 0);
489 static inline void nft_set_ext_init(struct nft_set_ext *ext,
490 const struct nft_set_ext_tmpl *tmpl)
492 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
495 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
497 return !!ext->offset[id];
500 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
502 return ext && __nft_set_ext_exists(ext, id);
505 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
507 return (void *)ext + ext->offset[id];
510 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
512 return nft_set_ext(ext, NFT_SET_EXT_KEY);
515 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
517 return nft_set_ext(ext, NFT_SET_EXT_DATA);
520 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
522 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
525 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
527 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
530 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
532 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
535 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
537 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
540 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
542 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
545 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
547 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
548 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
551 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
554 return elem + set->ops->elemsize;
557 void *nft_set_elem_init(const struct nft_set *set,
558 const struct nft_set_ext_tmpl *tmpl,
559 const u32 *key, const u32 *data,
560 u64 timeout, gfp_t gfp);
561 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
565 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
568 * @set: set the elements belong to
569 * @cnt: count of elements
571 struct nft_set_gc_batch_head {
573 const struct nft_set *set;
577 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
578 sizeof(struct nft_set_gc_batch_head)) / \
582 * struct nft_set_gc_batch - nf_tables set garbage collection batch
584 * @head: GC batch head
585 * @elems: garbage collection elements
587 struct nft_set_gc_batch {
588 struct nft_set_gc_batch_head head;
589 void *elems[NFT_SET_GC_BATCH_SIZE];
592 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
594 void nft_set_gc_batch_release(struct rcu_head *rcu);
596 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
599 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
602 static inline struct nft_set_gc_batch *
603 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
607 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
609 nft_set_gc_batch_complete(gcb);
611 return nft_set_gc_batch_alloc(set, gfp);
614 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
617 gcb->elems[gcb->head.cnt++] = elem;
621 * struct nft_expr_type - nf_tables expression type
623 * @select_ops: function to select nft_expr_ops
624 * @ops: default ops, used when no select_ops functions is present
625 * @list: used internally
627 * @owner: module reference
628 * @policy: netlink attribute policy
629 * @maxattr: highest netlink attribute number
630 * @family: address family for AF-specific types
631 * @flags: expression type flags
633 struct nft_expr_type {
634 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
635 const struct nlattr * const tb[]);
636 const struct nft_expr_ops *ops;
637 struct list_head list;
639 struct module *owner;
640 const struct nla_policy *policy;
641 unsigned int maxattr;
646 #define NFT_EXPR_STATEFUL 0x1
649 * struct nft_expr_ops - nf_tables expression operations
651 * @eval: Expression evaluation function
652 * @size: full expression size, including private data size
653 * @init: initialization function
654 * @destroy: destruction function
655 * @dump: function to dump parameters
656 * @type: expression type
657 * @validate: validate expression, called during loop detection
658 * @data: extra data to attach to this expression operation
661 struct nft_expr_ops {
662 void (*eval)(const struct nft_expr *expr,
663 struct nft_regs *regs,
664 const struct nft_pktinfo *pkt);
665 int (*clone)(struct nft_expr *dst,
666 const struct nft_expr *src);
669 int (*init)(const struct nft_ctx *ctx,
670 const struct nft_expr *expr,
671 const struct nlattr * const tb[]);
672 void (*destroy)(const struct nft_ctx *ctx,
673 const struct nft_expr *expr);
674 int (*dump)(struct sk_buff *skb,
675 const struct nft_expr *expr);
676 int (*validate)(const struct nft_ctx *ctx,
677 const struct nft_expr *expr,
678 const struct nft_data **data);
679 const struct nft_expr_type *type;
683 #define NFT_EXPR_MAXATTR 16
684 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
685 ALIGN(size, __alignof__(struct nft_expr)))
688 * struct nft_expr - nf_tables expression
690 * @ops: expression ops
691 * @data: expression private data
694 const struct nft_expr_ops *ops;
695 unsigned char data[];
698 static inline void *nft_expr_priv(const struct nft_expr *expr)
700 return (void *)expr->data;
703 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
704 const struct nlattr *nla);
705 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
706 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
707 const struct nft_expr *expr);
709 static inline int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
713 if (src->ops->clone) {
715 err = src->ops->clone(dst, src);
719 memcpy(dst, src, src->ops->size);
722 __module_get(src->ops->type->owner);
727 * struct nft_rule - nf_tables rule
729 * @list: used internally
730 * @handle: rule handle
731 * @genmask: generation mask
732 * @dlen: length of expression data
733 * @udata: user data is appended to the rule
734 * @data: expression data
737 struct list_head list;
743 __attribute__((aligned(__alignof__(struct nft_expr))));
746 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
748 return (struct nft_expr *)&rule->data[0];
751 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
753 return ((void *)expr) + expr->ops->size;
756 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
758 return (struct nft_expr *)&rule->data[rule->dlen];
761 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
763 return (void *)&rule->data[rule->dlen];
767 * The last pointer isn't really necessary, but the compiler isn't able to
768 * determine that the result of nft_expr_last() is always the same since it
769 * can't assume that the dlen value wasn't changed within calls in the loop.
771 #define nft_rule_for_each_expr(expr, last, rule) \
772 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
774 (expr) = nft_expr_next(expr))
776 enum nft_chain_flags {
777 NFT_BASE_CHAIN = 0x1,
781 * struct nft_chain - nf_tables chain
783 * @rules: list of rules in the chain
784 * @list: used internally
785 * @table: table that this chain belongs to
786 * @handle: chain handle
787 * @use: number of jump references to this chain
788 * @level: length of longest path to this chain
789 * @flags: bitmask of enum nft_chain_flags
790 * @name: name of the chain
793 struct list_head rules;
794 struct list_head list;
795 struct nft_table *table;
801 char name[NFT_CHAIN_MAXNAMELEN];
804 enum nft_chain_type {
805 NFT_CHAIN_T_DEFAULT = 0,
812 * struct nf_chain_type - nf_tables chain type info
814 * @name: name of the type
815 * @type: numeric identifier
816 * @family: address family
817 * @owner: module owner
818 * @hook_mask: mask of valid hooks
819 * @hooks: hookfn overrides
821 struct nf_chain_type {
823 enum nft_chain_type type;
825 struct module *owner;
826 unsigned int hook_mask;
827 nf_hookfn *hooks[NF_MAX_HOOKS];
830 int nft_chain_validate_dependency(const struct nft_chain *chain,
831 enum nft_chain_type type);
832 int nft_chain_validate_hooks(const struct nft_chain *chain,
833 unsigned int hook_flags);
838 struct u64_stats_sync syncp;
841 #define NFT_HOOK_OPS_MAX 2
844 * struct nft_base_chain - nf_tables base chain
846 * @ops: netfilter hook ops
848 * @policy: default policy
849 * @stats: per-cpu chain stats
851 * @dev_name: device name that this base chain is attached to (if any)
853 struct nft_base_chain {
854 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
855 const struct nf_chain_type *type;
858 struct nft_stats __percpu *stats;
859 struct nft_chain chain;
860 char dev_name[IFNAMSIZ];
863 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
865 return container_of(chain, struct nft_base_chain, chain);
868 int __nft_release_basechain(struct nft_ctx *ctx);
870 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
873 * struct nft_table - nf_tables table
875 * @list: used internally
876 * @chains: chains in the table
877 * @sets: sets in the table
878 * @hgenerator: handle generator state
879 * @use: number of chain references to this table
880 * @flags: table flag (see enum nft_table_flags)
881 * @genmask: generation mask
882 * @name: name of the table
885 struct list_head list;
886 struct list_head chains;
887 struct list_head sets;
892 char name[NFT_TABLE_MAXNAMELEN];
896 NFT_AF_NEEDS_DEV = (1 << 0),
900 * struct nft_af_info - nf_tables address family info
902 * @list: used internally
903 * @family: address family
904 * @nhooks: number of hooks in this family
905 * @owner: module owner
906 * @tables: used internally
907 * @flags: family flags
908 * @nops: number of hook ops in this family
909 * @hook_ops_init: initialization function for chain hook ops
910 * @hooks: hookfn overrides for packet validation
913 struct list_head list;
916 struct module *owner;
917 struct list_head tables;
920 void (*hook_ops_init)(struct nf_hook_ops *,
922 nf_hookfn *hooks[NF_MAX_HOOKS];
925 int nft_register_afinfo(struct net *, struct nft_af_info *);
926 void nft_unregister_afinfo(struct net *, struct nft_af_info *);
928 int nft_register_chain_type(const struct nf_chain_type *);
929 void nft_unregister_chain_type(const struct nf_chain_type *);
931 int nft_register_expr(struct nft_expr_type *);
932 void nft_unregister_expr(struct nft_expr_type *);
934 int nft_verdict_dump(struct sk_buff *skb, int type,
935 const struct nft_verdict *v);
938 * struct nft_traceinfo - nft tracing information and state
940 * @pkt: pktinfo currently processed
941 * @basechain: base chain currently processed
942 * @chain: chain currently processed
943 * @rule: rule that was evaluated
944 * @verdict: verdict given by rule
945 * @type: event type (enum nft_trace_types)
946 * @packet_dumped: packet headers sent in a previous traceinfo message
947 * @trace: other struct members are initialised
949 struct nft_traceinfo {
950 const struct nft_pktinfo *pkt;
951 const struct nft_base_chain *basechain;
952 const struct nft_chain *chain;
953 const struct nft_rule *rule;
954 const struct nft_verdict *verdict;
955 enum nft_trace_types type;
960 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
961 const struct nft_verdict *verdict,
962 const struct nft_chain *basechain);
964 void nft_trace_notify(struct nft_traceinfo *info);
966 #define nft_dereference(p) \
967 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
969 #define MODULE_ALIAS_NFT_FAMILY(family) \
970 MODULE_ALIAS("nft-afinfo-" __stringify(family))
972 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
973 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
975 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
976 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
978 #define MODULE_ALIAS_NFT_EXPR(name) \
979 MODULE_ALIAS("nft-expr-" name)
981 #define MODULE_ALIAS_NFT_SET() \
982 MODULE_ALIAS("nft-set")
985 * The gencursor defines two generations, the currently active and the
986 * next one. Objects contain a bitmask of 2 bits specifying the generations
987 * they're active in. A set bit means they're inactive in the generation
988 * represented by that bit.
990 * New objects start out as inactive in the current and active in the
991 * next generation. When committing the ruleset the bitmask is cleared,
992 * meaning they're active in all generations. When removing an object,
993 * it is set inactive in the next generation. After committing the ruleset,
994 * the objects are removed.
996 static inline unsigned int nft_gencursor_next(const struct net *net)
998 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1001 static inline u8 nft_genmask_next(const struct net *net)
1003 return 1 << nft_gencursor_next(net);
1006 static inline u8 nft_genmask_cur(const struct net *net)
1008 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1009 return 1 << ACCESS_ONCE(net->nft.gencursor);
1012 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1015 * Generic transaction helpers
1018 /* Check if this object is currently active. */
1019 #define nft_is_active(__net, __obj) \
1020 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1022 /* Check if this object is active in the next generation. */
1023 #define nft_is_active_next(__net, __obj) \
1024 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1026 /* This object becomes active in the next generation. */
1027 #define nft_activate_next(__net, __obj) \
1028 (__obj)->genmask = nft_genmask_cur(__net)
1030 /* This object becomes inactive in the next generation. */
1031 #define nft_deactivate_next(__net, __obj) \
1032 (__obj)->genmask = nft_genmask_next(__net)
1034 /* After committing the ruleset, clear the stale generation bit. */
1035 #define nft_clear(__net, __obj) \
1036 (__obj)->genmask &= ~nft_genmask_next(__net)
1037 #define nft_active_genmask(__obj, __genmask) \
1038 !((__obj)->genmask & __genmask)
1041 * Set element transaction helpers
1044 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1047 return !(ext->genmask & genmask);
1050 static inline void nft_set_elem_change_active(const struct net *net,
1051 const struct nft_set *set,
1052 struct nft_set_ext *ext)
1054 ext->genmask ^= nft_genmask_next(net);
1058 * We use a free bit in the genmask field to indicate the element
1059 * is busy, meaning it is currently being processed either by
1060 * the netlink API or GC.
1062 * Even though the genmask is only a single byte wide, this works
1063 * because the extension structure if fully constant once initialized,
1064 * so there are no non-atomic write accesses unless it is already
1067 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1069 #if defined(__LITTLE_ENDIAN_BITFIELD)
1070 #define NFT_SET_ELEM_BUSY_BIT 2
1071 #elif defined(__BIG_ENDIAN_BITFIELD)
1072 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1077 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1079 unsigned long *word = (unsigned long *)ext;
1081 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1082 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1085 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1087 unsigned long *word = (unsigned long *)ext;
1089 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1093 * struct nft_trans - nf_tables object update in transaction
1095 * @list: used internally
1096 * @msg_type: message type
1097 * @ctx: transaction context
1098 * @data: internal information related to the transaction
1101 struct list_head list;
1107 struct nft_trans_rule {
1108 struct nft_rule *rule;
1111 #define nft_trans_rule(trans) \
1112 (((struct nft_trans_rule *)trans->data)->rule)
1114 struct nft_trans_set {
1115 struct nft_set *set;
1119 #define nft_trans_set(trans) \
1120 (((struct nft_trans_set *)trans->data)->set)
1121 #define nft_trans_set_id(trans) \
1122 (((struct nft_trans_set *)trans->data)->set_id)
1124 struct nft_trans_chain {
1126 char name[NFT_CHAIN_MAXNAMELEN];
1127 struct nft_stats __percpu *stats;
1131 #define nft_trans_chain_update(trans) \
1132 (((struct nft_trans_chain *)trans->data)->update)
1133 #define nft_trans_chain_name(trans) \
1134 (((struct nft_trans_chain *)trans->data)->name)
1135 #define nft_trans_chain_stats(trans) \
1136 (((struct nft_trans_chain *)trans->data)->stats)
1137 #define nft_trans_chain_policy(trans) \
1138 (((struct nft_trans_chain *)trans->data)->policy)
1140 struct nft_trans_table {
1145 #define nft_trans_table_update(trans) \
1146 (((struct nft_trans_table *)trans->data)->update)
1147 #define nft_trans_table_enable(trans) \
1148 (((struct nft_trans_table *)trans->data)->enable)
1150 struct nft_trans_elem {
1151 struct nft_set *set;
1152 struct nft_set_elem elem;
1155 #define nft_trans_elem_set(trans) \
1156 (((struct nft_trans_elem *)trans->data)->set)
1157 #define nft_trans_elem(trans) \
1158 (((struct nft_trans_elem *)trans->data)->elem)
1160 #endif /* _NET_NF_TABLES_H */