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
;
19 const struct nf_hook_ops
*ops
;
23 /* for x_tables compatibility */
24 struct xt_action_param xt
;
27 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
28 const struct nf_hook_ops
*ops
,
30 const struct nf_hook_state
*state
)
33 pkt
->in
= pkt
->xt
.in
= state
->in
;
34 pkt
->out
= pkt
->xt
.out
= state
->out
;
36 pkt
->xt
.hooknum
= ops
->hooknum
;
37 pkt
->xt
.family
= ops
->pf
;
41 * struct nft_verdict - nf_tables verdict
43 * @code: nf_tables/netfilter verdict code
44 * @chain: destination chain for NFT_JUMP/NFT_GOTO
48 struct nft_chain
*chain
;
54 struct nft_verdict verdict
;
56 } __attribute__((aligned(__alignof__(u64
))));
59 * struct nft_regs - nf_tables register set
61 * @data: data registers
62 * @verdict: verdict register
64 * The first four data registers alias to the verdict register.
69 struct nft_verdict verdict
;
73 static inline void nft_data_copy(u32
*dst
, const struct nft_data
*src
,
76 memcpy(dst
, src
, len
);
79 static inline void nft_data_debug(const struct nft_data
*data
)
81 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
82 data
->data
[0], data
->data
[1],
83 data
->data
[2], data
->data
[3]);
87 * struct nft_ctx - nf_tables rule/set context
90 * @afi: address family info
91 * @table: the table the chain is contained in
92 * @chain: the chain the rule is contained in
93 * @nla: netlink attributes
94 * @portid: netlink portID of the original message
95 * @seq: netlink sequence number
96 * @report: notify via unicast netlink message
100 struct nft_af_info
*afi
;
101 struct nft_table
*table
;
102 struct nft_chain
*chain
;
103 const struct nlattr
* const *nla
;
109 struct nft_data_desc
{
110 enum nft_data_types type
;
114 int nft_data_init(const struct nft_ctx
*ctx
,
115 struct nft_data
*data
, unsigned int size
,
116 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
117 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
);
118 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
119 enum nft_data_types type
, unsigned int len
);
121 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
123 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
126 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
128 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
131 unsigned int nft_parse_register(const struct nlattr
*attr
);
132 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
134 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
);
135 int nft_validate_register_store(const struct nft_ctx
*ctx
,
136 enum nft_registers reg
,
137 const struct nft_data
*data
,
138 enum nft_data_types type
, unsigned int len
);
141 * struct nft_userdata - user defined data associated with an object
143 * @len: length of the data
146 * The presence of user data is indicated in an object specific fashion,
147 * so a length of zero can't occur and the value "len" indicates data
150 struct nft_userdata
{
152 unsigned char data
[0];
156 * struct nft_set_elem - generic representation of set elements
159 * @priv: element private data and extensions
161 struct nft_set_elem
{
163 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
170 struct nft_set_iter
{
174 int (*fn
)(const struct nft_ctx
*ctx
,
175 const struct nft_set
*set
,
176 const struct nft_set_iter
*iter
,
177 const struct nft_set_elem
*elem
);
181 * struct nft_set_desc - description of set elements
185 * @size: number of set elements
187 struct nft_set_desc
{
194 * enum nft_set_class - performance class
196 * @NFT_LOOKUP_O_1: constant, O(1)
197 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
198 * @NFT_LOOKUP_O_N: linear, O(N)
202 NFT_SET_CLASS_O_LOG_N
,
207 * struct nft_set_estimate - estimation of memory and performance
210 * @size: required memory
211 * @class: lookup performance class
213 struct nft_set_estimate
{
215 enum nft_set_class
class;
222 * struct nft_set_ops - nf_tables set operations
224 * @lookup: look up an element within the set
225 * @insert: insert new element into set
226 * @activate: activate new element in the next generation
227 * @deactivate: deactivate element in the next generation
228 * @remove: remove element from set
229 * @walk: iterate over all set elemeennts
230 * @privsize: function to return size of set private data
231 * @init: initialize private data of new set instance
232 * @destroy: destroy private data of set instance
233 * @list: nf_tables_set_ops list node
234 * @owner: module reference
235 * @elemsize: element private size
236 * @features: features supported by the implementation
239 bool (*lookup
)(const struct nft_set
*set
,
241 const struct nft_set_ext
**ext
);
242 bool (*update
)(struct nft_set
*set
,
244 void *(*new)(struct nft_set
*,
245 const struct nft_expr
*,
247 const struct nft_expr
*expr
,
248 struct nft_regs
*regs
,
249 const struct nft_set_ext
**ext
);
251 int (*insert
)(const struct nft_set
*set
,
252 const struct nft_set_elem
*elem
);
253 void (*activate
)(const struct nft_set
*set
,
254 const struct nft_set_elem
*elem
);
255 void * (*deactivate
)(const struct nft_set
*set
,
256 const struct nft_set_elem
*elem
);
257 void (*remove
)(const struct nft_set
*set
,
258 const struct nft_set_elem
*elem
);
259 void (*walk
)(const struct nft_ctx
*ctx
,
260 const struct nft_set
*set
,
261 struct nft_set_iter
*iter
);
263 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
264 bool (*estimate
)(const struct nft_set_desc
*desc
,
266 struct nft_set_estimate
*est
);
267 int (*init
)(const struct nft_set
*set
,
268 const struct nft_set_desc
*desc
,
269 const struct nlattr
* const nla
[]);
270 void (*destroy
)(const struct nft_set
*set
);
272 struct list_head list
;
273 struct module
*owner
;
274 unsigned int elemsize
;
278 int nft_register_set(struct nft_set_ops
*ops
);
279 void nft_unregister_set(struct nft_set_ops
*ops
);
282 * struct nft_set - nf_tables set instance
284 * @list: table set list node
285 * @bindings: list of set bindings
286 * @name: name of the set
287 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
288 * @dtype: data type (verdict or numeric type defined by userspace)
289 * @size: maximum set size
290 * @nelems: number of elements
291 * @ndeact: number of deactivated elements queued for removal
292 * @timeout: default timeout value in msecs
293 * @gc_int: garbage collection interval in msecs
294 * @policy: set parameterization (see enum nft_set_policies)
296 * @pnet: network namespace
300 * @data: private set data
303 struct list_head list
;
304 struct list_head bindings
;
314 /* runtime data below here */
315 const struct nft_set_ops
*ops ____cacheline_aligned
;
321 __attribute__((aligned(__alignof__(u64
))));
324 static inline void *nft_set_priv(const struct nft_set
*set
)
326 return (void *)set
->data
;
329 static inline struct nft_set
*nft_set_container_of(const void *priv
)
331 return (void *)priv
- offsetof(struct nft_set
, data
);
334 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
335 const struct nlattr
*nla
);
336 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
337 const struct nlattr
*nla
);
339 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
341 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
345 * struct nft_set_binding - nf_tables set binding
347 * @list: set bindings list node
348 * @chain: chain containing the rule bound to the set
349 * @flags: set action flags
351 * A set binding contains all information necessary for validation
352 * of new elements added to a bound set.
354 struct nft_set_binding
{
355 struct list_head list
;
356 const struct nft_chain
*chain
;
360 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
361 struct nft_set_binding
*binding
);
362 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
363 struct nft_set_binding
*binding
);
366 * enum nft_set_extensions - set extension type IDs
368 * @NFT_SET_EXT_KEY: element key
369 * @NFT_SET_EXT_DATA: mapping data
370 * @NFT_SET_EXT_FLAGS: element flags
371 * @NFT_SET_EXT_TIMEOUT: element timeout
372 * @NFT_SET_EXT_EXPIRATION: element expiration time
373 * @NFT_SET_EXT_USERDATA: user data associated with the element
374 * @NFT_SET_EXT_EXPR: expression assiociated with the element
375 * @NFT_SET_EXT_NUM: number of extension types
377 enum nft_set_extensions
{
382 NFT_SET_EXT_EXPIRATION
,
383 NFT_SET_EXT_USERDATA
,
389 * struct nft_set_ext_type - set extension type
391 * @len: fixed part length of the extension
392 * @align: alignment requirements of the extension
394 struct nft_set_ext_type
{
399 extern const struct nft_set_ext_type nft_set_ext_types
[];
402 * struct nft_set_ext_tmpl - set extension template
404 * @len: length of extension area
405 * @offset: offsets of individual extension types
407 struct nft_set_ext_tmpl
{
409 u8 offset
[NFT_SET_EXT_NUM
];
413 * struct nft_set_ext - set extensions
415 * @genmask: generation mask
416 * @offset: offsets of individual extension types
417 * @data: beginning of extension data
421 u8 offset
[NFT_SET_EXT_NUM
];
425 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
427 memset(tmpl
, 0, sizeof(*tmpl
));
428 tmpl
->len
= sizeof(struct nft_set_ext
);
431 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
434 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
435 BUG_ON(tmpl
->len
> U8_MAX
);
436 tmpl
->offset
[id
] = tmpl
->len
;
437 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
440 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
442 nft_set_ext_add_length(tmpl
, id
, 0);
445 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
446 const struct nft_set_ext_tmpl
*tmpl
)
448 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
451 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
453 return !!ext
->offset
[id
];
456 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
458 return ext
&& __nft_set_ext_exists(ext
, id
);
461 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
463 return (void *)ext
+ ext
->offset
[id
];
466 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
468 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
471 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
473 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
476 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
478 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
481 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
483 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
486 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
488 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
491 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
493 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
496 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
498 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
501 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
503 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
504 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
507 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
510 return elem
+ set
->ops
->elemsize
;
513 void *nft_set_elem_init(const struct nft_set
*set
,
514 const struct nft_set_ext_tmpl
*tmpl
,
515 const u32
*key
, const u32
*data
,
516 u64 timeout
, gfp_t gfp
);
517 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
);
520 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
523 * @set: set the elements belong to
524 * @cnt: count of elements
526 struct nft_set_gc_batch_head
{
528 const struct nft_set
*set
;
532 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
533 sizeof(struct nft_set_gc_batch_head)) / \
537 * struct nft_set_gc_batch - nf_tables set garbage collection batch
539 * @head: GC batch head
540 * @elems: garbage collection elements
542 struct nft_set_gc_batch
{
543 struct nft_set_gc_batch_head head
;
544 void *elems
[NFT_SET_GC_BATCH_SIZE
];
547 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
549 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
551 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
554 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
557 static inline struct nft_set_gc_batch
*
558 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
562 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
564 nft_set_gc_batch_complete(gcb
);
566 return nft_set_gc_batch_alloc(set
, gfp
);
569 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
572 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
576 * struct nft_expr_type - nf_tables expression type
578 * @select_ops: function to select nft_expr_ops
579 * @ops: default ops, used when no select_ops functions is present
580 * @list: used internally
582 * @owner: module reference
583 * @policy: netlink attribute policy
584 * @maxattr: highest netlink attribute number
585 * @family: address family for AF-specific types
586 * @flags: expression type flags
588 struct nft_expr_type
{
589 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
590 const struct nlattr
* const tb
[]);
591 const struct nft_expr_ops
*ops
;
592 struct list_head list
;
594 struct module
*owner
;
595 const struct nla_policy
*policy
;
596 unsigned int maxattr
;
601 #define NFT_EXPR_STATEFUL 0x1
604 * struct nft_expr_ops - nf_tables expression operations
606 * @eval: Expression evaluation function
607 * @size: full expression size, including private data size
608 * @init: initialization function
609 * @destroy: destruction function
610 * @dump: function to dump parameters
611 * @type: expression type
612 * @validate: validate expression, called during loop detection
613 * @data: extra data to attach to this expression operation
616 struct nft_expr_ops
{
617 void (*eval
)(const struct nft_expr
*expr
,
618 struct nft_regs
*regs
,
619 const struct nft_pktinfo
*pkt
);
622 int (*init
)(const struct nft_ctx
*ctx
,
623 const struct nft_expr
*expr
,
624 const struct nlattr
* const tb
[]);
625 void (*destroy
)(const struct nft_ctx
*ctx
,
626 const struct nft_expr
*expr
);
627 int (*dump
)(struct sk_buff
*skb
,
628 const struct nft_expr
*expr
);
629 int (*validate
)(const struct nft_ctx
*ctx
,
630 const struct nft_expr
*expr
,
631 const struct nft_data
**data
);
632 const struct nft_expr_type
*type
;
636 #define NFT_EXPR_MAXATTR 16
637 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
638 ALIGN(size, __alignof__(struct nft_expr)))
641 * struct nft_expr - nf_tables expression
643 * @ops: expression ops
644 * @data: expression private data
647 const struct nft_expr_ops
*ops
;
648 unsigned char data
[];
651 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
653 return (void *)expr
->data
;
656 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
657 const struct nlattr
*nla
);
658 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
659 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
660 const struct nft_expr
*expr
);
662 static inline void nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
664 __module_get(src
->ops
->type
->owner
);
665 memcpy(dst
, src
, src
->ops
->size
);
669 * struct nft_rule - nf_tables rule
671 * @list: used internally
672 * @handle: rule handle
673 * @genmask: generation mask
674 * @dlen: length of expression data
675 * @udata: user data is appended to the rule
676 * @data: expression data
679 struct list_head list
;
685 __attribute__((aligned(__alignof__(struct nft_expr
))));
688 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
690 return (struct nft_expr
*)&rule
->data
[0];
693 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
695 return ((void *)expr
) + expr
->ops
->size
;
698 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
700 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
703 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
705 return (void *)&rule
->data
[rule
->dlen
];
709 * The last pointer isn't really necessary, but the compiler isn't able to
710 * determine that the result of nft_expr_last() is always the same since it
711 * can't assume that the dlen value wasn't changed within calls in the loop.
713 #define nft_rule_for_each_expr(expr, last, rule) \
714 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
716 (expr) = nft_expr_next(expr))
718 enum nft_chain_flags
{
719 NFT_BASE_CHAIN
= 0x1,
720 NFT_CHAIN_INACTIVE
= 0x2,
724 * struct nft_chain - nf_tables chain
726 * @rules: list of rules in the chain
727 * @list: used internally
728 * @table: table that this chain belongs to
729 * @handle: chain handle
730 * @use: number of jump references to this chain
731 * @level: length of longest path to this chain
732 * @flags: bitmask of enum nft_chain_flags
733 * @name: name of the chain
736 struct list_head rules
;
737 struct list_head list
;
738 struct nft_table
*table
;
743 char name
[NFT_CHAIN_MAXNAMELEN
];
746 enum nft_chain_type
{
747 NFT_CHAIN_T_DEFAULT
= 0,
754 * struct nf_chain_type - nf_tables chain type info
756 * @name: name of the type
757 * @type: numeric identifier
758 * @family: address family
759 * @owner: module owner
760 * @hook_mask: mask of valid hooks
761 * @hooks: hookfn overrides
763 struct nf_chain_type
{
765 enum nft_chain_type type
;
767 struct module
*owner
;
768 unsigned int hook_mask
;
769 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
772 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
773 enum nft_chain_type type
);
774 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
775 unsigned int hook_flags
);
780 struct u64_stats_sync syncp
;
783 #define NFT_HOOK_OPS_MAX 2
784 #define NFT_BASECHAIN_DISABLED (1 << 0)
787 * struct nft_base_chain - nf_tables base chain
789 * @ops: netfilter hook ops
790 * @pnet: net namespace that this chain belongs to
792 * @policy: default policy
793 * @stats: per-cpu chain stats
795 * @dev_name: device name that this base chain is attached to (if any)
797 struct nft_base_chain
{
798 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
800 const struct nf_chain_type
*type
;
803 struct nft_stats __percpu
*stats
;
804 struct nft_chain chain
;
805 char dev_name
[IFNAMSIZ
];
808 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
810 return container_of(chain
, struct nft_base_chain
, chain
);
813 int nft_register_basechain(struct nft_base_chain
*basechain
,
814 unsigned int hook_nops
);
815 void nft_unregister_basechain(struct nft_base_chain
*basechain
,
816 unsigned int hook_nops
);
818 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
,
819 const struct nf_hook_ops
*ops
);
822 * struct nft_table - nf_tables table
824 * @list: used internally
825 * @chains: chains in the table
826 * @sets: sets in the table
827 * @hgenerator: handle generator state
828 * @use: number of chain references to this table
829 * @flags: table flag (see enum nft_table_flags)
830 * @name: name of the table
833 struct list_head list
;
834 struct list_head chains
;
835 struct list_head sets
;
839 char name
[NFT_TABLE_MAXNAMELEN
];
843 NFT_AF_NEEDS_DEV
= (1 << 0),
847 * struct nft_af_info - nf_tables address family info
849 * @list: used internally
850 * @family: address family
851 * @nhooks: number of hooks in this family
852 * @owner: module owner
853 * @tables: used internally
854 * @flags: family flags
855 * @nops: number of hook ops in this family
856 * @hook_ops_init: initialization function for chain hook ops
857 * @hooks: hookfn overrides for packet validation
860 struct list_head list
;
863 struct module
*owner
;
864 struct list_head tables
;
867 void (*hook_ops_init
)(struct nf_hook_ops
*,
869 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
872 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
873 void nft_unregister_afinfo(struct nft_af_info
*);
875 int nft_register_chain_type(const struct nf_chain_type
*);
876 void nft_unregister_chain_type(const struct nf_chain_type
*);
878 int nft_register_expr(struct nft_expr_type
*);
879 void nft_unregister_expr(struct nft_expr_type
*);
881 #define nft_dereference(p) \
882 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
884 #define MODULE_ALIAS_NFT_FAMILY(family) \
885 MODULE_ALIAS("nft-afinfo-" __stringify(family))
887 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
888 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
890 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
891 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
893 #define MODULE_ALIAS_NFT_EXPR(name) \
894 MODULE_ALIAS("nft-expr-" name)
896 #define MODULE_ALIAS_NFT_SET() \
897 MODULE_ALIAS("nft-set")
900 * The gencursor defines two generations, the currently active and the
901 * next one. Objects contain a bitmask of 2 bits specifying the generations
902 * they're active in. A set bit means they're inactive in the generation
903 * represented by that bit.
905 * New objects start out as inactive in the current and active in the
906 * next generation. When committing the ruleset the bitmask is cleared,
907 * meaning they're active in all generations. When removing an object,
908 * it is set inactive in the next generation. After committing the ruleset,
909 * the objects are removed.
911 static inline unsigned int nft_gencursor_next(const struct net
*net
)
913 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
916 static inline u8
nft_genmask_next(const struct net
*net
)
918 return 1 << nft_gencursor_next(net
);
921 static inline u8
nft_genmask_cur(const struct net
*net
)
923 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
924 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
927 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
930 * Set element transaction helpers
933 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
936 return !(ext
->genmask
& genmask
);
939 static inline void nft_set_elem_change_active(const struct nft_set
*set
,
940 struct nft_set_ext
*ext
)
942 ext
->genmask
^= nft_genmask_next(read_pnet(&set
->pnet
));
946 * We use a free bit in the genmask field to indicate the element
947 * is busy, meaning it is currently being processed either by
948 * the netlink API or GC.
950 * Even though the genmask is only a single byte wide, this works
951 * because the extension structure if fully constant once initialized,
952 * so there are no non-atomic write accesses unless it is already
955 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
957 #if defined(__LITTLE_ENDIAN_BITFIELD)
958 #define NFT_SET_ELEM_BUSY_BIT 2
959 #elif defined(__BIG_ENDIAN_BITFIELD)
960 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
965 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
967 unsigned long *word
= (unsigned long *)ext
;
969 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
970 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
973 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
975 unsigned long *word
= (unsigned long *)ext
;
977 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
981 * struct nft_trans - nf_tables object update in transaction
983 * @list: used internally
984 * @msg_type: message type
985 * @ctx: transaction context
986 * @data: internal information related to the transaction
989 struct list_head list
;
995 struct nft_trans_rule
{
996 struct nft_rule
*rule
;
999 #define nft_trans_rule(trans) \
1000 (((struct nft_trans_rule *)trans->data)->rule)
1002 struct nft_trans_set
{
1003 struct nft_set
*set
;
1007 #define nft_trans_set(trans) \
1008 (((struct nft_trans_set *)trans->data)->set)
1009 #define nft_trans_set_id(trans) \
1010 (((struct nft_trans_set *)trans->data)->set_id)
1012 struct nft_trans_chain
{
1014 char name
[NFT_CHAIN_MAXNAMELEN
];
1015 struct nft_stats __percpu
*stats
;
1019 #define nft_trans_chain_update(trans) \
1020 (((struct nft_trans_chain *)trans->data)->update)
1021 #define nft_trans_chain_name(trans) \
1022 (((struct nft_trans_chain *)trans->data)->name)
1023 #define nft_trans_chain_stats(trans) \
1024 (((struct nft_trans_chain *)trans->data)->stats)
1025 #define nft_trans_chain_policy(trans) \
1026 (((struct nft_trans_chain *)trans->data)->policy)
1028 struct nft_trans_table
{
1033 #define nft_trans_table_update(trans) \
1034 (((struct nft_trans_table *)trans->data)->update)
1035 #define nft_trans_table_enable(trans) \
1036 (((struct nft_trans_table *)trans->data)->enable)
1038 struct nft_trans_elem
{
1039 struct nft_set
*set
;
1040 struct nft_set_elem elem
;
1043 #define nft_trans_elem_set(trans) \
1044 (((struct nft_trans_elem *)trans->data)->set)
1045 #define nft_trans_elem(trans) \
1046 (((struct nft_trans_elem *)trans->data)->elem)
1048 #endif /* _NET_NF_TABLES_H */