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 unsigned int nft_parse_register(const struct nlattr
*attr
);
149 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
151 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
);
152 int nft_validate_register_store(const struct nft_ctx
*ctx
,
153 enum nft_registers reg
,
154 const struct nft_data
*data
,
155 enum nft_data_types type
, unsigned int len
);
158 * struct nft_userdata - user defined data associated with an object
160 * @len: length of the data
163 * The presence of user data is indicated in an object specific fashion,
164 * so a length of zero can't occur and the value "len" indicates data
167 struct nft_userdata
{
169 unsigned char data
[0];
173 * struct nft_set_elem - generic representation of set elements
176 * @priv: element private data and extensions
178 struct nft_set_elem
{
180 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
187 struct nft_set_iter
{
192 int (*fn
)(const struct nft_ctx
*ctx
,
193 const struct nft_set
*set
,
194 const struct nft_set_iter
*iter
,
195 const struct nft_set_elem
*elem
);
199 * struct nft_set_desc - description of set elements
203 * @size: number of set elements
205 struct nft_set_desc
{
212 * enum nft_set_class - performance class
214 * @NFT_LOOKUP_O_1: constant, O(1)
215 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
216 * @NFT_LOOKUP_O_N: linear, O(N)
220 NFT_SET_CLASS_O_LOG_N
,
225 * struct nft_set_estimate - estimation of memory and performance
228 * @size: required memory
229 * @class: lookup performance class
231 struct nft_set_estimate
{
233 enum nft_set_class
class;
240 * struct nft_set_ops - nf_tables set operations
242 * @lookup: look up an element within the set
243 * @insert: insert new element into set
244 * @activate: activate new element in the next generation
245 * @deactivate: deactivate element in the next generation
246 * @remove: remove element from set
247 * @walk: iterate over all set elemeennts
248 * @privsize: function to return size of set private data
249 * @init: initialize private data of new set instance
250 * @destroy: destroy private data of set instance
251 * @list: nf_tables_set_ops list node
252 * @owner: module reference
253 * @elemsize: element private size
254 * @features: features supported by the implementation
257 bool (*lookup
)(const struct net
*net
,
258 const struct nft_set
*set
,
260 const struct nft_set_ext
**ext
);
261 bool (*update
)(struct nft_set
*set
,
263 void *(*new)(struct nft_set
*,
264 const struct nft_expr
*,
266 const struct nft_expr
*expr
,
267 struct nft_regs
*regs
,
268 const struct nft_set_ext
**ext
);
270 int (*insert
)(const struct net
*net
,
271 const struct nft_set
*set
,
272 const struct nft_set_elem
*elem
,
273 struct nft_set_ext
**ext
);
274 void (*activate
)(const struct net
*net
,
275 const struct nft_set
*set
,
276 const struct nft_set_elem
*elem
);
277 void * (*deactivate
)(const struct net
*net
,
278 const struct nft_set
*set
,
279 const struct nft_set_elem
*elem
);
280 void (*remove
)(const struct nft_set
*set
,
281 const struct nft_set_elem
*elem
);
282 void (*walk
)(const struct nft_ctx
*ctx
,
283 const struct nft_set
*set
,
284 struct nft_set_iter
*iter
);
286 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
287 bool (*estimate
)(const struct nft_set_desc
*desc
,
289 struct nft_set_estimate
*est
);
290 int (*init
)(const struct nft_set
*set
,
291 const struct nft_set_desc
*desc
,
292 const struct nlattr
* const nla
[]);
293 void (*destroy
)(const struct nft_set
*set
);
295 struct list_head list
;
296 struct module
*owner
;
297 unsigned int elemsize
;
301 int nft_register_set(struct nft_set_ops
*ops
);
302 void nft_unregister_set(struct nft_set_ops
*ops
);
305 * struct nft_set - nf_tables set instance
307 * @list: table set list node
308 * @bindings: list of set bindings
309 * @name: name of the set
310 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
311 * @dtype: data type (verdict or numeric type defined by userspace)
312 * @size: maximum set size
313 * @nelems: number of elements
314 * @ndeact: number of deactivated elements queued for removal
315 * @timeout: default timeout value in msecs
316 * @gc_int: garbage collection interval in msecs
317 * @policy: set parameterization (see enum nft_set_policies)
318 * @udlen: user data length
322 * @genmask: generation mask
325 * @data: private set data
328 struct list_head list
;
329 struct list_head bindings
;
330 char name
[NFT_SET_MAXNAMELEN
];
340 unsigned char *udata
;
341 /* runtime data below here */
342 const struct nft_set_ops
*ops ____cacheline_aligned
;
348 __attribute__((aligned(__alignof__(u64
))));
351 static inline void *nft_set_priv(const struct nft_set
*set
)
353 return (void *)set
->data
;
356 static inline struct nft_set
*nft_set_container_of(const void *priv
)
358 return (void *)priv
- offsetof(struct nft_set
, data
);
361 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
362 const struct nlattr
*nla
, u8 genmask
);
363 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
364 const struct nlattr
*nla
, u8 genmask
);
366 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
368 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
372 * struct nft_set_binding - nf_tables set binding
374 * @list: set bindings list node
375 * @chain: chain containing the rule bound to the set
376 * @flags: set action flags
378 * A set binding contains all information necessary for validation
379 * of new elements added to a bound set.
381 struct nft_set_binding
{
382 struct list_head list
;
383 const struct nft_chain
*chain
;
387 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
388 struct nft_set_binding
*binding
);
389 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
390 struct nft_set_binding
*binding
);
393 * enum nft_set_extensions - set extension type IDs
395 * @NFT_SET_EXT_KEY: element key
396 * @NFT_SET_EXT_DATA: mapping data
397 * @NFT_SET_EXT_FLAGS: element flags
398 * @NFT_SET_EXT_TIMEOUT: element timeout
399 * @NFT_SET_EXT_EXPIRATION: element expiration time
400 * @NFT_SET_EXT_USERDATA: user data associated with the element
401 * @NFT_SET_EXT_EXPR: expression assiociated with the element
402 * @NFT_SET_EXT_NUM: number of extension types
404 enum nft_set_extensions
{
409 NFT_SET_EXT_EXPIRATION
,
410 NFT_SET_EXT_USERDATA
,
416 * struct nft_set_ext_type - set extension type
418 * @len: fixed part length of the extension
419 * @align: alignment requirements of the extension
421 struct nft_set_ext_type
{
426 extern const struct nft_set_ext_type nft_set_ext_types
[];
429 * struct nft_set_ext_tmpl - set extension template
431 * @len: length of extension area
432 * @offset: offsets of individual extension types
434 struct nft_set_ext_tmpl
{
436 u8 offset
[NFT_SET_EXT_NUM
];
440 * struct nft_set_ext - set extensions
442 * @genmask: generation mask
443 * @offset: offsets of individual extension types
444 * @data: beginning of extension data
448 u8 offset
[NFT_SET_EXT_NUM
];
452 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
454 memset(tmpl
, 0, sizeof(*tmpl
));
455 tmpl
->len
= sizeof(struct nft_set_ext
);
458 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
461 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
462 BUG_ON(tmpl
->len
> U8_MAX
);
463 tmpl
->offset
[id
] = tmpl
->len
;
464 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
467 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
469 nft_set_ext_add_length(tmpl
, id
, 0);
472 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
473 const struct nft_set_ext_tmpl
*tmpl
)
475 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
478 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
480 return !!ext
->offset
[id
];
483 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
485 return ext
&& __nft_set_ext_exists(ext
, id
);
488 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
490 return (void *)ext
+ ext
->offset
[id
];
493 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
495 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
498 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
500 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
503 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
505 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
508 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
510 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
513 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
515 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
518 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
520 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
523 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
525 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
528 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
530 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
531 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
534 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
537 return elem
+ set
->ops
->elemsize
;
540 void *nft_set_elem_init(const struct nft_set
*set
,
541 const struct nft_set_ext_tmpl
*tmpl
,
542 const u32
*key
, const u32
*data
,
543 u64 timeout
, gfp_t gfp
);
544 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
);
547 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
550 * @set: set the elements belong to
551 * @cnt: count of elements
553 struct nft_set_gc_batch_head
{
555 const struct nft_set
*set
;
559 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
560 sizeof(struct nft_set_gc_batch_head)) / \
564 * struct nft_set_gc_batch - nf_tables set garbage collection batch
566 * @head: GC batch head
567 * @elems: garbage collection elements
569 struct nft_set_gc_batch
{
570 struct nft_set_gc_batch_head head
;
571 void *elems
[NFT_SET_GC_BATCH_SIZE
];
574 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
576 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
578 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
581 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
584 static inline struct nft_set_gc_batch
*
585 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
589 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
591 nft_set_gc_batch_complete(gcb
);
593 return nft_set_gc_batch_alloc(set
, gfp
);
596 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
599 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
603 * struct nft_expr_type - nf_tables expression type
605 * @select_ops: function to select nft_expr_ops
606 * @ops: default ops, used when no select_ops functions is present
607 * @list: used internally
609 * @owner: module reference
610 * @policy: netlink attribute policy
611 * @maxattr: highest netlink attribute number
612 * @family: address family for AF-specific types
613 * @flags: expression type flags
615 struct nft_expr_type
{
616 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
617 const struct nlattr
* const tb
[]);
618 const struct nft_expr_ops
*ops
;
619 struct list_head list
;
621 struct module
*owner
;
622 const struct nla_policy
*policy
;
623 unsigned int maxattr
;
628 #define NFT_EXPR_STATEFUL 0x1
631 * struct nft_expr_ops - nf_tables expression operations
633 * @eval: Expression evaluation function
634 * @size: full expression size, including private data size
635 * @init: initialization function
636 * @destroy: destruction function
637 * @dump: function to dump parameters
638 * @type: expression type
639 * @validate: validate expression, called during loop detection
640 * @data: extra data to attach to this expression operation
643 struct nft_expr_ops
{
644 void (*eval
)(const struct nft_expr
*expr
,
645 struct nft_regs
*regs
,
646 const struct nft_pktinfo
*pkt
);
647 int (*clone
)(struct nft_expr
*dst
,
648 const struct nft_expr
*src
);
651 int (*init
)(const struct nft_ctx
*ctx
,
652 const struct nft_expr
*expr
,
653 const struct nlattr
* const tb
[]);
654 void (*destroy
)(const struct nft_ctx
*ctx
,
655 const struct nft_expr
*expr
);
656 int (*dump
)(struct sk_buff
*skb
,
657 const struct nft_expr
*expr
);
658 int (*validate
)(const struct nft_ctx
*ctx
,
659 const struct nft_expr
*expr
,
660 const struct nft_data
**data
);
661 const struct nft_expr_type
*type
;
665 #define NFT_EXPR_MAXATTR 16
666 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
667 ALIGN(size, __alignof__(struct nft_expr)))
670 * struct nft_expr - nf_tables expression
672 * @ops: expression ops
673 * @data: expression private data
676 const struct nft_expr_ops
*ops
;
677 unsigned char data
[];
680 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
682 return (void *)expr
->data
;
685 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
686 const struct nlattr
*nla
);
687 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
688 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
689 const struct nft_expr
*expr
);
691 static inline int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
695 __module_get(src
->ops
->type
->owner
);
696 if (src
->ops
->clone
) {
698 err
= src
->ops
->clone(dst
, src
);
702 memcpy(dst
, src
, src
->ops
->size
);
708 * struct nft_rule - nf_tables rule
710 * @list: used internally
711 * @handle: rule handle
712 * @genmask: generation mask
713 * @dlen: length of expression data
714 * @udata: user data is appended to the rule
715 * @data: expression data
718 struct list_head list
;
724 __attribute__((aligned(__alignof__(struct nft_expr
))));
727 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
729 return (struct nft_expr
*)&rule
->data
[0];
732 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
734 return ((void *)expr
) + expr
->ops
->size
;
737 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
739 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
742 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
744 return (void *)&rule
->data
[rule
->dlen
];
748 * The last pointer isn't really necessary, but the compiler isn't able to
749 * determine that the result of nft_expr_last() is always the same since it
750 * can't assume that the dlen value wasn't changed within calls in the loop.
752 #define nft_rule_for_each_expr(expr, last, rule) \
753 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
755 (expr) = nft_expr_next(expr))
757 enum nft_chain_flags
{
758 NFT_BASE_CHAIN
= 0x1,
762 * struct nft_chain - nf_tables chain
764 * @rules: list of rules in the chain
765 * @list: used internally
766 * @table: table that this chain belongs to
767 * @handle: chain handle
768 * @use: number of jump references to this chain
769 * @level: length of longest path to this chain
770 * @flags: bitmask of enum nft_chain_flags
771 * @name: name of the chain
774 struct list_head rules
;
775 struct list_head list
;
776 struct nft_table
*table
;
782 char name
[NFT_CHAIN_MAXNAMELEN
];
785 enum nft_chain_type
{
786 NFT_CHAIN_T_DEFAULT
= 0,
793 * struct nf_chain_type - nf_tables chain type info
795 * @name: name of the type
796 * @type: numeric identifier
797 * @family: address family
798 * @owner: module owner
799 * @hook_mask: mask of valid hooks
800 * @hooks: hookfn overrides
802 struct nf_chain_type
{
804 enum nft_chain_type type
;
806 struct module
*owner
;
807 unsigned int hook_mask
;
808 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
811 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
812 enum nft_chain_type type
);
813 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
814 unsigned int hook_flags
);
819 struct u64_stats_sync syncp
;
822 #define NFT_HOOK_OPS_MAX 2
825 * struct nft_base_chain - nf_tables base chain
827 * @ops: netfilter hook ops
829 * @policy: default policy
830 * @stats: per-cpu chain stats
832 * @dev_name: device name that this base chain is attached to (if any)
834 struct nft_base_chain
{
835 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
836 const struct nf_chain_type
*type
;
839 struct nft_stats __percpu
*stats
;
840 struct nft_chain chain
;
841 char dev_name
[IFNAMSIZ
];
844 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
846 return container_of(chain
, struct nft_base_chain
, chain
);
849 int __nft_release_basechain(struct nft_ctx
*ctx
);
851 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
, void *priv
);
854 * struct nft_table - nf_tables table
856 * @list: used internally
857 * @chains: chains in the table
858 * @sets: sets in the table
859 * @hgenerator: handle generator state
860 * @use: number of chain references to this table
861 * @flags: table flag (see enum nft_table_flags)
862 * @genmask: generation mask
863 * @name: name of the table
866 struct list_head list
;
867 struct list_head chains
;
868 struct list_head sets
;
873 char name
[NFT_TABLE_MAXNAMELEN
];
877 NFT_AF_NEEDS_DEV
= (1 << 0),
881 * struct nft_af_info - nf_tables address family info
883 * @list: used internally
884 * @family: address family
885 * @nhooks: number of hooks in this family
886 * @owner: module owner
887 * @tables: used internally
888 * @flags: family flags
889 * @nops: number of hook ops in this family
890 * @hook_ops_init: initialization function for chain hook ops
891 * @hooks: hookfn overrides for packet validation
894 struct list_head list
;
897 struct module
*owner
;
898 struct list_head tables
;
901 void (*hook_ops_init
)(struct nf_hook_ops
*,
903 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
906 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
907 void nft_unregister_afinfo(struct net
*, struct nft_af_info
*);
909 int nft_register_chain_type(const struct nf_chain_type
*);
910 void nft_unregister_chain_type(const struct nf_chain_type
*);
912 int nft_register_expr(struct nft_expr_type
*);
913 void nft_unregister_expr(struct nft_expr_type
*);
915 int nft_verdict_dump(struct sk_buff
*skb
, int type
,
916 const struct nft_verdict
*v
);
919 * struct nft_traceinfo - nft tracing information and state
921 * @pkt: pktinfo currently processed
922 * @basechain: base chain currently processed
923 * @chain: chain currently processed
924 * @rule: rule that was evaluated
925 * @verdict: verdict given by rule
926 * @type: event type (enum nft_trace_types)
927 * @packet_dumped: packet headers sent in a previous traceinfo message
928 * @trace: other struct members are initialised
930 struct nft_traceinfo
{
931 const struct nft_pktinfo
*pkt
;
932 const struct nft_base_chain
*basechain
;
933 const struct nft_chain
*chain
;
934 const struct nft_rule
*rule
;
935 const struct nft_verdict
*verdict
;
936 enum nft_trace_types type
;
941 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
942 const struct nft_verdict
*verdict
,
943 const struct nft_chain
*basechain
);
945 void nft_trace_notify(struct nft_traceinfo
*info
);
947 #define nft_dereference(p) \
948 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
950 #define MODULE_ALIAS_NFT_FAMILY(family) \
951 MODULE_ALIAS("nft-afinfo-" __stringify(family))
953 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
954 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
956 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
957 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
959 #define MODULE_ALIAS_NFT_EXPR(name) \
960 MODULE_ALIAS("nft-expr-" name)
962 #define MODULE_ALIAS_NFT_SET() \
963 MODULE_ALIAS("nft-set")
966 * The gencursor defines two generations, the currently active and the
967 * next one. Objects contain a bitmask of 2 bits specifying the generations
968 * they're active in. A set bit means they're inactive in the generation
969 * represented by that bit.
971 * New objects start out as inactive in the current and active in the
972 * next generation. When committing the ruleset the bitmask is cleared,
973 * meaning they're active in all generations. When removing an object,
974 * it is set inactive in the next generation. After committing the ruleset,
975 * the objects are removed.
977 static inline unsigned int nft_gencursor_next(const struct net
*net
)
979 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
982 static inline u8
nft_genmask_next(const struct net
*net
)
984 return 1 << nft_gencursor_next(net
);
987 static inline u8
nft_genmask_cur(const struct net
*net
)
989 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
990 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
993 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
996 * Generic transaction helpers
999 /* Check if this object is currently active. */
1000 #define nft_is_active(__net, __obj) \
1001 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1003 /* Check if this object is active in the next generation. */
1004 #define nft_is_active_next(__net, __obj) \
1005 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1007 /* This object becomes active in the next generation. */
1008 #define nft_activate_next(__net, __obj) \
1009 (__obj)->genmask = nft_genmask_cur(__net)
1011 /* This object becomes inactive in the next generation. */
1012 #define nft_deactivate_next(__net, __obj) \
1013 (__obj)->genmask = nft_genmask_next(__net)
1015 /* After committing the ruleset, clear the stale generation bit. */
1016 #define nft_clear(__net, __obj) \
1017 (__obj)->genmask &= ~nft_genmask_next(__net)
1018 #define nft_active_genmask(__obj, __genmask) \
1019 !((__obj)->genmask & __genmask)
1022 * Set element transaction helpers
1025 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
1028 return !(ext
->genmask
& genmask
);
1031 static inline void nft_set_elem_change_active(const struct net
*net
,
1032 const struct nft_set
*set
,
1033 struct nft_set_ext
*ext
)
1035 ext
->genmask
^= nft_genmask_next(net
);
1039 * We use a free bit in the genmask field to indicate the element
1040 * is busy, meaning it is currently being processed either by
1041 * the netlink API or GC.
1043 * Even though the genmask is only a single byte wide, this works
1044 * because the extension structure if fully constant once initialized,
1045 * so there are no non-atomic write accesses unless it is already
1048 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1050 #if defined(__LITTLE_ENDIAN_BITFIELD)
1051 #define NFT_SET_ELEM_BUSY_BIT 2
1052 #elif defined(__BIG_ENDIAN_BITFIELD)
1053 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1058 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
1060 unsigned long *word
= (unsigned long *)ext
;
1062 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
1063 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1066 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
1068 unsigned long *word
= (unsigned long *)ext
;
1070 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1074 * struct nft_trans - nf_tables object update in transaction
1076 * @list: used internally
1077 * @msg_type: message type
1078 * @ctx: transaction context
1079 * @data: internal information related to the transaction
1082 struct list_head list
;
1088 struct nft_trans_rule
{
1089 struct nft_rule
*rule
;
1092 #define nft_trans_rule(trans) \
1093 (((struct nft_trans_rule *)trans->data)->rule)
1095 struct nft_trans_set
{
1096 struct nft_set
*set
;
1100 #define nft_trans_set(trans) \
1101 (((struct nft_trans_set *)trans->data)->set)
1102 #define nft_trans_set_id(trans) \
1103 (((struct nft_trans_set *)trans->data)->set_id)
1105 struct nft_trans_chain
{
1107 char name
[NFT_CHAIN_MAXNAMELEN
];
1108 struct nft_stats __percpu
*stats
;
1112 #define nft_trans_chain_update(trans) \
1113 (((struct nft_trans_chain *)trans->data)->update)
1114 #define nft_trans_chain_name(trans) \
1115 (((struct nft_trans_chain *)trans->data)->name)
1116 #define nft_trans_chain_stats(trans) \
1117 (((struct nft_trans_chain *)trans->data)->stats)
1118 #define nft_trans_chain_policy(trans) \
1119 (((struct nft_trans_chain *)trans->data)->policy)
1121 struct nft_trans_table
{
1126 #define nft_trans_table_update(trans) \
1127 (((struct nft_trans_table *)trans->data)->update)
1128 #define nft_trans_table_enable(trans) \
1129 (((struct nft_trans_table *)trans->data)->enable)
1131 struct nft_trans_elem
{
1132 struct nft_set
*set
;
1133 struct nft_set_elem elem
;
1136 #define nft_trans_elem_set(trans) \
1137 (((struct nft_trans_elem *)trans->data)->set)
1138 #define nft_trans_elem(trans) \
1139 (((struct nft_trans_elem *)trans->data)->elem)
1141 #endif /* _NET_NF_TABLES_H */