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
19 /* for x_tables compatibility */
20 struct xt_action_param xt
;
23 static inline struct net
*nft_net(const struct nft_pktinfo
*pkt
)
25 return pkt
->xt
.state
->net
;
28 static inline unsigned int nft_hook(const struct nft_pktinfo
*pkt
)
30 return pkt
->xt
.state
->hook
;
33 static inline u8
nft_pf(const struct nft_pktinfo
*pkt
)
35 return pkt
->xt
.state
->pf
;
38 static inline const struct net_device
*nft_in(const struct nft_pktinfo
*pkt
)
40 return pkt
->xt
.state
->in
;
43 static inline const struct net_device
*nft_out(const struct nft_pktinfo
*pkt
)
45 return pkt
->xt
.state
->out
;
48 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
50 const struct nf_hook_state
*state
)
53 pkt
->xt
.state
= state
;
56 static inline void nft_set_pktinfo_proto_unspec(struct nft_pktinfo
*pkt
,
59 pkt
->tprot_set
= false;
65 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo
*pkt
,
67 const struct nf_hook_state
*state
)
69 nft_set_pktinfo(pkt
, skb
, state
);
70 nft_set_pktinfo_proto_unspec(pkt
, skb
);
74 * struct nft_verdict - nf_tables verdict
76 * @code: nf_tables/netfilter verdict code
77 * @chain: destination chain for NFT_JUMP/NFT_GOTO
81 struct nft_chain
*chain
;
87 struct nft_verdict verdict
;
89 } __attribute__((aligned(__alignof__(u64
))));
92 * struct nft_regs - nf_tables register set
94 * @data: data registers
95 * @verdict: verdict register
97 * The first four data registers alias to the verdict register.
102 struct nft_verdict verdict
;
106 static inline void nft_data_copy(u32
*dst
, const struct nft_data
*src
,
109 memcpy(dst
, src
, len
);
112 static inline void nft_data_debug(const struct nft_data
*data
)
114 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
115 data
->data
[0], data
->data
[1],
116 data
->data
[2], data
->data
[3]);
120 * struct nft_ctx - nf_tables rule/set context
122 * @net: net namespace
123 * @afi: address family info
124 * @table: the table the chain is contained in
125 * @chain: the chain the rule is contained in
126 * @nla: netlink attributes
127 * @portid: netlink portID of the original message
128 * @seq: netlink sequence number
129 * @report: notify via unicast netlink message
133 struct nft_af_info
*afi
;
134 struct nft_table
*table
;
135 struct nft_chain
*chain
;
136 const struct nlattr
* const *nla
;
142 struct nft_data_desc
{
143 enum nft_data_types type
;
147 int nft_data_init(const struct nft_ctx
*ctx
,
148 struct nft_data
*data
, unsigned int size
,
149 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
150 void nft_data_uninit(const struct nft_data
*data
, enum nft_data_types type
);
151 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
152 enum nft_data_types type
, unsigned int len
);
154 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
156 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
159 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
161 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
164 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
);
165 unsigned int nft_parse_register(const struct nlattr
*attr
);
166 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
168 int nft_validate_register_load(enum nft_registers reg
, unsigned int len
);
169 int nft_validate_register_store(const struct nft_ctx
*ctx
,
170 enum nft_registers reg
,
171 const struct nft_data
*data
,
172 enum nft_data_types type
, unsigned int len
);
175 * struct nft_userdata - user defined data associated with an object
177 * @len: length of the data
180 * The presence of user data is indicated in an object specific fashion,
181 * so a length of zero can't occur and the value "len" indicates data
184 struct nft_userdata
{
186 unsigned char data
[0];
190 * struct nft_set_elem - generic representation of set elements
193 * @priv: element private data and extensions
195 struct nft_set_elem
{
197 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
204 struct nft_set_iter
{
209 int (*fn
)(const struct nft_ctx
*ctx
,
211 const struct nft_set_iter
*iter
,
212 struct nft_set_elem
*elem
);
216 * struct nft_set_desc - description of set elements
220 * @size: number of set elements
222 struct nft_set_desc
{
229 * enum nft_set_class - performance class
231 * @NFT_LOOKUP_O_1: constant, O(1)
232 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
233 * @NFT_LOOKUP_O_N: linear, O(N)
237 NFT_SET_CLASS_O_LOG_N
,
242 * struct nft_set_estimate - estimation of memory and performance
245 * @size: required memory
246 * @class: lookup performance class
248 struct nft_set_estimate
{
250 enum nft_set_class
class;
257 * struct nft_set_ops - nf_tables set operations
259 * @lookup: look up an element within the set
260 * @insert: insert new element into set
261 * @activate: activate new element in the next generation
262 * @deactivate: lookup for element and deactivate it in the next generation
263 * @deactivate_one: deactivate element in the next generation
264 * @remove: remove element from set
265 * @walk: iterate over all set elemeennts
266 * @privsize: function to return size of set private data
267 * @init: initialize private data of new set instance
268 * @destroy: destroy private data of set instance
269 * @list: nf_tables_set_ops list node
270 * @owner: module reference
271 * @elemsize: element private size
272 * @features: features supported by the implementation
275 bool (*lookup
)(const struct net
*net
,
276 const struct nft_set
*set
,
278 const struct nft_set_ext
**ext
);
279 bool (*update
)(struct nft_set
*set
,
281 void *(*new)(struct nft_set
*,
282 const struct nft_expr
*,
284 const struct nft_expr
*expr
,
285 struct nft_regs
*regs
,
286 const struct nft_set_ext
**ext
);
288 int (*insert
)(const struct net
*net
,
289 const struct nft_set
*set
,
290 const struct nft_set_elem
*elem
,
291 struct nft_set_ext
**ext
);
292 void (*activate
)(const struct net
*net
,
293 const struct nft_set
*set
,
294 const struct nft_set_elem
*elem
);
295 void * (*deactivate
)(const struct net
*net
,
296 const struct nft_set
*set
,
297 const struct nft_set_elem
*elem
);
298 bool (*deactivate_one
)(const struct net
*net
,
299 const struct nft_set
*set
,
301 void (*remove
)(const struct net
*net
,
302 const struct nft_set
*set
,
303 const struct nft_set_elem
*elem
);
304 void (*walk
)(const struct nft_ctx
*ctx
,
306 struct nft_set_iter
*iter
);
308 unsigned int (*privsize
)(const struct nlattr
* const nla
[]);
309 bool (*estimate
)(const struct nft_set_desc
*desc
,
311 struct nft_set_estimate
*est
);
312 int (*init
)(const struct nft_set
*set
,
313 const struct nft_set_desc
*desc
,
314 const struct nlattr
* const nla
[]);
315 void (*destroy
)(const struct nft_set
*set
);
317 struct list_head list
;
318 struct module
*owner
;
319 unsigned int elemsize
;
323 int nft_register_set(struct nft_set_ops
*ops
);
324 void nft_unregister_set(struct nft_set_ops
*ops
);
327 * struct nft_set - nf_tables set instance
329 * @list: table set list node
330 * @bindings: list of set bindings
331 * @name: name of the set
332 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
333 * @dtype: data type (verdict or numeric type defined by userspace)
334 * @objtype: object type (see NFT_OBJECT_* definitions)
335 * @size: maximum set size
336 * @nelems: number of elements
337 * @ndeact: number of deactivated elements queued for removal
338 * @timeout: default timeout value in jiffies
339 * @gc_int: garbage collection interval in msecs
340 * @policy: set parameterization (see enum nft_set_policies)
341 * @udlen: user data length
345 * @genmask: generation mask
348 * @data: private set data
351 struct list_head list
;
352 struct list_head bindings
;
353 char name
[NFT_SET_MAXNAMELEN
];
364 unsigned char *udata
;
365 /* runtime data below here */
366 const struct nft_set_ops
*ops ____cacheline_aligned
;
372 __attribute__((aligned(__alignof__(u64
))));
375 static inline void *nft_set_priv(const struct nft_set
*set
)
377 return (void *)set
->data
;
380 static inline struct nft_set
*nft_set_container_of(const void *priv
)
382 return (void *)priv
- offsetof(struct nft_set
, data
);
385 struct nft_set
*nf_tables_set_lookup(const struct nft_table
*table
,
386 const struct nlattr
*nla
, u8 genmask
);
387 struct nft_set
*nf_tables_set_lookup_byid(const struct net
*net
,
388 const struct nlattr
*nla
, u8 genmask
);
390 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
392 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
396 * struct nft_set_binding - nf_tables set binding
398 * @list: set bindings list node
399 * @chain: chain containing the rule bound to the set
400 * @flags: set action flags
402 * A set binding contains all information necessary for validation
403 * of new elements added to a bound set.
405 struct nft_set_binding
{
406 struct list_head list
;
407 const struct nft_chain
*chain
;
411 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
412 struct nft_set_binding
*binding
);
413 void nf_tables_unbind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
414 struct nft_set_binding
*binding
);
417 * enum nft_set_extensions - set extension type IDs
419 * @NFT_SET_EXT_KEY: element key
420 * @NFT_SET_EXT_DATA: mapping data
421 * @NFT_SET_EXT_FLAGS: element flags
422 * @NFT_SET_EXT_TIMEOUT: element timeout
423 * @NFT_SET_EXT_EXPIRATION: element expiration time
424 * @NFT_SET_EXT_USERDATA: user data associated with the element
425 * @NFT_SET_EXT_EXPR: expression assiociated with the element
426 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
427 * @NFT_SET_EXT_NUM: number of extension types
429 enum nft_set_extensions
{
434 NFT_SET_EXT_EXPIRATION
,
435 NFT_SET_EXT_USERDATA
,
442 * struct nft_set_ext_type - set extension type
444 * @len: fixed part length of the extension
445 * @align: alignment requirements of the extension
447 struct nft_set_ext_type
{
452 extern const struct nft_set_ext_type nft_set_ext_types
[];
455 * struct nft_set_ext_tmpl - set extension template
457 * @len: length of extension area
458 * @offset: offsets of individual extension types
460 struct nft_set_ext_tmpl
{
462 u8 offset
[NFT_SET_EXT_NUM
];
466 * struct nft_set_ext - set extensions
468 * @genmask: generation mask
469 * @offset: offsets of individual extension types
470 * @data: beginning of extension data
474 u8 offset
[NFT_SET_EXT_NUM
];
478 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
480 memset(tmpl
, 0, sizeof(*tmpl
));
481 tmpl
->len
= sizeof(struct nft_set_ext
);
484 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
487 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
488 BUG_ON(tmpl
->len
> U8_MAX
);
489 tmpl
->offset
[id
] = tmpl
->len
;
490 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
493 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
495 nft_set_ext_add_length(tmpl
, id
, 0);
498 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
499 const struct nft_set_ext_tmpl
*tmpl
)
501 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
504 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
506 return !!ext
->offset
[id
];
509 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
511 return ext
&& __nft_set_ext_exists(ext
, id
);
514 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
516 return (void *)ext
+ ext
->offset
[id
];
519 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
521 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
524 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
526 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
529 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
531 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
534 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
536 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
539 static inline unsigned long *nft_set_ext_expiration(const struct nft_set_ext
*ext
)
541 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
544 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
546 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
549 static inline struct nft_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
551 return nft_set_ext(ext
, NFT_SET_EXT_EXPR
);
554 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
556 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
557 time_is_before_eq_jiffies(*nft_set_ext_expiration(ext
));
560 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
563 return elem
+ set
->ops
->elemsize
;
566 static inline struct nft_object
**nft_set_ext_obj(const struct nft_set_ext
*ext
)
568 return nft_set_ext(ext
, NFT_SET_EXT_OBJREF
);
571 void *nft_set_elem_init(const struct nft_set
*set
,
572 const struct nft_set_ext_tmpl
*tmpl
,
573 const u32
*key
, const u32
*data
,
574 u64 timeout
, gfp_t gfp
);
575 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
579 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
582 * @set: set the elements belong to
583 * @cnt: count of elements
585 struct nft_set_gc_batch_head
{
587 const struct nft_set
*set
;
591 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
592 sizeof(struct nft_set_gc_batch_head)) / \
596 * struct nft_set_gc_batch - nf_tables set garbage collection batch
598 * @head: GC batch head
599 * @elems: garbage collection elements
601 struct nft_set_gc_batch
{
602 struct nft_set_gc_batch_head head
;
603 void *elems
[NFT_SET_GC_BATCH_SIZE
];
606 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
608 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
610 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
613 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
616 static inline struct nft_set_gc_batch
*
617 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
621 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
623 nft_set_gc_batch_complete(gcb
);
625 return nft_set_gc_batch_alloc(set
, gfp
);
628 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
631 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
635 * struct nft_expr_type - nf_tables expression type
637 * @select_ops: function to select nft_expr_ops
638 * @ops: default ops, used when no select_ops functions is present
639 * @list: used internally
641 * @owner: module reference
642 * @policy: netlink attribute policy
643 * @maxattr: highest netlink attribute number
644 * @family: address family for AF-specific types
645 * @flags: expression type flags
647 struct nft_expr_type
{
648 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
649 const struct nlattr
* const tb
[]);
650 const struct nft_expr_ops
*ops
;
651 struct list_head list
;
653 struct module
*owner
;
654 const struct nla_policy
*policy
;
655 unsigned int maxattr
;
660 #define NFT_EXPR_STATEFUL 0x1
663 * struct nft_expr_ops - nf_tables expression operations
665 * @eval: Expression evaluation function
666 * @size: full expression size, including private data size
667 * @init: initialization function
668 * @destroy: destruction function
669 * @dump: function to dump parameters
670 * @type: expression type
671 * @validate: validate expression, called during loop detection
672 * @data: extra data to attach to this expression operation
675 struct nft_expr_ops
{
676 void (*eval
)(const struct nft_expr
*expr
,
677 struct nft_regs
*regs
,
678 const struct nft_pktinfo
*pkt
);
679 int (*clone
)(struct nft_expr
*dst
,
680 const struct nft_expr
*src
);
683 int (*init
)(const struct nft_ctx
*ctx
,
684 const struct nft_expr
*expr
,
685 const struct nlattr
* const tb
[]);
686 void (*destroy
)(const struct nft_ctx
*ctx
,
687 const struct nft_expr
*expr
);
688 int (*dump
)(struct sk_buff
*skb
,
689 const struct nft_expr
*expr
);
690 int (*validate
)(const struct nft_ctx
*ctx
,
691 const struct nft_expr
*expr
,
692 const struct nft_data
**data
);
693 const struct nft_expr_type
*type
;
697 #define NFT_EXPR_MAXATTR 16
698 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
699 ALIGN(size, __alignof__(struct nft_expr)))
702 * struct nft_expr - nf_tables expression
704 * @ops: expression ops
705 * @data: expression private data
708 const struct nft_expr_ops
*ops
;
709 unsigned char data
[];
712 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
714 return (void *)expr
->data
;
717 struct nft_expr
*nft_expr_init(const struct nft_ctx
*ctx
,
718 const struct nlattr
*nla
);
719 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
720 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
721 const struct nft_expr
*expr
);
723 static inline int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
)
727 if (src
->ops
->clone
) {
729 err
= src
->ops
->clone(dst
, src
);
733 memcpy(dst
, src
, src
->ops
->size
);
736 __module_get(src
->ops
->type
->owner
);
741 * struct nft_rule - nf_tables rule
743 * @list: used internally
744 * @handle: rule handle
745 * @genmask: generation mask
746 * @dlen: length of expression data
747 * @udata: user data is appended to the rule
748 * @data: expression data
751 struct list_head list
;
757 __attribute__((aligned(__alignof__(struct nft_expr
))));
760 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
762 return (struct nft_expr
*)&rule
->data
[0];
765 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
767 return ((void *)expr
) + expr
->ops
->size
;
770 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
772 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
775 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
777 return (void *)&rule
->data
[rule
->dlen
];
781 * The last pointer isn't really necessary, but the compiler isn't able to
782 * determine that the result of nft_expr_last() is always the same since it
783 * can't assume that the dlen value wasn't changed within calls in the loop.
785 #define nft_rule_for_each_expr(expr, last, rule) \
786 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
788 (expr) = nft_expr_next(expr))
790 enum nft_chain_flags
{
791 NFT_BASE_CHAIN
= 0x1,
795 * struct nft_chain - nf_tables chain
797 * @rules: list of rules in the chain
798 * @list: used internally
799 * @table: table that this chain belongs to
800 * @handle: chain handle
801 * @use: number of jump references to this chain
802 * @level: length of longest path to this chain
803 * @flags: bitmask of enum nft_chain_flags
804 * @name: name of the chain
807 struct list_head rules
;
808 struct list_head list
;
809 struct nft_table
*table
;
815 char name
[NFT_CHAIN_MAXNAMELEN
];
818 enum nft_chain_type
{
819 NFT_CHAIN_T_DEFAULT
= 0,
826 * struct nf_chain_type - nf_tables chain type info
828 * @name: name of the type
829 * @type: numeric identifier
830 * @family: address family
831 * @owner: module owner
832 * @hook_mask: mask of valid hooks
833 * @hooks: hookfn overrides
835 struct nf_chain_type
{
837 enum nft_chain_type type
;
839 struct module
*owner
;
840 unsigned int hook_mask
;
841 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
844 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
845 enum nft_chain_type type
);
846 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
847 unsigned int hook_flags
);
852 struct u64_stats_sync syncp
;
855 #define NFT_HOOK_OPS_MAX 2
858 * struct nft_base_chain - nf_tables base chain
860 * @ops: netfilter hook ops
862 * @policy: default policy
863 * @stats: per-cpu chain stats
865 * @dev_name: device name that this base chain is attached to (if any)
867 struct nft_base_chain
{
868 struct nf_hook_ops ops
[NFT_HOOK_OPS_MAX
];
869 const struct nf_chain_type
*type
;
872 struct nft_stats __percpu
*stats
;
873 struct nft_chain chain
;
874 char dev_name
[IFNAMSIZ
];
877 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
879 return container_of(chain
, struct nft_base_chain
, chain
);
882 int __nft_release_basechain(struct nft_ctx
*ctx
);
884 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
, void *priv
);
887 * struct nft_table - nf_tables table
889 * @list: used internally
890 * @chains: chains in the table
891 * @sets: sets in the table
892 * @objects: stateful objects in the table
893 * @hgenerator: handle generator state
894 * @use: number of chain references to this table
895 * @flags: table flag (see enum nft_table_flags)
896 * @genmask: generation mask
897 * @name: name of the table
900 struct list_head list
;
901 struct list_head chains
;
902 struct list_head sets
;
903 struct list_head objects
;
908 char name
[NFT_TABLE_MAXNAMELEN
];
912 NFT_AF_NEEDS_DEV
= (1 << 0),
916 * struct nft_af_info - nf_tables address family info
918 * @list: used internally
919 * @family: address family
920 * @nhooks: number of hooks in this family
921 * @owner: module owner
922 * @tables: used internally
923 * @flags: family flags
924 * @nops: number of hook ops in this family
925 * @hook_ops_init: initialization function for chain hook ops
926 * @hooks: hookfn overrides for packet validation
929 struct list_head list
;
932 struct module
*owner
;
933 struct list_head tables
;
936 void (*hook_ops_init
)(struct nf_hook_ops
*,
938 nf_hookfn
*hooks
[NF_MAX_HOOKS
];
941 int nft_register_afinfo(struct net
*, struct nft_af_info
*);
942 void nft_unregister_afinfo(struct net
*, struct nft_af_info
*);
944 int nft_register_chain_type(const struct nf_chain_type
*);
945 void nft_unregister_chain_type(const struct nf_chain_type
*);
947 int nft_register_expr(struct nft_expr_type
*);
948 void nft_unregister_expr(struct nft_expr_type
*);
950 int nft_verdict_dump(struct sk_buff
*skb
, int type
,
951 const struct nft_verdict
*v
);
954 * struct nft_object - nf_tables stateful object
956 * @list: table stateful object list node
957 * @table: table this object belongs to
958 * @type: pointer to object type
959 * @data: pointer to object data
960 * @name: name of this stateful object
961 * @genmask: generation mask
962 * @use: number of references to this stateful object
963 * @data: object data, layout depends on type
966 struct list_head list
;
967 char name
[NFT_OBJ_MAXNAMELEN
];
968 struct nft_table
*table
;
971 /* runtime data below here */
972 const struct nft_object_type
*type ____cacheline_aligned
;
974 __attribute__((aligned(__alignof__(u64
))));
977 static inline void *nft_obj_data(const struct nft_object
*obj
)
979 return (void *)obj
->data
;
982 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
984 struct nft_object
*nf_tables_obj_lookup(const struct nft_table
*table
,
985 const struct nlattr
*nla
, u32 objtype
,
988 int nft_obj_notify(struct net
*net
, struct nft_table
*table
,
989 struct nft_object
*obj
, u32 portid
, u32 seq
,
990 int event
, int family
, int report
, gfp_t gfp
);
993 * struct nft_object_type - stateful object type
995 * @eval: stateful object evaluation function
996 * @list: list node in list of object types
997 * @type: stateful object numeric type
998 * @size: stateful object size
999 * @owner: module owner
1000 * @maxattr: maximum netlink attribute
1001 * @policy: netlink attribute policy
1002 * @init: initialize object from netlink attributes
1003 * @destroy: release existing stateful object
1004 * @dump: netlink dump stateful object
1006 struct nft_object_type
{
1007 void (*eval
)(struct nft_object
*obj
,
1008 struct nft_regs
*regs
,
1009 const struct nft_pktinfo
*pkt
);
1010 struct list_head list
;
1013 unsigned int maxattr
;
1014 struct module
*owner
;
1015 const struct nla_policy
*policy
;
1016 int (*init
)(const struct nlattr
* const tb
[],
1017 struct nft_object
*obj
);
1018 void (*destroy
)(struct nft_object
*obj
);
1019 int (*dump
)(struct sk_buff
*skb
,
1020 struct nft_object
*obj
,
1024 int nft_register_obj(struct nft_object_type
*obj_type
);
1025 void nft_unregister_obj(struct nft_object_type
*obj_type
);
1028 * struct nft_traceinfo - nft tracing information and state
1030 * @pkt: pktinfo currently processed
1031 * @basechain: base chain currently processed
1032 * @chain: chain currently processed
1033 * @rule: rule that was evaluated
1034 * @verdict: verdict given by rule
1035 * @type: event type (enum nft_trace_types)
1036 * @packet_dumped: packet headers sent in a previous traceinfo message
1037 * @trace: other struct members are initialised
1039 struct nft_traceinfo
{
1040 const struct nft_pktinfo
*pkt
;
1041 const struct nft_base_chain
*basechain
;
1042 const struct nft_chain
*chain
;
1043 const struct nft_rule
*rule
;
1044 const struct nft_verdict
*verdict
;
1045 enum nft_trace_types type
;
1050 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
1051 const struct nft_verdict
*verdict
,
1052 const struct nft_chain
*basechain
);
1054 void nft_trace_notify(struct nft_traceinfo
*info
);
1056 #define nft_dereference(p) \
1057 nfnl_dereference(p, NFNL_SUBSYS_NFTABLES)
1059 #define MODULE_ALIAS_NFT_FAMILY(family) \
1060 MODULE_ALIAS("nft-afinfo-" __stringify(family))
1062 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1063 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1065 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1066 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1068 #define MODULE_ALIAS_NFT_EXPR(name) \
1069 MODULE_ALIAS("nft-expr-" name)
1071 #define MODULE_ALIAS_NFT_SET() \
1072 MODULE_ALIAS("nft-set")
1074 #define MODULE_ALIAS_NFT_OBJ(type) \
1075 MODULE_ALIAS("nft-obj-" __stringify(type))
1078 * The gencursor defines two generations, the currently active and the
1079 * next one. Objects contain a bitmask of 2 bits specifying the generations
1080 * they're active in. A set bit means they're inactive in the generation
1081 * represented by that bit.
1083 * New objects start out as inactive in the current and active in the
1084 * next generation. When committing the ruleset the bitmask is cleared,
1085 * meaning they're active in all generations. When removing an object,
1086 * it is set inactive in the next generation. After committing the ruleset,
1087 * the objects are removed.
1089 static inline unsigned int nft_gencursor_next(const struct net
*net
)
1091 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
1094 static inline u8
nft_genmask_next(const struct net
*net
)
1096 return 1 << nft_gencursor_next(net
);
1099 static inline u8
nft_genmask_cur(const struct net
*net
)
1101 /* Use ACCESS_ONCE() to prevent refetching the value for atomicity */
1102 return 1 << ACCESS_ONCE(net
->nft
.gencursor
);
1105 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1108 * Generic transaction helpers
1111 /* Check if this object is currently active. */
1112 #define nft_is_active(__net, __obj) \
1113 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1115 /* Check if this object is active in the next generation. */
1116 #define nft_is_active_next(__net, __obj) \
1117 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1119 /* This object becomes active in the next generation. */
1120 #define nft_activate_next(__net, __obj) \
1121 (__obj)->genmask = nft_genmask_cur(__net)
1123 /* This object becomes inactive in the next generation. */
1124 #define nft_deactivate_next(__net, __obj) \
1125 (__obj)->genmask = nft_genmask_next(__net)
1127 /* After committing the ruleset, clear the stale generation bit. */
1128 #define nft_clear(__net, __obj) \
1129 (__obj)->genmask &= ~nft_genmask_next(__net)
1130 #define nft_active_genmask(__obj, __genmask) \
1131 !((__obj)->genmask & __genmask)
1134 * Set element transaction helpers
1137 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
1140 return !(ext
->genmask
& genmask
);
1143 static inline void nft_set_elem_change_active(const struct net
*net
,
1144 const struct nft_set
*set
,
1145 struct nft_set_ext
*ext
)
1147 ext
->genmask
^= nft_genmask_next(net
);
1151 * We use a free bit in the genmask field to indicate the element
1152 * is busy, meaning it is currently being processed either by
1153 * the netlink API or GC.
1155 * Even though the genmask is only a single byte wide, this works
1156 * because the extension structure if fully constant once initialized,
1157 * so there are no non-atomic write accesses unless it is already
1160 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1162 #if defined(__LITTLE_ENDIAN_BITFIELD)
1163 #define NFT_SET_ELEM_BUSY_BIT 2
1164 #elif defined(__BIG_ENDIAN_BITFIELD)
1165 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1170 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
1172 unsigned long *word
= (unsigned long *)ext
;
1174 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
1175 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1178 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
1180 unsigned long *word
= (unsigned long *)ext
;
1182 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1186 * struct nft_trans - nf_tables object update in transaction
1188 * @list: used internally
1189 * @msg_type: message type
1190 * @ctx: transaction context
1191 * @data: internal information related to the transaction
1194 struct list_head list
;
1200 struct nft_trans_rule
{
1201 struct nft_rule
*rule
;
1204 #define nft_trans_rule(trans) \
1205 (((struct nft_trans_rule *)trans->data)->rule)
1207 struct nft_trans_set
{
1208 struct nft_set
*set
;
1212 #define nft_trans_set(trans) \
1213 (((struct nft_trans_set *)trans->data)->set)
1214 #define nft_trans_set_id(trans) \
1215 (((struct nft_trans_set *)trans->data)->set_id)
1217 struct nft_trans_chain
{
1219 char name
[NFT_CHAIN_MAXNAMELEN
];
1220 struct nft_stats __percpu
*stats
;
1224 #define nft_trans_chain_update(trans) \
1225 (((struct nft_trans_chain *)trans->data)->update)
1226 #define nft_trans_chain_name(trans) \
1227 (((struct nft_trans_chain *)trans->data)->name)
1228 #define nft_trans_chain_stats(trans) \
1229 (((struct nft_trans_chain *)trans->data)->stats)
1230 #define nft_trans_chain_policy(trans) \
1231 (((struct nft_trans_chain *)trans->data)->policy)
1233 struct nft_trans_table
{
1238 #define nft_trans_table_update(trans) \
1239 (((struct nft_trans_table *)trans->data)->update)
1240 #define nft_trans_table_enable(trans) \
1241 (((struct nft_trans_table *)trans->data)->enable)
1243 struct nft_trans_elem
{
1244 struct nft_set
*set
;
1245 struct nft_set_elem elem
;
1248 #define nft_trans_elem_set(trans) \
1249 (((struct nft_trans_elem *)trans->data)->set)
1250 #define nft_trans_elem(trans) \
1251 (((struct nft_trans_elem *)trans->data)->elem)
1253 struct nft_trans_obj
{
1254 struct nft_object
*obj
;
1257 #define nft_trans_obj(trans) \
1258 (((struct nft_trans_obj *)trans->data)->obj)
1260 #endif /* _NET_NF_TABLES_H */