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
17 const struct net_device
*in
;
18 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
->in
= pkt
->xt
.in
= state
->in
;
34 pkt
->out
= pkt
->xt
.out
= state
->out
;
35 pkt
->hook
= pkt
->xt
.hooknum
= state
->hook
;
36 pkt
->pf
= pkt
->xt
.family
= state
->pf
;
40 * struct nft_verdict - nf_tables verdict
42 * @code: nf_tables/netfilter verdict code
43 * @chain: destination chain for NFT_JUMP/NFT_GOTO
47 struct nft_chain
*chain
;
53 struct nft_verdict verdict
;
55 } __attribute__((aligned(__alignof__(u64
))));
58 * struct nft_regs - nf_tables register set
60 * @data: data registers
61 * @verdict: verdict register
63 * The first four data registers alias to the verdict register.
68 struct nft_verdict verdict
;
72 static inline void nft_data_copy(u32
*dst
, const struct nft_data
*src
,
75 memcpy(dst
, src
, len
);
78 static inline void nft_data_debug(const struct nft_data
*data
)
80 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
81 data
->data
[0], data
->data
[1],
82 data
->data
[2], data
->data
[3]);
86 * struct nft_ctx - nf_tables rule/set context
89 * @afi: address family info
90 * @table: the table the chain is contained in
91 * @chain: the chain the rule is contained in
92 * @nla: netlink attributes
93 * @portid: netlink portID of the original message
94 * @seq: netlink sequence number
95 * @report: notify via unicast netlink message
99 struct nft_af_info
*afi
;
100 struct nft_table
*table
;
101 struct nft_chain
*chain
;
102 const struct nlattr
* const *nla
;
108 struct nft_data_desc
{
109 enum nft_data_types type
;
113 int nft_data_init(const struct nft_ctx
*ctx
,
114 struct nft_data
*data
, unsigned int size
,
115 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
116 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
);
117 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
118 enum nft_data_types type
, unsigned int len
);
120 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
122 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
125 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
127 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
130 unsigned int nft_parse_register(const struct nlattr
*attr
);
131 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
133 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
);
134 int nft_validate_register_store(const struct nft_ctx
*ctx
,
135 enum nft_registers reg
,
136 const struct nft_data
*data
,
137 enum nft_data_types type
, unsigned int len
);
140 * struct nft_userdata - user defined data associated with an object
142 * @len: length of the data
145 * The presence of user data is indicated in an object specific fashion,
146 * so a length of zero can't occur and the value "len" indicates data
149 struct nft_userdata
{
151 unsigned char data
[0];
155 * struct nft_set_elem - generic representation of set elements
158 * @priv: element private data and extensions
160 struct nft_set_elem
{
162 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
169 struct nft_set_iter
{
173 int (*fn
)(const struct nft_ctx
*ctx
,
174 const struct nft_set
*set
,
175 const struct nft_set_iter
*iter
,
176 const struct nft_set_elem
*elem
);
180 * struct nft_set_desc - description of set elements
184 * @size: number of set elements
186 struct nft_set_desc
{
193 * enum nft_set_class - performance class
195 * @NFT_LOOKUP_O_1: constant, O(1)
196 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
197 * @NFT_LOOKUP_O_N: linear, O(N)
201 NFT_SET_CLASS_O_LOG_N
,
206 * struct nft_set_estimate - estimation of memory and performance
209 * @size: required memory
210 * @class: lookup performance class
212 struct nft_set_estimate
{
214 enum nft_set_class
class;
221 * struct nft_set_ops - nf_tables set operations
223 * @lookup: look up an element within the set
224 * @insert: insert new element into set
225 * @activate: activate new element in the next generation
226 * @deactivate: deactivate element in the next generation
227 * @remove: remove element from set
228 * @walk: iterate over all set elemeennts
229 * @privsize: function to return size of set private data
230 * @init: initialize private data of new set instance
231 * @destroy: destroy private data of set instance
232 * @list: nf_tables_set_ops list node
233 * @owner: module reference
234 * @elemsize: element private size
235 * @features: features supported by the implementation
238 bool (*lookup
)(const struct nft_set
*set
,
240 const struct nft_set_ext
**ext
);
241 bool (*update
)(struct nft_set
*set
,
243 void *(*new)(struct nft_set
*,
244 const struct nft_expr
*,
246 const struct nft_expr
*expr
,
247 struct nft_regs
*regs
,
248 const struct nft_set_ext
**ext
);
250 int (*insert
)(const struct nft_set
*set
,
251 const struct nft_set_elem
*elem
);
252 void (*activate
)(const struct nft_set
*set
,
253 const struct nft_set_elem
*elem
);
254 void * (*deactivate
)(const struct nft_set
*set
,
255 const struct nft_set_elem
*elem
);
256 void (*remove
)(const struct nft_set
*set
,
257 const struct nft_set_elem
*elem
);
258 void (*walk
)(const struct nft_ctx
*ctx
,
259 const struct nft_set
*set
,
260 struct nft_set_iter
*iter
);
262 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
263 bool (*estimate
)(const struct nft_set_desc
*desc
,
265 struct nft_set_estimate
*est
);
266 int (*init
)(const struct nft_set
*set
,
267 const struct nft_set_desc
*desc
,
268 const struct nlattr
* const nla
[]);
269 void (*destroy
)(const struct nft_set
*set
);
271 struct list_head list
;
272 struct module
*owner
;
273 unsigned int elemsize
;
277 int nft_register_set(struct nft_set_ops
*ops
);
278 void nft_unregister_set(struct nft_set_ops
*ops
);
281 * struct nft_set - nf_tables set instance
283 * @list: table set list node
284 * @bindings: list of set bindings
285 * @name: name of the set
286 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
287 * @dtype: data type (verdict or numeric type defined by userspace)
288 * @size: maximum set size
289 * @nelems: number of elements
290 * @ndeact: number of deactivated elements queued for removal
291 * @timeout: default timeout value in msecs
292 * @gc_int: garbage collection interval in msecs
293 * @policy: set parameterization (see enum nft_set_policies)
295 * @pnet: network namespace
299 * @data: private set data
302 struct list_head list
;
303 struct list_head bindings
;
313 /* runtime data below here */
314 const struct nft_set_ops
*ops ____cacheline_aligned
;
320 __attribute__((aligned(__alignof__(u64
))));
323 static inline void *nft_set_priv(const struct nft_set
*set
)
325 return (void *)set
->data
;
328 static inline struct nft_set
*nft_set_container_of(const void *priv
)
330 return (void *)priv
- offsetof(struct nft_set
, data
);
333 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
334 const struct nlattr
*nla
);
335 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
336 const struct nlattr
*nla
);
338 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
340 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
344 * struct nft_set_binding - nf_tables set binding
346 * @list: set bindings list node
347 * @chain: chain containing the rule bound to the set
348 * @flags: set action flags
350 * A set binding contains all information necessary for validation
351 * of new elements added to a bound set.
353 struct nft_set_binding
{
354 struct list_head list
;
355 const struct nft_chain
*chain
;
359 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
360 struct nft_set_binding
*binding
);
361 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
362 struct nft_set_binding
*binding
);
365 * enum nft_set_extensions - set extension type IDs
367 * @NFT_SET_EXT_KEY: element key
368 * @NFT_SET_EXT_DATA: mapping data
369 * @NFT_SET_EXT_FLAGS: element flags
370 * @NFT_SET_EXT_TIMEOUT: element timeout
371 * @NFT_SET_EXT_EXPIRATION: element expiration time
372 * @NFT_SET_EXT_USERDATA: user data associated with the element
373 * @NFT_SET_EXT_EXPR: expression assiociated with the element
374 * @NFT_SET_EXT_NUM: number of extension types
376 enum nft_set_extensions
{
381 NFT_SET_EXT_EXPIRATION
,
382 NFT_SET_EXT_USERDATA
,
388 * struct nft_set_ext_type - set extension type
390 * @len: fixed part length of the extension
391 * @align: alignment requirements of the extension
393 struct nft_set_ext_type
{
398 extern const struct nft_set_ext_type nft_set_ext_types
[];
401 * struct nft_set_ext_tmpl - set extension template
403 * @len: length of extension area
404 * @offset: offsets of individual extension types
406 struct nft_set_ext_tmpl
{
408 u8 offset
[NFT_SET_EXT_NUM
];
412 * struct nft_set_ext - set extensions
414 * @genmask: generation mask
415 * @offset: offsets of individual extension types
416 * @data: beginning of extension data
420 u8 offset
[NFT_SET_EXT_NUM
];
424 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
426 memset(tmpl
, 0, sizeof(*tmpl
));
427 tmpl
->len
= sizeof(struct nft_set_ext
);
430 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
433 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
434 BUG_ON(tmpl
->len
> U8_MAX
);
435 tmpl
->offset
[id
] = tmpl
->len
;
436 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
439 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
441 nft_set_ext_add_length(tmpl
, id
, 0);
444 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
445 const struct nft_set_ext_tmpl
*tmpl
)
447 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
450 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
452 return !!ext
->offset
[id
];
455 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
457 return ext
&& __nft_set_ext_exists(ext
, id
);
460 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
462 return (void *)ext
+ ext
->offset
[id
];
465 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
467 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
470 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
472 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
475 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
477 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
480 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
482 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
485 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
487 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
490 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
492 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
495 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
497 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
500 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
502 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
503 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
506 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
509 return elem
+ set
->ops
->elemsize
;
512 void *nft_set_elem_init(const struct nft_set
*set
,
513 const struct nft_set_ext_tmpl
*tmpl
,
514 const u32
*key
, const u32
*data
,
515 u64 timeout
, gfp_t gfp
);
516 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
);
519 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
522 * @set: set the elements belong to
523 * @cnt: count of elements
525 struct nft_set_gc_batch_head
{
527 const struct nft_set
*set
;
531 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
532 sizeof(struct nft_set_gc_batch_head)) / \
536 * struct nft_set_gc_batch - nf_tables set garbage collection batch
538 * @head: GC batch head
539 * @elems: garbage collection elements
541 struct nft_set_gc_batch
{
542 struct nft_set_gc_batch_head head
;
543 void *elems
[NFT_SET_GC_BATCH_SIZE
];
546 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
548 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
550 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
553 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
556 static inline struct nft_set_gc_batch
*
557 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
561 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
563 nft_set_gc_batch_complete(gcb
);
565 return nft_set_gc_batch_alloc(set
, gfp
);
568 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
571 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
575 * struct nft_expr_type - nf_tables expression type
577 * @select_ops: function to select nft_expr_ops
578 * @ops: default ops, used when no select_ops functions is present
579 * @list: used internally
581 * @owner: module reference
582 * @policy: netlink attribute policy
583 * @maxattr: highest netlink attribute number
584 * @family: address family for AF-specific types
585 * @flags: expression type flags
587 struct nft_expr_type
{
588 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
589 const struct nlattr
* const tb
[]);
590 const struct nft_expr_ops
*ops
;
591 struct list_head list
;
593 struct module
*owner
;
594 const struct nla_policy
*policy
;
595 unsigned int maxattr
;
600 #define NFT_EXPR_STATEFUL 0x1
603 * struct nft_expr_ops - nf_tables expression operations
605 * @eval: Expression evaluation function
606 * @size: full expression size, including private data size
607 * @init: initialization function
608 * @destroy: destruction function
609 * @dump: function to dump parameters
610 * @type: expression type
611 * @validate: validate expression, called during loop detection
612 * @data: extra data to attach to this expression operation
615 struct nft_expr_ops
{
616 void (*eval
)(const struct nft_expr
*expr
,
617 struct nft_regs
*regs
,
618 const struct nft_pktinfo
*pkt
);
621 int (*init
)(const struct nft_ctx
*ctx
,
622 const struct nft_expr
*expr
,
623 const struct nlattr
* const tb
[]);
624 void (*destroy
)(const struct nft_ctx
*ctx
,
625 const struct nft_expr
*expr
);
626 int (*dump
)(struct sk_buff
*skb
,
627 const struct nft_expr
*expr
);
628 int (*validate
)(const struct nft_ctx
*ctx
,
629 const struct nft_expr
*expr
,
630 const struct nft_data
**data
);
631 const struct nft_expr_type
*type
;
635 #define NFT_EXPR_MAXATTR 16
636 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
637 ALIGN(size, __alignof__(struct nft_expr)))
640 * struct nft_expr - nf_tables expression
642 * @ops: expression ops
643 * @data: expression private data
646 const struct nft_expr_ops
*ops
;
647 unsigned char data
[];
650 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
652 return (void *)expr
->data
;
655 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
656 const struct nlattr
*nla
);
657 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
658 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
659 const struct nft_expr
*expr
);
661 static inline void nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
663 __module_get(src
->ops
->type
->owner
);
664 memcpy(dst
, src
, src
->ops
->size
);
668 * struct nft_rule - nf_tables rule
670 * @list: used internally
671 * @handle: rule handle
672 * @genmask: generation mask
673 * @dlen: length of expression data
674 * @udata: user data is appended to the rule
675 * @data: expression data
678 struct list_head list
;
684 __attribute__((aligned(__alignof__(struct nft_expr
))));
687 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
689 return (struct nft_expr
*)&rule
->data
[0];
692 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
694 return ((void *)expr
) + expr
->ops
->size
;
697 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
699 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
702 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
704 return (void *)&rule
->data
[rule
->dlen
];
708 * The last pointer isn't really necessary, but the compiler isn't able to
709 * determine that the result of nft_expr_last() is always the same since it
710 * can't assume that the dlen value wasn't changed within calls in the loop.
712 #define nft_rule_for_each_expr(expr, last, rule) \
713 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
715 (expr) = nft_expr_next(expr))
717 enum nft_chain_flags
{
718 NFT_BASE_CHAIN
= 0x1,
719 NFT_CHAIN_INACTIVE
= 0x2,
723 * struct nft_chain - nf_tables chain
725 * @rules: list of rules in the chain
726 * @list: used internally
727 * @table: table that this chain belongs to
728 * @handle: chain handle
729 * @use: number of jump references to this chain
730 * @level: length of longest path to this chain
731 * @flags: bitmask of enum nft_chain_flags
732 * @name: name of the chain
735 struct list_head rules
;
736 struct list_head list
;
737 struct nft_table
*table
;
742 char name
[NFT_CHAIN_MAXNAMELEN
];
745 enum nft_chain_type
{
746 NFT_CHAIN_T_DEFAULT
= 0,
753 * struct nf_chain_type - nf_tables chain type info
755 * @name: name of the type
756 * @type: numeric identifier
757 * @family: address family
758 * @owner: module owner
759 * @hook_mask: mask of valid hooks
760 * @hooks: hookfn overrides
762 struct nf_chain_type
{
764 enum nft_chain_type type
;
766 struct module
*owner
;
767 unsigned int hook_mask
;
768 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
771 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
772 enum nft_chain_type type
);
773 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
774 unsigned int hook_flags
);
779 struct u64_stats_sync syncp
;
782 #define NFT_HOOK_OPS_MAX 2
783 #define NFT_BASECHAIN_DISABLED (1 << 0)
786 * struct nft_base_chain - nf_tables base chain
788 * @ops: netfilter hook ops
789 * @pnet: net namespace that this chain belongs to
791 * @policy: default policy
792 * @stats: per-cpu chain stats
794 * @dev_name: device name that this base chain is attached to (if any)
796 struct nft_base_chain
{
797 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
799 const struct nf_chain_type
*type
;
802 struct nft_stats __percpu
*stats
;
803 struct nft_chain chain
;
804 char dev_name
[IFNAMSIZ
];
807 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
809 return container_of(chain
, struct nft_base_chain
, chain
);
812 int nft_register_basechain(struct nft_base_chain
*basechain
,
813 unsigned int hook_nops
);
814 void nft_unregister_basechain(struct nft_base_chain
*basechain
,
815 unsigned int hook_nops
);
817 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
,
818 const struct nf_hook_ops
*ops
);
821 * struct nft_table - nf_tables table
823 * @list: used internally
824 * @chains: chains in the table
825 * @sets: sets in the table
826 * @hgenerator: handle generator state
827 * @use: number of chain references to this table
828 * @flags: table flag (see enum nft_table_flags)
829 * @name: name of the table
832 struct list_head list
;
833 struct list_head chains
;
834 struct list_head sets
;
838 char name
[NFT_TABLE_MAXNAMELEN
];
842 NFT_AF_NEEDS_DEV
= (1 << 0),
846 * struct nft_af_info - nf_tables address family info
848 * @list: used internally
849 * @family: address family
850 * @nhooks: number of hooks in this family
851 * @owner: module owner
852 * @tables: used internally
853 * @flags: family flags
854 * @nops: number of hook ops in this family
855 * @hook_ops_init: initialization function for chain hook ops
856 * @hooks: hookfn overrides for packet validation
859 struct list_head list
;
862 struct module
*owner
;
863 struct list_head tables
;
866 void (*hook_ops_init
)(struct nf_hook_ops
*,
868 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
871 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
872 void nft_unregister_afinfo(struct nft_af_info
*);
874 int nft_register_chain_type(const struct nf_chain_type
*);
875 void nft_unregister_chain_type(const struct nf_chain_type
*);
877 int nft_register_expr(struct nft_expr_type
*);
878 void nft_unregister_expr(struct nft_expr_type
*);
880 #define nft_dereference(p) \
881 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
883 #define MODULE_ALIAS_NFT_FAMILY(family) \
884 MODULE_ALIAS("nft-afinfo-" __stringify(family))
886 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
887 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
889 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
890 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
892 #define MODULE_ALIAS_NFT_EXPR(name) \
893 MODULE_ALIAS("nft-expr-" name)
895 #define MODULE_ALIAS_NFT_SET() \
896 MODULE_ALIAS("nft-set")
899 * The gencursor defines two generations, the currently active and the
900 * next one. Objects contain a bitmask of 2 bits specifying the generations
901 * they're active in. A set bit means they're inactive in the generation
902 * represented by that bit.
904 * New objects start out as inactive in the current and active in the
905 * next generation. When committing the ruleset the bitmask is cleared,
906 * meaning they're active in all generations. When removing an object,
907 * it is set inactive in the next generation. After committing the ruleset,
908 * the objects are removed.
910 static inline unsigned int nft_gencursor_next(const struct net
*net
)
912 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
915 static inline u8
nft_genmask_next(const struct net
*net
)
917 return 1 << nft_gencursor_next(net
);
920 static inline u8
nft_genmask_cur(const struct net
*net
)
922 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
923 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
926 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
929 * Set element transaction helpers
932 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
935 return !(ext
->genmask
& genmask
);
938 static inline void nft_set_elem_change_active(const struct nft_set
*set
,
939 struct nft_set_ext
*ext
)
941 ext
->genmask
^= nft_genmask_next(read_pnet(&set
->pnet
));
945 * We use a free bit in the genmask field to indicate the element
946 * is busy, meaning it is currently being processed either by
947 * the netlink API or GC.
949 * Even though the genmask is only a single byte wide, this works
950 * because the extension structure if fully constant once initialized,
951 * so there are no non-atomic write accesses unless it is already
954 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
956 #if defined(__LITTLE_ENDIAN_BITFIELD)
957 #define NFT_SET_ELEM_BUSY_BIT 2
958 #elif defined(__BIG_ENDIAN_BITFIELD)
959 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
964 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
966 unsigned long *word
= (unsigned long *)ext
;
968 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
969 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
972 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
974 unsigned long *word
= (unsigned long *)ext
;
976 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
980 * struct nft_trans - nf_tables object update in transaction
982 * @list: used internally
983 * @msg_type: message type
984 * @ctx: transaction context
985 * @data: internal information related to the transaction
988 struct list_head list
;
994 struct nft_trans_rule
{
995 struct nft_rule
*rule
;
998 #define nft_trans_rule(trans) \
999 (((struct nft_trans_rule *)trans->data)->rule)
1001 struct nft_trans_set
{
1002 struct nft_set
*set
;
1006 #define nft_trans_set(trans) \
1007 (((struct nft_trans_set *)trans->data)->set)
1008 #define nft_trans_set_id(trans) \
1009 (((struct nft_trans_set *)trans->data)->set_id)
1011 struct nft_trans_chain
{
1013 char name
[NFT_CHAIN_MAXNAMELEN
];
1014 struct nft_stats __percpu
*stats
;
1018 #define nft_trans_chain_update(trans) \
1019 (((struct nft_trans_chain *)trans->data)->update)
1020 #define nft_trans_chain_name(trans) \
1021 (((struct nft_trans_chain *)trans->data)->name)
1022 #define nft_trans_chain_stats(trans) \
1023 (((struct nft_trans_chain *)trans->data)->stats)
1024 #define nft_trans_chain_policy(trans) \
1025 (((struct nft_trans_chain *)trans->data)->policy)
1027 struct nft_trans_table
{
1032 #define nft_trans_table_update(trans) \
1033 (((struct nft_trans_table *)trans->data)->update)
1034 #define nft_trans_table_enable(trans) \
1035 (((struct nft_trans_table *)trans->data)->enable)
1037 struct nft_trans_elem
{
1038 struct nft_set
*set
;
1039 struct nft_set_elem elem
;
1042 #define nft_trans_elem_set(trans) \
1043 (((struct nft_trans_elem *)trans->data)->set)
1044 #define nft_trans_elem(trans) \
1045 (((struct nft_trans_elem *)trans->data)->elem)
1047 #endif /* _NET_NF_TABLES_H */