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
;
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
->net
= pkt
->xt
.net
= state
->net
;
34 pkt
->in
= pkt
->xt
.in
= state
->in
;
35 pkt
->out
= pkt
->xt
.out
= state
->out
;
36 pkt
->hook
= pkt
->xt
.hooknum
= state
->hook
;
37 pkt
->pf
= pkt
->xt
.family
= state
->pf
;
40 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo
*pkt
,
43 pkt
->tprot_set
= false;
49 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo
*pkt
,
51 const struct nf_hook_state
*state
)
53 nft_set_pktinfo(pkt
, skb
, state
);
54 nft_set_pktinfo_proto_unspec(pkt
, skb
);
58 * struct nft_verdict - nf_tables verdict
60 * @code: nf_tables/netfilter verdict code
61 * @chain: destination chain for NFT_JUMP/NFT_GOTO
65 struct nft_chain
*chain
;
71 struct nft_verdict verdict
;
73 } __attribute__((aligned(__alignof__(u64
))));
76 * struct nft_regs - nf_tables register set
78 * @data: data registers
79 * @verdict: verdict register
81 * The first four data registers alias to the verdict register.
86 struct nft_verdict verdict
;
90 static inline void nft_data_copy(u32
*dst
, const struct nft_data
*src
,
93 memcpy(dst
, src
, len
);
96 static inline void nft_data_debug(const struct nft_data
*data
)
98 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
99 data
->data
[0], data
->data
[1],
100 data
->data
[2], data
->data
[3]);
104 * struct nft_ctx - nf_tables rule/set context
106 * @net: net namespace
107 * @afi: address family info
108 * @table: the table the chain is contained in
109 * @chain: the chain the rule is contained in
110 * @nla: netlink attributes
111 * @portid: netlink portID of the original message
112 * @seq: netlink sequence number
113 * @report: notify via unicast netlink message
117 struct nft_af_info
*afi
;
118 struct nft_table
*table
;
119 struct nft_chain
*chain
;
120 const struct nlattr
* const *nla
;
126 struct nft_data_desc
{
127 enum nft_data_types type
;
131 int nft_data_init(const struct nft_ctx
*ctx
,
132 struct nft_data
*data
, unsigned int size
,
133 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
134 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
);
135 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
136 enum nft_data_types type
, unsigned int len
);
138 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
140 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
143 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
145 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
148 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
);
149 unsigned int nft_parse_register(const struct nlattr
*attr
);
150 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
152 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
);
153 int nft_validate_register_store(const struct nft_ctx
*ctx
,
154 enum nft_registers reg
,
155 const struct nft_data
*data
,
156 enum nft_data_types type
, unsigned int len
);
159 * struct nft_userdata - user defined data associated with an object
161 * @len: length of the data
164 * The presence of user data is indicated in an object specific fashion,
165 * so a length of zero can't occur and the value "len" indicates data
168 struct nft_userdata
{
170 unsigned char data
[0];
174 * struct nft_set_elem - generic representation of set elements
177 * @priv: element private data and extensions
179 struct nft_set_elem
{
181 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
188 struct nft_set_iter
{
193 int (*fn
)(const struct nft_ctx
*ctx
,
194 const struct nft_set
*set
,
195 const struct nft_set_iter
*iter
,
196 const struct nft_set_elem
*elem
);
200 * struct nft_set_desc - description of set elements
204 * @size: number of set elements
206 struct nft_set_desc
{
213 * enum nft_set_class - performance class
215 * @NFT_LOOKUP_O_1: constant, O(1)
216 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
217 * @NFT_LOOKUP_O_N: linear, O(N)
221 NFT_SET_CLASS_O_LOG_N
,
226 * struct nft_set_estimate - estimation of memory and performance
229 * @size: required memory
230 * @class: lookup performance class
232 struct nft_set_estimate
{
234 enum nft_set_class
class;
241 * struct nft_set_ops - nf_tables set operations
243 * @lookup: look up an element within the set
244 * @insert: insert new element into set
245 * @activate: activate new element in the next generation
246 * @deactivate: deactivate element in the next generation
247 * @remove: remove element from set
248 * @walk: iterate over all set elemeennts
249 * @privsize: function to return size of set private data
250 * @init: initialize private data of new set instance
251 * @destroy: destroy private data of set instance
252 * @list: nf_tables_set_ops list node
253 * @owner: module reference
254 * @elemsize: element private size
255 * @features: features supported by the implementation
258 bool (*lookup
)(const struct net
*net
,
259 const struct nft_set
*set
,
261 const struct nft_set_ext
**ext
);
262 bool (*update
)(struct nft_set
*set
,
264 void *(*new)(struct nft_set
*,
265 const struct nft_expr
*,
267 const struct nft_expr
*expr
,
268 struct nft_regs
*regs
,
269 const struct nft_set_ext
**ext
);
271 int (*insert
)(const struct net
*net
,
272 const struct nft_set
*set
,
273 const struct nft_set_elem
*elem
,
274 struct nft_set_ext
**ext
);
275 void (*activate
)(const struct net
*net
,
276 const struct nft_set
*set
,
277 const struct nft_set_elem
*elem
);
278 void * (*deactivate
)(const struct net
*net
,
279 const struct nft_set
*set
,
280 const struct nft_set_elem
*elem
);
281 void (*remove
)(const struct nft_set
*set
,
282 const struct nft_set_elem
*elem
);
283 void (*walk
)(const struct nft_ctx
*ctx
,
284 const struct nft_set
*set
,
285 struct nft_set_iter
*iter
);
287 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
288 bool (*estimate
)(const struct nft_set_desc
*desc
,
290 struct nft_set_estimate
*est
);
291 int (*init
)(const struct nft_set
*set
,
292 const struct nft_set_desc
*desc
,
293 const struct nlattr
* const nla
[]);
294 void (*destroy
)(const struct nft_set
*set
);
296 struct list_head list
;
297 struct module
*owner
;
298 unsigned int elemsize
;
302 int nft_register_set(struct nft_set_ops
*ops
);
303 void nft_unregister_set(struct nft_set_ops
*ops
);
306 * struct nft_set - nf_tables set instance
308 * @list: table set list node
309 * @bindings: list of set bindings
310 * @name: name of the set
311 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
312 * @dtype: data type (verdict or numeric type defined by userspace)
313 * @size: maximum set size
314 * @nelems: number of elements
315 * @ndeact: number of deactivated elements queued for removal
316 * @timeout: default timeout value in jiffies
317 * @gc_int: garbage collection interval in msecs
318 * @policy: set parameterization (see enum nft_set_policies)
319 * @udlen: user data length
323 * @genmask: generation mask
326 * @data: private set data
329 struct list_head list
;
330 struct list_head bindings
;
331 char name
[NFT_SET_MAXNAMELEN
];
341 unsigned char *udata
;
342 /* runtime data below here */
343 const struct nft_set_ops
*ops ____cacheline_aligned
;
349 __attribute__((aligned(__alignof__(u64
))));
352 static inline void *nft_set_priv(const struct nft_set
*set
)
354 return (void *)set
->data
;
357 static inline struct nft_set
*nft_set_container_of(const void *priv
)
359 return (void *)priv
- offsetof(struct nft_set
, data
);
362 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
363 const struct nlattr
*nla
, u8 genmask
);
364 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
365 const struct nlattr
*nla
, u8 genmask
);
367 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
369 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
373 * struct nft_set_binding - nf_tables set binding
375 * @list: set bindings list node
376 * @chain: chain containing the rule bound to the set
377 * @flags: set action flags
379 * A set binding contains all information necessary for validation
380 * of new elements added to a bound set.
382 struct nft_set_binding
{
383 struct list_head list
;
384 const struct nft_chain
*chain
;
388 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
389 struct nft_set_binding
*binding
);
390 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
391 struct nft_set_binding
*binding
);
394 * enum nft_set_extensions - set extension type IDs
396 * @NFT_SET_EXT_KEY: element key
397 * @NFT_SET_EXT_DATA: mapping data
398 * @NFT_SET_EXT_FLAGS: element flags
399 * @NFT_SET_EXT_TIMEOUT: element timeout
400 * @NFT_SET_EXT_EXPIRATION: element expiration time
401 * @NFT_SET_EXT_USERDATA: user data associated with the element
402 * @NFT_SET_EXT_EXPR: expression assiociated with the element
403 * @NFT_SET_EXT_NUM: number of extension types
405 enum nft_set_extensions
{
410 NFT_SET_EXT_EXPIRATION
,
411 NFT_SET_EXT_USERDATA
,
417 * struct nft_set_ext_type - set extension type
419 * @len: fixed part length of the extension
420 * @align: alignment requirements of the extension
422 struct nft_set_ext_type
{
427 extern const struct nft_set_ext_type nft_set_ext_types
[];
430 * struct nft_set_ext_tmpl - set extension template
432 * @len: length of extension area
433 * @offset: offsets of individual extension types
435 struct nft_set_ext_tmpl
{
437 u8 offset
[NFT_SET_EXT_NUM
];
441 * struct nft_set_ext - set extensions
443 * @genmask: generation mask
444 * @offset: offsets of individual extension types
445 * @data: beginning of extension data
449 u8 offset
[NFT_SET_EXT_NUM
];
453 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
455 memset(tmpl
, 0, sizeof(*tmpl
));
456 tmpl
->len
= sizeof(struct nft_set_ext
);
459 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
462 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
463 BUG_ON(tmpl
->len
> U8_MAX
);
464 tmpl
->offset
[id
] = tmpl
->len
;
465 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
468 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
470 nft_set_ext_add_length(tmpl
, id
, 0);
473 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
474 const struct nft_set_ext_tmpl
*tmpl
)
476 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
479 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
481 return !!ext
->offset
[id
];
484 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
486 return ext
&& __nft_set_ext_exists(ext
, id
);
489 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
491 return (void *)ext
+ ext
->offset
[id
];
494 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
496 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
499 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
501 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
504 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
506 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
509 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
511 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
514 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
516 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
519 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
521 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
524 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
526 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
529 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
531 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
532 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
535 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
538 return elem
+ set
->ops
->elemsize
;
541 void *nft_set_elem_init(const struct nft_set
*set
,
542 const struct nft_set_ext_tmpl
*tmpl
,
543 const u32
*key
, const u32
*data
,
544 u64 timeout
, gfp_t gfp
);
545 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
549 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
552 * @set: set the elements belong to
553 * @cnt: count of elements
555 struct nft_set_gc_batch_head
{
557 const struct nft_set
*set
;
561 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
562 sizeof(struct nft_set_gc_batch_head)) / \
566 * struct nft_set_gc_batch - nf_tables set garbage collection batch
568 * @head: GC batch head
569 * @elems: garbage collection elements
571 struct nft_set_gc_batch
{
572 struct nft_set_gc_batch_head head
;
573 void *elems
[NFT_SET_GC_BATCH_SIZE
];
576 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
578 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
580 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
583 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
586 static inline struct nft_set_gc_batch
*
587 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
591 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
593 nft_set_gc_batch_complete(gcb
);
595 return nft_set_gc_batch_alloc(set
, gfp
);
598 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
601 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
605 * struct nft_expr_type - nf_tables expression type
607 * @select_ops: function to select nft_expr_ops
608 * @ops: default ops, used when no select_ops functions is present
609 * @list: used internally
611 * @owner: module reference
612 * @policy: netlink attribute policy
613 * @maxattr: highest netlink attribute number
614 * @family: address family for AF-specific types
615 * @flags: expression type flags
617 struct nft_expr_type
{
618 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
619 const struct nlattr
* const tb
[]);
620 const struct nft_expr_ops
*ops
;
621 struct list_head list
;
623 struct module
*owner
;
624 const struct nla_policy
*policy
;
625 unsigned int maxattr
;
630 #define NFT_EXPR_STATEFUL 0x1
633 * struct nft_expr_ops - nf_tables expression operations
635 * @eval: Expression evaluation function
636 * @size: full expression size, including private data size
637 * @init: initialization function
638 * @destroy: destruction function
639 * @dump: function to dump parameters
640 * @type: expression type
641 * @validate: validate expression, called during loop detection
642 * @data: extra data to attach to this expression operation
645 struct nft_expr_ops
{
646 void (*eval
)(const struct nft_expr
*expr
,
647 struct nft_regs
*regs
,
648 const struct nft_pktinfo
*pkt
);
649 int (*clone
)(struct nft_expr
*dst
,
650 const struct nft_expr
*src
);
653 int (*init
)(const struct nft_ctx
*ctx
,
654 const struct nft_expr
*expr
,
655 const struct nlattr
* const tb
[]);
656 void (*destroy
)(const struct nft_ctx
*ctx
,
657 const struct nft_expr
*expr
);
658 int (*dump
)(struct sk_buff
*skb
,
659 const struct nft_expr
*expr
);
660 int (*validate
)(const struct nft_ctx
*ctx
,
661 const struct nft_expr
*expr
,
662 const struct nft_data
**data
);
663 const struct nft_expr_type
*type
;
667 #define NFT_EXPR_MAXATTR 16
668 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
669 ALIGN(size, __alignof__(struct nft_expr)))
672 * struct nft_expr - nf_tables expression
674 * @ops: expression ops
675 * @data: expression private data
678 const struct nft_expr_ops
*ops
;
679 unsigned char data
[];
682 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
684 return (void *)expr
->data
;
687 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
688 const struct nlattr
*nla
);
689 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
690 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
691 const struct nft_expr
*expr
);
693 static inline int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
697 if (src
->ops
->clone
) {
699 err
= src
->ops
->clone(dst
, src
);
703 memcpy(dst
, src
, src
->ops
->size
);
706 __module_get(src
->ops
->type
->owner
);
711 * struct nft_rule - nf_tables rule
713 * @list: used internally
714 * @handle: rule handle
715 * @genmask: generation mask
716 * @dlen: length of expression data
717 * @udata: user data is appended to the rule
718 * @data: expression data
721 struct list_head list
;
727 __attribute__((aligned(__alignof__(struct nft_expr
))));
730 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
732 return (struct nft_expr
*)&rule
->data
[0];
735 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
737 return ((void *)expr
) + expr
->ops
->size
;
740 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
742 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
745 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
747 return (void *)&rule
->data
[rule
->dlen
];
751 * The last pointer isn't really necessary, but the compiler isn't able to
752 * determine that the result of nft_expr_last() is always the same since it
753 * can't assume that the dlen value wasn't changed within calls in the loop.
755 #define nft_rule_for_each_expr(expr, last, rule) \
756 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
758 (expr) = nft_expr_next(expr))
760 enum nft_chain_flags
{
761 NFT_BASE_CHAIN
= 0x1,
765 * struct nft_chain - nf_tables chain
767 * @rules: list of rules in the chain
768 * @list: used internally
769 * @table: table that this chain belongs to
770 * @handle: chain handle
771 * @use: number of jump references to this chain
772 * @level: length of longest path to this chain
773 * @flags: bitmask of enum nft_chain_flags
774 * @name: name of the chain
777 struct list_head rules
;
778 struct list_head list
;
779 struct nft_table
*table
;
785 char name
[NFT_CHAIN_MAXNAMELEN
];
788 enum nft_chain_type
{
789 NFT_CHAIN_T_DEFAULT
= 0,
796 * struct nf_chain_type - nf_tables chain type info
798 * @name: name of the type
799 * @type: numeric identifier
800 * @family: address family
801 * @owner: module owner
802 * @hook_mask: mask of valid hooks
803 * @hooks: hookfn overrides
805 struct nf_chain_type
{
807 enum nft_chain_type type
;
809 struct module
*owner
;
810 unsigned int hook_mask
;
811 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
814 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
815 enum nft_chain_type type
);
816 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
817 unsigned int hook_flags
);
822 struct u64_stats_sync syncp
;
825 #define NFT_HOOK_OPS_MAX 2
828 * struct nft_base_chain - nf_tables base chain
830 * @ops: netfilter hook ops
832 * @policy: default policy
833 * @stats: per-cpu chain stats
835 * @dev_name: device name that this base chain is attached to (if any)
837 struct nft_base_chain
{
838 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
839 const struct nf_chain_type
*type
;
842 struct nft_stats __percpu
*stats
;
843 struct nft_chain chain
;
844 char dev_name
[IFNAMSIZ
];
847 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
849 return container_of(chain
, struct nft_base_chain
, chain
);
852 int __nft_release_basechain(struct nft_ctx
*ctx
);
854 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
, void *priv
);
857 * struct nft_table - nf_tables table
859 * @list: used internally
860 * @chains: chains in the table
861 * @sets: sets in the table
862 * @hgenerator: handle generator state
863 * @use: number of chain references to this table
864 * @flags: table flag (see enum nft_table_flags)
865 * @genmask: generation mask
866 * @name: name of the table
869 struct list_head list
;
870 struct list_head chains
;
871 struct list_head sets
;
876 char name
[NFT_TABLE_MAXNAMELEN
];
880 NFT_AF_NEEDS_DEV
= (1 << 0),
884 * struct nft_af_info - nf_tables address family info
886 * @list: used internally
887 * @family: address family
888 * @nhooks: number of hooks in this family
889 * @owner: module owner
890 * @tables: used internally
891 * @flags: family flags
892 * @nops: number of hook ops in this family
893 * @hook_ops_init: initialization function for chain hook ops
894 * @hooks: hookfn overrides for packet validation
897 struct list_head list
;
900 struct module
*owner
;
901 struct list_head tables
;
904 void (*hook_ops_init
)(struct nf_hook_ops
*,
906 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
909 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
910 void nft_unregister_afinfo(struct net
*, struct nft_af_info
*);
912 int nft_register_chain_type(const struct nf_chain_type
*);
913 void nft_unregister_chain_type(const struct nf_chain_type
*);
915 int nft_register_expr(struct nft_expr_type
*);
916 void nft_unregister_expr(struct nft_expr_type
*);
918 int nft_verdict_dump(struct sk_buff
*skb
, int type
,
919 const struct nft_verdict
*v
);
922 * struct nft_traceinfo - nft tracing information and state
924 * @pkt: pktinfo currently processed
925 * @basechain: base chain currently processed
926 * @chain: chain currently processed
927 * @rule: rule that was evaluated
928 * @verdict: verdict given by rule
929 * @type: event type (enum nft_trace_types)
930 * @packet_dumped: packet headers sent in a previous traceinfo message
931 * @trace: other struct members are initialised
933 struct nft_traceinfo
{
934 const struct nft_pktinfo
*pkt
;
935 const struct nft_base_chain
*basechain
;
936 const struct nft_chain
*chain
;
937 const struct nft_rule
*rule
;
938 const struct nft_verdict
*verdict
;
939 enum nft_trace_types type
;
944 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
945 const struct nft_verdict
*verdict
,
946 const struct nft_chain
*basechain
);
948 void nft_trace_notify(struct nft_traceinfo
*info
);
950 #define nft_dereference(p) \
951 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
953 #define MODULE_ALIAS_NFT_FAMILY(family) \
954 MODULE_ALIAS("nft-afinfo-" __stringify(family))
956 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
957 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
959 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
960 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
962 #define MODULE_ALIAS_NFT_EXPR(name) \
963 MODULE_ALIAS("nft-expr-" name)
965 #define MODULE_ALIAS_NFT_SET() \
966 MODULE_ALIAS("nft-set")
969 * The gencursor defines two generations, the currently active and the
970 * next one. Objects contain a bitmask of 2 bits specifying the generations
971 * they're active in. A set bit means they're inactive in the generation
972 * represented by that bit.
974 * New objects start out as inactive in the current and active in the
975 * next generation. When committing the ruleset the bitmask is cleared,
976 * meaning they're active in all generations. When removing an object,
977 * it is set inactive in the next generation. After committing the ruleset,
978 * the objects are removed.
980 static inline unsigned int nft_gencursor_next(const struct net
*net
)
982 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
985 static inline u8
nft_genmask_next(const struct net
*net
)
987 return 1 << nft_gencursor_next(net
);
990 static inline u8
nft_genmask_cur(const struct net
*net
)
992 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
993 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
996 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
999 * Generic transaction helpers
1002 /* Check if this object is currently active. */
1003 #define nft_is_active(__net, __obj) \
1004 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1006 /* Check if this object is active in the next generation. */
1007 #define nft_is_active_next(__net, __obj) \
1008 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1010 /* This object becomes active in the next generation. */
1011 #define nft_activate_next(__net, __obj) \
1012 (__obj)->genmask = nft_genmask_cur(__net)
1014 /* This object becomes inactive in the next generation. */
1015 #define nft_deactivate_next(__net, __obj) \
1016 (__obj)->genmask = nft_genmask_next(__net)
1018 /* After committing the ruleset, clear the stale generation bit. */
1019 #define nft_clear(__net, __obj) \
1020 (__obj)->genmask &= ~nft_genmask_next(__net)
1021 #define nft_active_genmask(__obj, __genmask) \
1022 !((__obj)->genmask & __genmask)
1025 * Set element transaction helpers
1028 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
1031 return !(ext
->genmask
& genmask
);
1034 static inline void nft_set_elem_change_active(const struct net
*net
,
1035 const struct nft_set
*set
,
1036 struct nft_set_ext
*ext
)
1038 ext
->genmask
^= nft_genmask_next(net
);
1042 * We use a free bit in the genmask field to indicate the element
1043 * is busy, meaning it is currently being processed either by
1044 * the netlink API or GC.
1046 * Even though the genmask is only a single byte wide, this works
1047 * because the extension structure if fully constant once initialized,
1048 * so there are no non-atomic write accesses unless it is already
1051 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1053 #if defined(__LITTLE_ENDIAN_BITFIELD)
1054 #define NFT_SET_ELEM_BUSY_BIT 2
1055 #elif defined(__BIG_ENDIAN_BITFIELD)
1056 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1061 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
1063 unsigned long *word
= (unsigned long *)ext
;
1065 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
1066 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1069 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
1071 unsigned long *word
= (unsigned long *)ext
;
1073 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1077 * struct nft_trans - nf_tables object update in transaction
1079 * @list: used internally
1080 * @msg_type: message type
1081 * @ctx: transaction context
1082 * @data: internal information related to the transaction
1085 struct list_head list
;
1091 struct nft_trans_rule
{
1092 struct nft_rule
*rule
;
1095 #define nft_trans_rule(trans) \
1096 (((struct nft_trans_rule *)trans->data)->rule)
1098 struct nft_trans_set
{
1099 struct nft_set
*set
;
1103 #define nft_trans_set(trans) \
1104 (((struct nft_trans_set *)trans->data)->set)
1105 #define nft_trans_set_id(trans) \
1106 (((struct nft_trans_set *)trans->data)->set_id)
1108 struct nft_trans_chain
{
1110 char name
[NFT_CHAIN_MAXNAMELEN
];
1111 struct nft_stats __percpu
*stats
;
1115 #define nft_trans_chain_update(trans) \
1116 (((struct nft_trans_chain *)trans->data)->update)
1117 #define nft_trans_chain_name(trans) \
1118 (((struct nft_trans_chain *)trans->data)->name)
1119 #define nft_trans_chain_stats(trans) \
1120 (((struct nft_trans_chain *)trans->data)->stats)
1121 #define nft_trans_chain_policy(trans) \
1122 (((struct nft_trans_chain *)trans->data)->policy)
1124 struct nft_trans_table
{
1129 #define nft_trans_table_update(trans) \
1130 (((struct nft_trans_table *)trans->data)->update)
1131 #define nft_trans_table_enable(trans) \
1132 (((struct nft_trans_table *)trans->data)->enable)
1134 struct nft_trans_elem
{
1135 struct nft_set
*set
;
1136 struct nft_set_elem elem
;
1139 #define nft_trans_elem_set(trans) \
1140 (((struct nft_trans_elem *)trans->data)->set)
1141 #define nft_trans_elem(trans) \
1142 (((struct nft_trans_elem *)trans->data)->elem)
1144 #endif /* _NET_NF_TABLES_H */