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
;
23 /* for x_tables compatibility */
24 struct xt_action_param xt
;
27 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
29 const struct nf_hook_state
*state
)
32 pkt
->net
= pkt
->xt
.net
= state
->net
;
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
{
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 net
*net
,
240 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 net
*net
,
253 const struct nft_set
*set
,
254 const struct nft_set_elem
*elem
);
255 void (*activate
)(const struct net
*net
,
256 const struct nft_set
*set
,
257 const struct nft_set_elem
*elem
);
258 void * (*deactivate
)(const struct net
*net
,
259 const struct nft_set
*set
,
260 const struct nft_set_elem
*elem
);
261 void (*remove
)(const struct nft_set
*set
,
262 const struct nft_set_elem
*elem
);
263 void (*walk
)(const struct nft_ctx
*ctx
,
264 const struct nft_set
*set
,
265 struct nft_set_iter
*iter
);
267 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
268 bool (*estimate
)(const struct nft_set_desc
*desc
,
270 struct nft_set_estimate
*est
);
271 int (*init
)(const struct nft_set
*set
,
272 const struct nft_set_desc
*desc
,
273 const struct nlattr
* const nla
[]);
274 void (*destroy
)(const struct nft_set
*set
);
276 struct list_head list
;
277 struct module
*owner
;
278 unsigned int elemsize
;
282 int nft_register_set(struct nft_set_ops
*ops
);
283 void nft_unregister_set(struct nft_set_ops
*ops
);
286 * struct nft_set - nf_tables set instance
288 * @list: table set list node
289 * @bindings: list of set bindings
290 * @name: name of the set
291 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
292 * @dtype: data type (verdict or numeric type defined by userspace)
293 * @size: maximum set size
294 * @nelems: number of elements
295 * @ndeact: number of deactivated elements queued for removal
296 * @timeout: default timeout value in msecs
297 * @gc_int: garbage collection interval in msecs
298 * @policy: set parameterization (see enum nft_set_policies)
299 * @udlen: user data length
303 * @genmask: generation mask
306 * @data: private set data
309 struct list_head list
;
310 struct list_head bindings
;
311 char name
[NFT_SET_MAXNAMELEN
];
321 unsigned char *udata
;
322 /* runtime data below here */
323 const struct nft_set_ops
*ops ____cacheline_aligned
;
329 __attribute__((aligned(__alignof__(u64
))));
332 static inline void *nft_set_priv(const struct nft_set
*set
)
334 return (void *)set
->data
;
337 static inline struct nft_set
*nft_set_container_of(const void *priv
)
339 return (void *)priv
- offsetof(struct nft_set
, data
);
342 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
343 const struct nlattr
*nla
, u8 genmask
);
344 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
345 const struct nlattr
*nla
, u8 genmask
);
347 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
349 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
353 * struct nft_set_binding - nf_tables set binding
355 * @list: set bindings list node
356 * @chain: chain containing the rule bound to the set
357 * @flags: set action flags
359 * A set binding contains all information necessary for validation
360 * of new elements added to a bound set.
362 struct nft_set_binding
{
363 struct list_head list
;
364 const struct nft_chain
*chain
;
368 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
369 struct nft_set_binding
*binding
);
370 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
371 struct nft_set_binding
*binding
);
374 * enum nft_set_extensions - set extension type IDs
376 * @NFT_SET_EXT_KEY: element key
377 * @NFT_SET_EXT_DATA: mapping data
378 * @NFT_SET_EXT_FLAGS: element flags
379 * @NFT_SET_EXT_TIMEOUT: element timeout
380 * @NFT_SET_EXT_EXPIRATION: element expiration time
381 * @NFT_SET_EXT_USERDATA: user data associated with the element
382 * @NFT_SET_EXT_EXPR: expression assiociated with the element
383 * @NFT_SET_EXT_NUM: number of extension types
385 enum nft_set_extensions
{
390 NFT_SET_EXT_EXPIRATION
,
391 NFT_SET_EXT_USERDATA
,
397 * struct nft_set_ext_type - set extension type
399 * @len: fixed part length of the extension
400 * @align: alignment requirements of the extension
402 struct nft_set_ext_type
{
407 extern const struct nft_set_ext_type nft_set_ext_types
[];
410 * struct nft_set_ext_tmpl - set extension template
412 * @len: length of extension area
413 * @offset: offsets of individual extension types
415 struct nft_set_ext_tmpl
{
417 u8 offset
[NFT_SET_EXT_NUM
];
421 * struct nft_set_ext - set extensions
423 * @genmask: generation mask
424 * @offset: offsets of individual extension types
425 * @data: beginning of extension data
429 u8 offset
[NFT_SET_EXT_NUM
];
433 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
435 memset(tmpl
, 0, sizeof(*tmpl
));
436 tmpl
->len
= sizeof(struct nft_set_ext
);
439 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
442 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
443 BUG_ON(tmpl
->len
> U8_MAX
);
444 tmpl
->offset
[id
] = tmpl
->len
;
445 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
448 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
450 nft_set_ext_add_length(tmpl
, id
, 0);
453 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
454 const struct nft_set_ext_tmpl
*tmpl
)
456 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
459 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
461 return !!ext
->offset
[id
];
464 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
466 return ext
&& __nft_set_ext_exists(ext
, id
);
469 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
471 return (void *)ext
+ ext
->offset
[id
];
474 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
476 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
479 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
481 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
484 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
486 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
489 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
491 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
494 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
496 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
499 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
501 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
504 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
506 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
509 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
511 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
512 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
515 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
518 return elem
+ set
->ops
->elemsize
;
521 void *nft_set_elem_init(const struct nft_set
*set
,
522 const struct nft_set_ext_tmpl
*tmpl
,
523 const u32
*key
, const u32
*data
,
524 u64 timeout
, gfp_t gfp
);
525 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
);
528 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
531 * @set: set the elements belong to
532 * @cnt: count of elements
534 struct nft_set_gc_batch_head
{
536 const struct nft_set
*set
;
540 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
541 sizeof(struct nft_set_gc_batch_head)) / \
545 * struct nft_set_gc_batch - nf_tables set garbage collection batch
547 * @head: GC batch head
548 * @elems: garbage collection elements
550 struct nft_set_gc_batch
{
551 struct nft_set_gc_batch_head head
;
552 void *elems
[NFT_SET_GC_BATCH_SIZE
];
555 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
557 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
559 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
562 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
565 static inline struct nft_set_gc_batch
*
566 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
570 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
572 nft_set_gc_batch_complete(gcb
);
574 return nft_set_gc_batch_alloc(set
, gfp
);
577 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
580 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
584 * struct nft_expr_type - nf_tables expression type
586 * @select_ops: function to select nft_expr_ops
587 * @ops: default ops, used when no select_ops functions is present
588 * @list: used internally
590 * @owner: module reference
591 * @policy: netlink attribute policy
592 * @maxattr: highest netlink attribute number
593 * @family: address family for AF-specific types
594 * @flags: expression type flags
596 struct nft_expr_type
{
597 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
598 const struct nlattr
* const tb
[]);
599 const struct nft_expr_ops
*ops
;
600 struct list_head list
;
602 struct module
*owner
;
603 const struct nla_policy
*policy
;
604 unsigned int maxattr
;
609 #define NFT_EXPR_STATEFUL 0x1
612 * struct nft_expr_ops - nf_tables expression operations
614 * @eval: Expression evaluation function
615 * @size: full expression size, including private data size
616 * @init: initialization function
617 * @destroy: destruction function
618 * @dump: function to dump parameters
619 * @type: expression type
620 * @validate: validate expression, called during loop detection
621 * @data: extra data to attach to this expression operation
624 struct nft_expr_ops
{
625 void (*eval
)(const struct nft_expr
*expr
,
626 struct nft_regs
*regs
,
627 const struct nft_pktinfo
*pkt
);
628 int (*clone
)(struct nft_expr
*dst
,
629 const struct nft_expr
*src
);
632 int (*init
)(const struct nft_ctx
*ctx
,
633 const struct nft_expr
*expr
,
634 const struct nlattr
* const tb
[]);
635 void (*destroy
)(const struct nft_ctx
*ctx
,
636 const struct nft_expr
*expr
);
637 int (*dump
)(struct sk_buff
*skb
,
638 const struct nft_expr
*expr
);
639 int (*validate
)(const struct nft_ctx
*ctx
,
640 const struct nft_expr
*expr
,
641 const struct nft_data
**data
);
642 const struct nft_expr_type
*type
;
646 #define NFT_EXPR_MAXATTR 16
647 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
648 ALIGN(size, __alignof__(struct nft_expr)))
651 * struct nft_expr - nf_tables expression
653 * @ops: expression ops
654 * @data: expression private data
657 const struct nft_expr_ops
*ops
;
658 unsigned char data
[];
661 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
663 return (void *)expr
->data
;
666 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
667 const struct nlattr
*nla
);
668 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
669 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
670 const struct nft_expr
*expr
);
672 static inline int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
676 __module_get(src
->ops
->type
->owner
);
677 if (src
->ops
->clone
) {
679 err
= src
->ops
->clone(dst
, src
);
683 memcpy(dst
, src
, src
->ops
->size
);
689 * struct nft_rule - nf_tables rule
691 * @list: used internally
692 * @handle: rule handle
693 * @genmask: generation mask
694 * @dlen: length of expression data
695 * @udata: user data is appended to the rule
696 * @data: expression data
699 struct list_head list
;
705 __attribute__((aligned(__alignof__(struct nft_expr
))));
708 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
710 return (struct nft_expr
*)&rule
->data
[0];
713 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
715 return ((void *)expr
) + expr
->ops
->size
;
718 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
720 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
723 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
725 return (void *)&rule
->data
[rule
->dlen
];
729 * The last pointer isn't really necessary, but the compiler isn't able to
730 * determine that the result of nft_expr_last() is always the same since it
731 * can't assume that the dlen value wasn't changed within calls in the loop.
733 #define nft_rule_for_each_expr(expr, last, rule) \
734 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
736 (expr) = nft_expr_next(expr))
738 enum nft_chain_flags
{
739 NFT_BASE_CHAIN
= 0x1,
743 * struct nft_chain - nf_tables chain
745 * @rules: list of rules in the chain
746 * @list: used internally
747 * @table: table that this chain belongs to
748 * @handle: chain handle
749 * @use: number of jump references to this chain
750 * @level: length of longest path to this chain
751 * @flags: bitmask of enum nft_chain_flags
752 * @name: name of the chain
755 struct list_head rules
;
756 struct list_head list
;
757 struct nft_table
*table
;
763 char name
[NFT_CHAIN_MAXNAMELEN
];
766 enum nft_chain_type
{
767 NFT_CHAIN_T_DEFAULT
= 0,
774 * struct nf_chain_type - nf_tables chain type info
776 * @name: name of the type
777 * @type: numeric identifier
778 * @family: address family
779 * @owner: module owner
780 * @hook_mask: mask of valid hooks
781 * @hooks: hookfn overrides
783 struct nf_chain_type
{
785 enum nft_chain_type type
;
787 struct module
*owner
;
788 unsigned int hook_mask
;
789 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
792 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
793 enum nft_chain_type type
);
794 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
795 unsigned int hook_flags
);
800 struct u64_stats_sync syncp
;
803 #define NFT_HOOK_OPS_MAX 2
806 * struct nft_base_chain - nf_tables base chain
808 * @ops: netfilter hook ops
810 * @policy: default policy
811 * @stats: per-cpu chain stats
813 * @dev_name: device name that this base chain is attached to (if any)
815 struct nft_base_chain
{
816 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
817 const struct nf_chain_type
*type
;
820 struct nft_stats __percpu
*stats
;
821 struct nft_chain chain
;
822 char dev_name
[IFNAMSIZ
];
825 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
827 return container_of(chain
, struct nft_base_chain
, chain
);
830 int __nft_release_basechain(struct nft_ctx
*ctx
);
832 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
, void *priv
);
835 * struct nft_table - nf_tables table
837 * @list: used internally
838 * @chains: chains in the table
839 * @sets: sets in the table
840 * @hgenerator: handle generator state
841 * @use: number of chain references to this table
842 * @flags: table flag (see enum nft_table_flags)
843 * @genmask: generation mask
844 * @name: name of the table
847 struct list_head list
;
848 struct list_head chains
;
849 struct list_head sets
;
854 char name
[NFT_TABLE_MAXNAMELEN
];
858 NFT_AF_NEEDS_DEV
= (1 << 0),
862 * struct nft_af_info - nf_tables address family info
864 * @list: used internally
865 * @family: address family
866 * @nhooks: number of hooks in this family
867 * @owner: module owner
868 * @tables: used internally
869 * @flags: family flags
870 * @nops: number of hook ops in this family
871 * @hook_ops_init: initialization function for chain hook ops
872 * @hooks: hookfn overrides for packet validation
875 struct list_head list
;
878 struct module
*owner
;
879 struct list_head tables
;
882 void (*hook_ops_init
)(struct nf_hook_ops
*,
884 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
887 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
888 void nft_unregister_afinfo(struct net
*, struct nft_af_info
*);
890 int nft_register_chain_type(const struct nf_chain_type
*);
891 void nft_unregister_chain_type(const struct nf_chain_type
*);
893 int nft_register_expr(struct nft_expr_type
*);
894 void nft_unregister_expr(struct nft_expr_type
*);
896 int nft_verdict_dump(struct sk_buff
*skb
, int type
,
897 const struct nft_verdict
*v
);
900 * struct nft_traceinfo - nft tracing information and state
902 * @pkt: pktinfo currently processed
903 * @basechain: base chain currently processed
904 * @chain: chain currently processed
905 * @rule: rule that was evaluated
906 * @verdict: verdict given by rule
907 * @type: event type (enum nft_trace_types)
908 * @packet_dumped: packet headers sent in a previous traceinfo message
909 * @trace: other struct members are initialised
911 struct nft_traceinfo
{
912 const struct nft_pktinfo
*pkt
;
913 const struct nft_base_chain
*basechain
;
914 const struct nft_chain
*chain
;
915 const struct nft_rule
*rule
;
916 const struct nft_verdict
*verdict
;
917 enum nft_trace_types type
;
922 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
923 const struct nft_verdict
*verdict
,
924 const struct nft_chain
*basechain
);
926 void nft_trace_notify(struct nft_traceinfo
*info
);
928 #define nft_dereference(p) \
929 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
931 #define MODULE_ALIAS_NFT_FAMILY(family) \
932 MODULE_ALIAS("nft-afinfo-" __stringify(family))
934 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
935 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
937 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
938 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
940 #define MODULE_ALIAS_NFT_EXPR(name) \
941 MODULE_ALIAS("nft-expr-" name)
943 #define MODULE_ALIAS_NFT_SET() \
944 MODULE_ALIAS("nft-set")
947 * The gencursor defines two generations, the currently active and the
948 * next one. Objects contain a bitmask of 2 bits specifying the generations
949 * they're active in. A set bit means they're inactive in the generation
950 * represented by that bit.
952 * New objects start out as inactive in the current and active in the
953 * next generation. When committing the ruleset the bitmask is cleared,
954 * meaning they're active in all generations. When removing an object,
955 * it is set inactive in the next generation. After committing the ruleset,
956 * the objects are removed.
958 static inline unsigned int nft_gencursor_next(const struct net
*net
)
960 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
963 static inline u8
nft_genmask_next(const struct net
*net
)
965 return 1 << nft_gencursor_next(net
);
968 static inline u8
nft_genmask_cur(const struct net
*net
)
970 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
971 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
974 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
977 * Generic transaction helpers
980 /* Check if this object is currently active. */
981 #define nft_is_active(__net, __obj) \
982 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
984 /* Check if this object is active in the next generation. */
985 #define nft_is_active_next(__net, __obj) \
986 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
988 /* This object becomes active in the next generation. */
989 #define nft_activate_next(__net, __obj) \
990 (__obj)->genmask = nft_genmask_cur(__net)
992 /* This object becomes inactive in the next generation. */
993 #define nft_deactivate_next(__net, __obj) \
994 (__obj)->genmask = nft_genmask_next(__net)
996 /* After committing the ruleset, clear the stale generation bit. */
997 #define nft_clear(__net, __obj) \
998 (__obj)->genmask &= ~nft_genmask_next(__net)
999 #define nft_active_genmask(__obj, __genmask) \
1000 !((__obj)->genmask & __genmask)
1003 * Set element transaction helpers
1006 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
1009 return !(ext
->genmask
& genmask
);
1012 static inline void nft_set_elem_change_active(const struct net
*net
,
1013 const struct nft_set
*set
,
1014 struct nft_set_ext
*ext
)
1016 ext
->genmask
^= nft_genmask_next(net
);
1020 * We use a free bit in the genmask field to indicate the element
1021 * is busy, meaning it is currently being processed either by
1022 * the netlink API or GC.
1024 * Even though the genmask is only a single byte wide, this works
1025 * because the extension structure if fully constant once initialized,
1026 * so there are no non-atomic write accesses unless it is already
1029 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1031 #if defined(__LITTLE_ENDIAN_BITFIELD)
1032 #define NFT_SET_ELEM_BUSY_BIT 2
1033 #elif defined(__BIG_ENDIAN_BITFIELD)
1034 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1039 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
1041 unsigned long *word
= (unsigned long *)ext
;
1043 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
1044 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1047 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
1049 unsigned long *word
= (unsigned long *)ext
;
1051 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1055 * struct nft_trans - nf_tables object update in transaction
1057 * @list: used internally
1058 * @msg_type: message type
1059 * @ctx: transaction context
1060 * @data: internal information related to the transaction
1063 struct list_head list
;
1069 struct nft_trans_rule
{
1070 struct nft_rule
*rule
;
1073 #define nft_trans_rule(trans) \
1074 (((struct nft_trans_rule *)trans->data)->rule)
1076 struct nft_trans_set
{
1077 struct nft_set
*set
;
1081 #define nft_trans_set(trans) \
1082 (((struct nft_trans_set *)trans->data)->set)
1083 #define nft_trans_set_id(trans) \
1084 (((struct nft_trans_set *)trans->data)->set_id)
1086 struct nft_trans_chain
{
1088 char name
[NFT_CHAIN_MAXNAMELEN
];
1089 struct nft_stats __percpu
*stats
;
1093 #define nft_trans_chain_update(trans) \
1094 (((struct nft_trans_chain *)trans->data)->update)
1095 #define nft_trans_chain_name(trans) \
1096 (((struct nft_trans_chain *)trans->data)->name)
1097 #define nft_trans_chain_stats(trans) \
1098 (((struct nft_trans_chain *)trans->data)->stats)
1099 #define nft_trans_chain_policy(trans) \
1100 (((struct nft_trans_chain *)trans->data)->policy)
1102 struct nft_trans_table
{
1107 #define nft_trans_table_update(trans) \
1108 (((struct nft_trans_table *)trans->data)->update)
1109 #define nft_trans_table_enable(trans) \
1110 (((struct nft_trans_table *)trans->data)->enable)
1112 struct nft_trans_elem
{
1113 struct nft_set
*set
;
1114 struct nft_set_elem elem
;
1117 #define nft_trans_elem_set(trans) \
1118 (((struct nft_trans_elem *)trans->data)->set)
1119 #define nft_trans_elem(trans) \
1120 (((struct nft_trans_elem *)trans->data)->elem)
1122 #endif /* _NET_NF_TABLES_H */