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;
25 /* for x_tables compatibility */
26 struct xt_action_param xt;
29 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
31 const struct nf_hook_state *state)
34 pkt->net = pkt->xt.net = state->net;
35 pkt->in = pkt->xt.in = state->in;
36 pkt->out = pkt->xt.out = state->out;
37 pkt->hook = pkt->xt.hooknum = state->hook;
38 pkt->pf = pkt->xt.family = state->pf;
42 * struct nft_verdict - nf_tables verdict
44 * @code: nf_tables/netfilter verdict code
45 * @chain: destination chain for NFT_JUMP/NFT_GOTO
49 struct nft_chain *chain;
55 struct nft_verdict verdict;
57 } __attribute__((aligned(__alignof__(u64))));
60 * struct nft_regs - nf_tables register set
62 * @data: data registers
63 * @verdict: verdict register
65 * The first four data registers alias to the verdict register.
70 struct nft_verdict verdict;
74 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
77 memcpy(dst, src, len);
80 static inline void nft_data_debug(const struct nft_data *data)
82 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
83 data->data[0], data->data[1],
84 data->data[2], data->data[3]);
88 * struct nft_ctx - nf_tables rule/set context
91 * @afi: address family info
92 * @table: the table the chain is contained in
93 * @chain: the chain the rule is contained in
94 * @nla: netlink attributes
95 * @portid: netlink portID of the original message
96 * @seq: netlink sequence number
97 * @report: notify via unicast netlink message
101 struct nft_af_info *afi;
102 struct nft_table *table;
103 struct nft_chain *chain;
104 const struct nlattr * const *nla;
110 struct nft_data_desc {
111 enum nft_data_types type;
115 int nft_data_init(const struct nft_ctx *ctx,
116 struct nft_data *data, unsigned int size,
117 struct nft_data_desc *desc, const struct nlattr *nla);
118 void nft_data_uninit(const struct nft_data *data, enum nft_data_types type);
119 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
120 enum nft_data_types type, unsigned int len);
122 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
124 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
127 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
129 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
132 unsigned int nft_parse_register(const struct nlattr *attr);
133 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
135 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
136 int nft_validate_register_store(const struct nft_ctx *ctx,
137 enum nft_registers reg,
138 const struct nft_data *data,
139 enum nft_data_types type, unsigned int len);
142 * struct nft_userdata - user defined data associated with an object
144 * @len: length of the data
147 * The presence of user data is indicated in an object specific fashion,
148 * so a length of zero can't occur and the value "len" indicates data
151 struct nft_userdata {
153 unsigned char data[0];
157 * struct nft_set_elem - generic representation of set elements
160 * @priv: element private data and extensions
162 struct nft_set_elem {
164 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
171 struct nft_set_iter {
175 int (*fn)(const struct nft_ctx *ctx,
176 const struct nft_set *set,
177 const struct nft_set_iter *iter,
178 const struct nft_set_elem *elem);
182 * struct nft_set_desc - description of set elements
186 * @size: number of set elements
188 struct nft_set_desc {
195 * enum nft_set_class - performance class
197 * @NFT_LOOKUP_O_1: constant, O(1)
198 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
199 * @NFT_LOOKUP_O_N: linear, O(N)
203 NFT_SET_CLASS_O_LOG_N,
208 * struct nft_set_estimate - estimation of memory and performance
211 * @size: required memory
212 * @class: lookup performance class
214 struct nft_set_estimate {
216 enum nft_set_class class;
223 * struct nft_set_ops - nf_tables set operations
225 * @lookup: look up an element within the set
226 * @insert: insert new element into set
227 * @activate: activate new element in the next generation
228 * @deactivate: deactivate element in the next generation
229 * @remove: remove element from set
230 * @walk: iterate over all set elemeennts
231 * @privsize: function to return size of set private data
232 * @init: initialize private data of new set instance
233 * @destroy: destroy private data of set instance
234 * @list: nf_tables_set_ops list node
235 * @owner: module reference
236 * @elemsize: element private size
237 * @features: features supported by the implementation
240 bool (*lookup)(const struct nft_set *set,
242 const struct nft_set_ext **ext);
243 bool (*update)(struct nft_set *set,
245 void *(*new)(struct nft_set *,
246 const struct nft_expr *,
248 const struct nft_expr *expr,
249 struct nft_regs *regs,
250 const struct nft_set_ext **ext);
252 int (*insert)(const struct nft_set *set,
253 const struct nft_set_elem *elem);
254 void (*activate)(const struct nft_set *set,
255 const struct nft_set_elem *elem);
256 void * (*deactivate)(const struct nft_set *set,
257 const struct nft_set_elem *elem);
258 void (*remove)(const struct nft_set *set,
259 const struct nft_set_elem *elem);
260 void (*walk)(const struct nft_ctx *ctx,
261 const struct nft_set *set,
262 struct nft_set_iter *iter);
264 unsigned int (*privsize)(const struct nlattr * const nla[]);
265 bool (*estimate)(const struct nft_set_desc *desc,
267 struct nft_set_estimate *est);
268 int (*init)(const struct nft_set *set,
269 const struct nft_set_desc *desc,
270 const struct nlattr * const nla[]);
271 void (*destroy)(const struct nft_set *set);
273 struct list_head list;
274 struct module *owner;
275 unsigned int elemsize;
279 int nft_register_set(struct nft_set_ops *ops);
280 void nft_unregister_set(struct nft_set_ops *ops);
283 * struct nft_set - nf_tables set instance
285 * @list: table set list node
286 * @bindings: list of set bindings
287 * @name: name of the set
288 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
289 * @dtype: data type (verdict or numeric type defined by userspace)
290 * @size: maximum set size
291 * @nelems: number of elements
292 * @ndeact: number of deactivated elements queued for removal
293 * @timeout: default timeout value in msecs
294 * @gc_int: garbage collection interval in msecs
295 * @policy: set parameterization (see enum nft_set_policies)
297 * @pnet: network namespace
301 * @data: private set data
304 struct list_head list;
305 struct list_head bindings;
315 /* runtime data below here */
316 const struct nft_set_ops *ops ____cacheline_aligned;
322 __attribute__((aligned(__alignof__(u64))));
325 static inline void *nft_set_priv(const struct nft_set *set)
327 return (void *)set->data;
330 static inline struct nft_set *nft_set_container_of(const void *priv)
332 return (void *)priv - offsetof(struct nft_set, data);
335 struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
336 const struct nlattr *nla);
337 struct nft_set *nf_tables_set_lookup_byid(const struct net *net,
338 const struct nlattr *nla);
340 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
342 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
346 * struct nft_set_binding - nf_tables set binding
348 * @list: set bindings list node
349 * @chain: chain containing the rule bound to the set
350 * @flags: set action flags
352 * A set binding contains all information necessary for validation
353 * of new elements added to a bound set.
355 struct nft_set_binding {
356 struct list_head list;
357 const struct nft_chain *chain;
361 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
362 struct nft_set_binding *binding);
363 void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
364 struct nft_set_binding *binding);
367 * enum nft_set_extensions - set extension type IDs
369 * @NFT_SET_EXT_KEY: element key
370 * @NFT_SET_EXT_DATA: mapping data
371 * @NFT_SET_EXT_FLAGS: element flags
372 * @NFT_SET_EXT_TIMEOUT: element timeout
373 * @NFT_SET_EXT_EXPIRATION: element expiration time
374 * @NFT_SET_EXT_USERDATA: user data associated with the element
375 * @NFT_SET_EXT_EXPR: expression assiociated with the element
376 * @NFT_SET_EXT_NUM: number of extension types
378 enum nft_set_extensions {
383 NFT_SET_EXT_EXPIRATION,
384 NFT_SET_EXT_USERDATA,
390 * struct nft_set_ext_type - set extension type
392 * @len: fixed part length of the extension
393 * @align: alignment requirements of the extension
395 struct nft_set_ext_type {
400 extern const struct nft_set_ext_type nft_set_ext_types[];
403 * struct nft_set_ext_tmpl - set extension template
405 * @len: length of extension area
406 * @offset: offsets of individual extension types
408 struct nft_set_ext_tmpl {
410 u8 offset[NFT_SET_EXT_NUM];
414 * struct nft_set_ext - set extensions
416 * @genmask: generation mask
417 * @offset: offsets of individual extension types
418 * @data: beginning of extension data
422 u8 offset[NFT_SET_EXT_NUM];
426 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
428 memset(tmpl, 0, sizeof(*tmpl));
429 tmpl->len = sizeof(struct nft_set_ext);
432 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
435 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
436 BUG_ON(tmpl->len > U8_MAX);
437 tmpl->offset[id] = tmpl->len;
438 tmpl->len += nft_set_ext_types[id].len + len;
441 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
443 nft_set_ext_add_length(tmpl, id, 0);
446 static inline void nft_set_ext_init(struct nft_set_ext *ext,
447 const struct nft_set_ext_tmpl *tmpl)
449 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
452 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
454 return !!ext->offset[id];
457 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
459 return ext && __nft_set_ext_exists(ext, id);
462 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
464 return (void *)ext + ext->offset[id];
467 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
469 return nft_set_ext(ext, NFT_SET_EXT_KEY);
472 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
474 return nft_set_ext(ext, NFT_SET_EXT_DATA);
477 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
479 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
482 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
484 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
487 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext *ext)
489 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
492 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
494 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
497 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
499 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
502 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
504 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
505 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext));
508 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
511 return elem + set->ops->elemsize;
514 void *nft_set_elem_init(const struct nft_set *set,
515 const struct nft_set_ext_tmpl *tmpl,
516 const u32 *key, const u32 *data,
517 u64 timeout, gfp_t gfp);
518 void nft_set_elem_destroy(const struct nft_set *set, void *elem);
521 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
524 * @set: set the elements belong to
525 * @cnt: count of elements
527 struct nft_set_gc_batch_head {
529 const struct nft_set *set;
533 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
534 sizeof(struct nft_set_gc_batch_head)) / \
538 * struct nft_set_gc_batch - nf_tables set garbage collection batch
540 * @head: GC batch head
541 * @elems: garbage collection elements
543 struct nft_set_gc_batch {
544 struct nft_set_gc_batch_head head;
545 void *elems[NFT_SET_GC_BATCH_SIZE];
548 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
550 void nft_set_gc_batch_release(struct rcu_head *rcu);
552 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
555 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
558 static inline struct nft_set_gc_batch *
559 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
563 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
565 nft_set_gc_batch_complete(gcb);
567 return nft_set_gc_batch_alloc(set, gfp);
570 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
573 gcb->elems[gcb->head.cnt++] = elem;
577 * struct nft_expr_type - nf_tables expression type
579 * @select_ops: function to select nft_expr_ops
580 * @ops: default ops, used when no select_ops functions is present
581 * @list: used internally
583 * @owner: module reference
584 * @policy: netlink attribute policy
585 * @maxattr: highest netlink attribute number
586 * @family: address family for AF-specific types
587 * @flags: expression type flags
589 struct nft_expr_type {
590 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
591 const struct nlattr * const tb[]);
592 const struct nft_expr_ops *ops;
593 struct list_head list;
595 struct module *owner;
596 const struct nla_policy *policy;
597 unsigned int maxattr;
602 #define NFT_EXPR_STATEFUL 0x1
605 * struct nft_expr_ops - nf_tables expression operations
607 * @eval: Expression evaluation function
608 * @size: full expression size, including private data size
609 * @init: initialization function
610 * @destroy: destruction function
611 * @dump: function to dump parameters
612 * @type: expression type
613 * @validate: validate expression, called during loop detection
614 * @data: extra data to attach to this expression operation
617 struct nft_expr_ops {
618 void (*eval)(const struct nft_expr *expr,
619 struct nft_regs *regs,
620 const struct nft_pktinfo *pkt);
623 int (*init)(const struct nft_ctx *ctx,
624 const struct nft_expr *expr,
625 const struct nlattr * const tb[]);
626 void (*destroy)(const struct nft_ctx *ctx,
627 const struct nft_expr *expr);
628 int (*dump)(struct sk_buff *skb,
629 const struct nft_expr *expr);
630 int (*validate)(const struct nft_ctx *ctx,
631 const struct nft_expr *expr,
632 const struct nft_data **data);
633 const struct nft_expr_type *type;
637 #define NFT_EXPR_MAXATTR 16
638 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
639 ALIGN(size, __alignof__(struct nft_expr)))
642 * struct nft_expr - nf_tables expression
644 * @ops: expression ops
645 * @data: expression private data
648 const struct nft_expr_ops *ops;
649 unsigned char data[];
652 static inline void *nft_expr_priv(const struct nft_expr *expr)
654 return (void *)expr->data;
657 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
658 const struct nlattr *nla);
659 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
660 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
661 const struct nft_expr *expr);
663 static inline void nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
665 __module_get(src->ops->type->owner);
666 memcpy(dst, src, src->ops->size);
670 * struct nft_rule - nf_tables rule
672 * @list: used internally
673 * @handle: rule handle
674 * @genmask: generation mask
675 * @dlen: length of expression data
676 * @udata: user data is appended to the rule
677 * @data: expression data
680 struct list_head list;
686 __attribute__((aligned(__alignof__(struct nft_expr))));
689 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
691 return (struct nft_expr *)&rule->data[0];
694 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
696 return ((void *)expr) + expr->ops->size;
699 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
701 return (struct nft_expr *)&rule->data[rule->dlen];
704 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
706 return (void *)&rule->data[rule->dlen];
710 * The last pointer isn't really necessary, but the compiler isn't able to
711 * determine that the result of nft_expr_last() is always the same since it
712 * can't assume that the dlen value wasn't changed within calls in the loop.
714 #define nft_rule_for_each_expr(expr, last, rule) \
715 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
717 (expr) = nft_expr_next(expr))
719 enum nft_chain_flags {
720 NFT_BASE_CHAIN = 0x1,
721 NFT_CHAIN_INACTIVE = 0x2,
725 * struct nft_chain - nf_tables chain
727 * @rules: list of rules in the chain
728 * @list: used internally
729 * @table: table that this chain belongs to
730 * @handle: chain handle
731 * @use: number of jump references to this chain
732 * @level: length of longest path to this chain
733 * @flags: bitmask of enum nft_chain_flags
734 * @name: name of the chain
737 struct list_head rules;
738 struct list_head list;
739 struct nft_table *table;
744 char name[NFT_CHAIN_MAXNAMELEN];
747 enum nft_chain_type {
748 NFT_CHAIN_T_DEFAULT = 0,
755 * struct nf_chain_type - nf_tables chain type info
757 * @name: name of the type
758 * @type: numeric identifier
759 * @family: address family
760 * @owner: module owner
761 * @hook_mask: mask of valid hooks
762 * @hooks: hookfn overrides
764 struct nf_chain_type {
766 enum nft_chain_type type;
768 struct module *owner;
769 unsigned int hook_mask;
770 nf_hookfn *hooks[NF_MAX_HOOKS];
773 int nft_chain_validate_dependency(const struct nft_chain *chain,
774 enum nft_chain_type type);
775 int nft_chain_validate_hooks(const struct nft_chain *chain,
776 unsigned int hook_flags);
781 struct u64_stats_sync syncp;
784 #define NFT_HOOK_OPS_MAX 2
785 #define NFT_BASECHAIN_DISABLED (1 << 0)
788 * struct nft_base_chain - nf_tables base chain
790 * @ops: netfilter hook ops
791 * @pnet: net namespace that this chain belongs to
793 * @policy: default policy
794 * @stats: per-cpu chain stats
796 * @dev_name: device name that this base chain is attached to (if any)
798 struct nft_base_chain {
799 struct nf_hook_ops ops[NFT_HOOK_OPS_MAX];
801 const struct nf_chain_type *type;
804 struct nft_stats __percpu *stats;
805 struct nft_chain chain;
806 char dev_name[IFNAMSIZ];
809 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
811 return container_of(chain, struct nft_base_chain, chain);
814 int nft_register_basechain(struct nft_base_chain *basechain,
815 unsigned int hook_nops);
816 void nft_unregister_basechain(struct nft_base_chain *basechain,
817 unsigned int hook_nops);
819 unsigned int nft_do_chain(struct nft_pktinfo *pkt,
820 const struct nf_hook_ops *ops);
823 * struct nft_table - nf_tables table
825 * @list: used internally
826 * @chains: chains in the table
827 * @sets: sets in the table
828 * @hgenerator: handle generator state
829 * @use: number of chain references to this table
830 * @flags: table flag (see enum nft_table_flags)
831 * @name: name of the table
834 struct list_head list;
835 struct list_head chains;
836 struct list_head sets;
840 char name[NFT_TABLE_MAXNAMELEN];
844 NFT_AF_NEEDS_DEV = (1 << 0),
848 * struct nft_af_info - nf_tables address family info
850 * @list: used internally
851 * @family: address family
852 * @nhooks: number of hooks in this family
853 * @owner: module owner
854 * @tables: used internally
855 * @flags: family flags
856 * @nops: number of hook ops in this family
857 * @hook_ops_init: initialization function for chain hook ops
858 * @hooks: hookfn overrides for packet validation
861 struct list_head list;
864 struct module *owner;
865 struct list_head tables;
868 void (*hook_ops_init)(struct nf_hook_ops *,
870 nf_hookfn *hooks[NF_MAX_HOOKS];
873 int nft_register_afinfo(struct net *, struct nft_af_info *);
874 void nft_unregister_afinfo(struct nft_af_info *);
876 int nft_register_chain_type(const struct nf_chain_type *);
877 void nft_unregister_chain_type(const struct nf_chain_type *);
879 int nft_register_expr(struct nft_expr_type *);
880 void nft_unregister_expr(struct nft_expr_type *);
882 #define nft_dereference(p) \
883 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
885 #define MODULE_ALIAS_NFT_FAMILY(family) \
886 MODULE_ALIAS("nft-afinfo-" __stringify(family))
888 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
889 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
891 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
892 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
894 #define MODULE_ALIAS_NFT_EXPR(name) \
895 MODULE_ALIAS("nft-expr-" name)
897 #define MODULE_ALIAS_NFT_SET() \
898 MODULE_ALIAS("nft-set")
901 * The gencursor defines two generations, the currently active and the
902 * next one. Objects contain a bitmask of 2 bits specifying the generations
903 * they're active in. A set bit means they're inactive in the generation
904 * represented by that bit.
906 * New objects start out as inactive in the current and active in the
907 * next generation. When committing the ruleset the bitmask is cleared,
908 * meaning they're active in all generations. When removing an object,
909 * it is set inactive in the next generation. After committing the ruleset,
910 * the objects are removed.
912 static inline unsigned int nft_gencursor_next(const struct net *net)
914 return net->nft.gencursor + 1 == 1 ? 1 : 0;
917 static inline u8 nft_genmask_next(const struct net *net)
919 return 1 << nft_gencursor_next(net);
922 static inline u8 nft_genmask_cur(const struct net *net)
924 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
925 return 1 << ACCESS_ONCE(net->nft.gencursor);
928 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
931 * Set element transaction helpers
934 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
937 return !(ext->genmask & genmask);
940 static inline void nft_set_elem_change_active(const struct nft_set *set,
941 struct nft_set_ext *ext)
943 ext->genmask ^= nft_genmask_next(read_pnet(&set->pnet));
947 * We use a free bit in the genmask field to indicate the element
948 * is busy, meaning it is currently being processed either by
949 * the netlink API or GC.
951 * Even though the genmask is only a single byte wide, this works
952 * because the extension structure if fully constant once initialized,
953 * so there are no non-atomic write accesses unless it is already
956 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
958 #if defined(__LITTLE_ENDIAN_BITFIELD)
959 #define NFT_SET_ELEM_BUSY_BIT 2
960 #elif defined(__BIG_ENDIAN_BITFIELD)
961 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
966 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
968 unsigned long *word = (unsigned long *)ext;
970 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
971 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
974 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
976 unsigned long *word = (unsigned long *)ext;
978 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
982 * struct nft_trans - nf_tables object update in transaction
984 * @list: used internally
985 * @msg_type: message type
986 * @ctx: transaction context
987 * @data: internal information related to the transaction
990 struct list_head list;
996 struct nft_trans_rule {
997 struct nft_rule *rule;
1000 #define nft_trans_rule(trans) \
1001 (((struct nft_trans_rule *)trans->data)->rule)
1003 struct nft_trans_set {
1004 struct nft_set *set;
1008 #define nft_trans_set(trans) \
1009 (((struct nft_trans_set *)trans->data)->set)
1010 #define nft_trans_set_id(trans) \
1011 (((struct nft_trans_set *)trans->data)->set_id)
1013 struct nft_trans_chain {
1015 char name[NFT_CHAIN_MAXNAMELEN];
1016 struct nft_stats __percpu *stats;
1020 #define nft_trans_chain_update(trans) \
1021 (((struct nft_trans_chain *)trans->data)->update)
1022 #define nft_trans_chain_name(trans) \
1023 (((struct nft_trans_chain *)trans->data)->name)
1024 #define nft_trans_chain_stats(trans) \
1025 (((struct nft_trans_chain *)trans->data)->stats)
1026 #define nft_trans_chain_policy(trans) \
1027 (((struct nft_trans_chain *)trans->data)->policy)
1029 struct nft_trans_table {
1034 #define nft_trans_table_update(trans) \
1035 (((struct nft_trans_table *)trans->data)->update)
1036 #define nft_trans_table_enable(trans) \
1037 (((struct nft_trans_table *)trans->data)->enable)
1039 struct nft_trans_elem {
1040 struct nft_set *set;
1041 struct nft_set_elem elem;
1044 #define nft_trans_elem_set(trans) \
1045 (((struct nft_trans_elem *)trans->data)->set)
1046 #define nft_trans_elem(trans) \
1047 (((struct nft_trans_elem *)trans->data)->elem)
1049 #endif /* _NET_NF_TABLES_H */