1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <asm/unaligned.h>
6 #include <linux/list.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nfnetlink.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <linux/u64_stats_sync.h>
12 #include <linux/rhashtable.h>
13 #include <net/netfilter/nf_flow_table.h>
14 #include <net/netlink.h>
15 #include <net/flow_offload.h>
16 #include <net/netns/generic.h>
18 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
22 #define NFT_JUMP_STACK_SIZE 16
28 /* for x_tables compatibility */
29 struct xt_action_param xt
;
32 static inline struct net
*nft_net(const struct nft_pktinfo
*pkt
)
34 return pkt
->xt
.state
->net
;
37 static inline unsigned int nft_hook(const struct nft_pktinfo
*pkt
)
39 return pkt
->xt
.state
->hook
;
42 static inline u8
nft_pf(const struct nft_pktinfo
*pkt
)
44 return pkt
->xt
.state
->pf
;
47 static inline const struct net_device
*nft_in(const struct nft_pktinfo
*pkt
)
49 return pkt
->xt
.state
->in
;
52 static inline const struct net_device
*nft_out(const struct nft_pktinfo
*pkt
)
54 return pkt
->xt
.state
->out
;
57 static inline void nft_set_pktinfo(struct nft_pktinfo
*pkt
,
59 const struct nf_hook_state
*state
)
62 pkt
->xt
.state
= state
;
65 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo
*pkt
,
68 pkt
->tprot_set
= false;
75 * struct nft_verdict - nf_tables verdict
77 * @code: nf_tables/netfilter verdict code
78 * @chain: destination chain for NFT_JUMP/NFT_GOTO
82 struct nft_chain
*chain
;
88 struct nft_verdict verdict
;
90 } __attribute__((aligned(__alignof__(u64
))));
93 * struct nft_regs - nf_tables register set
95 * @data: data registers
96 * @verdict: verdict register
98 * The first four data registers alias to the verdict register.
103 struct nft_verdict verdict
;
107 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
109 * Note, when using concatenations, register allocation happens at 32-bit
110 * level. So for store instruction, pad the rest part with zero to avoid
114 static inline void nft_reg_store8(u32
*dreg
, u8 val
)
120 static inline u8
nft_reg_load8(const u32
*sreg
)
125 static inline void nft_reg_store16(u32
*dreg
, u16 val
)
131 static inline u16
nft_reg_load16(const u32
*sreg
)
136 static inline void nft_reg_store64(u32
*dreg
, u64 val
)
138 put_unaligned(val
, (u64
*)dreg
);
141 static inline u64
nft_reg_load64(const u32
*sreg
)
143 return get_unaligned((u64
*)sreg
);
146 static inline void nft_data_copy(u32
*dst
, const struct nft_data
*src
,
149 if (len
% NFT_REG32_SIZE
)
150 dst
[len
/ NFT_REG32_SIZE
] = 0;
151 memcpy(dst
, src
, len
);
155 * struct nft_ctx - nf_tables rule/set context
157 * @net: net namespace
158 * @table: the table the chain is contained in
159 * @chain: the chain the rule is contained in
160 * @nla: netlink attributes
161 * @portid: netlink portID of the original message
162 * @seq: netlink sequence number
163 * @family: protocol family
164 * @level: depth of the chains
165 * @report: notify via unicast netlink message
169 struct nft_table
*table
;
170 struct nft_chain
*chain
;
171 const struct nlattr
* const *nla
;
180 struct nft_data_desc
{
181 enum nft_data_types type
;
185 int nft_data_init(const struct nft_ctx
*ctx
,
186 struct nft_data
*data
, unsigned int size
,
187 struct nft_data_desc
*desc
, const struct nlattr
*nla
);
188 void nft_data_hold(const struct nft_data
*data
, enum nft_data_types type
);
189 void nft_data_release(const struct nft_data
*data
, enum nft_data_types type
);
190 int nft_data_dump(struct sk_buff
*skb
, int attr
, const struct nft_data
*data
,
191 enum nft_data_types type
, unsigned int len
);
193 static inline enum nft_data_types
nft_dreg_to_type(enum nft_registers reg
)
195 return reg
== NFT_REG_VERDICT
? NFT_DATA_VERDICT
: NFT_DATA_VALUE
;
198 static inline enum nft_registers
nft_type_to_reg(enum nft_data_types type
)
200 return type
== NFT_DATA_VERDICT
? NFT_REG_VERDICT
: NFT_REG_1
* NFT_REG_SIZE
/ NFT_REG32_SIZE
;
203 int nft_parse_u32_check(const struct nlattr
*attr
, int max
, u32
*dest
);
204 int nft_dump_register(struct sk_buff
*skb
, unsigned int attr
, unsigned int reg
);
206 int nft_parse_register_load(const struct nlattr
*attr
, u8
*sreg
, u32 len
);
207 int nft_parse_register_store(const struct nft_ctx
*ctx
,
208 const struct nlattr
*attr
, u8
*dreg
,
209 const struct nft_data
*data
,
210 enum nft_data_types type
, unsigned int len
);
213 * struct nft_userdata - user defined data associated with an object
215 * @len: length of the data
218 * The presence of user data is indicated in an object specific fashion,
219 * so a length of zero can't occur and the value "len" indicates data
222 struct nft_userdata
{
224 unsigned char data
[];
228 * struct nft_set_elem - generic representation of set elements
231 * @key_end: closing element key
232 * @priv: element private data and extensions
234 struct nft_set_elem
{
236 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
240 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
244 u32 buf
[NFT_DATA_VALUE_MAXLEN
/ sizeof(u32
)];
251 struct nft_set_iter
{
256 int (*fn
)(const struct nft_ctx
*ctx
,
258 const struct nft_set_iter
*iter
,
259 struct nft_set_elem
*elem
);
263 * struct nft_set_desc - description of set elements
267 * @size: number of set elements
268 * @field_len: length of each field in concatenation, bytes
269 * @field_count: number of concatenated fields in element
270 * @expr: set must support for expressions
272 struct nft_set_desc
{
276 u8 field_len
[NFT_REG32_COUNT
];
282 * enum nft_set_class - performance class
284 * @NFT_LOOKUP_O_1: constant, O(1)
285 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
286 * @NFT_LOOKUP_O_N: linear, O(N)
290 NFT_SET_CLASS_O_LOG_N
,
295 * struct nft_set_estimate - estimation of memory and performance
298 * @size: required memory
299 * @lookup: lookup performance class
300 * @space: memory class
302 struct nft_set_estimate
{
304 enum nft_set_class lookup
;
305 enum nft_set_class space
;
308 #define NFT_EXPR_MAXATTR 16
309 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
310 ALIGN(size, __alignof__(struct nft_expr)))
313 * struct nft_expr - nf_tables expression
315 * @ops: expression ops
316 * @data: expression private data
319 const struct nft_expr_ops
*ops
;
321 __attribute__((aligned(__alignof__(u64
))));
324 static inline void *nft_expr_priv(const struct nft_expr
*expr
)
326 return (void *)expr
->data
;
329 int nft_expr_clone(struct nft_expr
*dst
, struct nft_expr
*src
);
330 void nft_expr_destroy(const struct nft_ctx
*ctx
, struct nft_expr
*expr
);
331 int nft_expr_dump(struct sk_buff
*skb
, unsigned int attr
,
332 const struct nft_expr
*expr
);
337 * struct nft_set_ops - nf_tables set operations
339 * @lookup: look up an element within the set
340 * @update: update an element if exists, add it if doesn't exist
341 * @delete: delete an element
342 * @insert: insert new element into set
343 * @activate: activate new element in the next generation
344 * @deactivate: lookup for element and deactivate it in the next generation
345 * @flush: deactivate element in the next generation
346 * @remove: remove element from set
347 * @walk: iterate over all set elements
348 * @get: get set elements
349 * @privsize: function to return size of set private data
350 * @init: initialize private data of new set instance
351 * @destroy: destroy private data of set instance
352 * @elemsize: element private size
354 * Operations lookup, update and delete have simpler interfaces, are faster
355 * and currently only used in the packet path. All the rest are slower,
356 * control plane functions.
359 bool (*lookup
)(const struct net
*net
,
360 const struct nft_set
*set
,
362 const struct nft_set_ext
**ext
);
363 bool (*update
)(struct nft_set
*set
,
365 void *(*new)(struct nft_set
*,
366 const struct nft_expr
*,
368 const struct nft_expr
*expr
,
369 struct nft_regs
*regs
,
370 const struct nft_set_ext
**ext
);
371 bool (*delete)(const struct nft_set
*set
,
374 int (*insert
)(const struct net
*net
,
375 const struct nft_set
*set
,
376 const struct nft_set_elem
*elem
,
377 struct nft_set_ext
**ext
);
378 void (*activate
)(const struct net
*net
,
379 const struct nft_set
*set
,
380 const struct nft_set_elem
*elem
);
381 void * (*deactivate
)(const struct net
*net
,
382 const struct nft_set
*set
,
383 const struct nft_set_elem
*elem
);
384 bool (*flush
)(const struct net
*net
,
385 const struct nft_set
*set
,
387 void (*remove
)(const struct net
*net
,
388 const struct nft_set
*set
,
389 const struct nft_set_elem
*elem
);
390 void (*walk
)(const struct nft_ctx
*ctx
,
392 struct nft_set_iter
*iter
);
393 void * (*get
)(const struct net
*net
,
394 const struct nft_set
*set
,
395 const struct nft_set_elem
*elem
,
398 u64 (*privsize
)(const struct nlattr
* const nla
[],
399 const struct nft_set_desc
*desc
);
400 bool (*estimate
)(const struct nft_set_desc
*desc
,
402 struct nft_set_estimate
*est
);
403 int (*init
)(const struct nft_set
*set
,
404 const struct nft_set_desc
*desc
,
405 const struct nlattr
* const nla
[]);
406 void (*destroy
)(const struct nft_set
*set
);
407 void (*gc_init
)(const struct nft_set
*set
);
409 unsigned int elemsize
;
413 * struct nft_set_type - nf_tables set type
415 * @ops: set ops for this type
416 * @features: features supported by the implementation
418 struct nft_set_type
{
419 const struct nft_set_ops ops
;
422 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
424 struct nft_set_elem_expr
{
427 __attribute__((aligned(__alignof__(struct nft_expr
))));
430 #define nft_setelem_expr_at(__elem_expr, __offset) \
431 ((struct nft_expr *)&__elem_expr->data[__offset])
433 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
434 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
435 __size < (__elem_expr)->size; \
436 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
438 #define NFT_SET_EXPR_MAX 2
441 * struct nft_set - nf_tables set instance
443 * @list: table set list node
444 * @bindings: list of set bindings
445 * @table: table this set belongs to
446 * @net: netnamespace this set belongs to
447 * @name: name of the set
448 * @handle: unique handle of the set
449 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
450 * @dtype: data type (verdict or numeric type defined by userspace)
451 * @objtype: object type (see NFT_OBJECT_* definitions)
452 * @size: maximum set size
453 * @field_len: length of each field in concatenation, bytes
454 * @field_count: number of concatenated fields in element
455 * @use: number of rules references to this set
456 * @nelems: number of elements
457 * @ndeact: number of deactivated elements queued for removal
458 * @timeout: default timeout value in jiffies
459 * @gc_int: garbage collection interval in msecs
460 * @policy: set parameterization (see enum nft_set_policies)
461 * @udlen: user data length
463 * @expr: stateful expression
466 * @genmask: generation mask
469 * @data: private set data
472 struct list_head list
;
473 struct list_head bindings
;
474 struct nft_table
*table
;
482 u8 field_len
[NFT_REG32_COUNT
];
491 unsigned char *udata
;
492 /* runtime data below here */
493 const struct nft_set_ops
*ops ____cacheline_aligned
;
499 struct nft_expr
*exprs
[NFT_SET_EXPR_MAX
];
501 __attribute__((aligned(__alignof__(u64
))));
504 static inline bool nft_set_is_anonymous(const struct nft_set
*set
)
506 return set
->flags
& NFT_SET_ANONYMOUS
;
509 static inline void *nft_set_priv(const struct nft_set
*set
)
511 return (void *)set
->data
;
514 static inline struct nft_set
*nft_set_container_of(const void *priv
)
516 return (void *)priv
- offsetof(struct nft_set
, data
);
519 struct nft_set
*nft_set_lookup_global(const struct net
*net
,
520 const struct nft_table
*table
,
521 const struct nlattr
*nla_set_name
,
522 const struct nlattr
*nla_set_id
,
525 static inline unsigned long nft_set_gc_interval(const struct nft_set
*set
)
527 return set
->gc_int
? msecs_to_jiffies(set
->gc_int
) : HZ
;
531 * struct nft_set_binding - nf_tables set binding
533 * @list: set bindings list node
534 * @chain: chain containing the rule bound to the set
535 * @flags: set action flags
537 * A set binding contains all information necessary for validation
538 * of new elements added to a bound set.
540 struct nft_set_binding
{
541 struct list_head list
;
542 const struct nft_chain
*chain
;
546 enum nft_trans_phase
;
547 void nf_tables_deactivate_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
548 struct nft_set_binding
*binding
,
549 enum nft_trans_phase phase
);
550 int nf_tables_bind_set(const struct nft_ctx
*ctx
, struct nft_set
*set
,
551 struct nft_set_binding
*binding
);
552 void nf_tables_destroy_set(const struct nft_ctx
*ctx
, struct nft_set
*set
);
555 * enum nft_set_extensions - set extension type IDs
557 * @NFT_SET_EXT_KEY: element key
558 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
559 * @NFT_SET_EXT_DATA: mapping data
560 * @NFT_SET_EXT_FLAGS: element flags
561 * @NFT_SET_EXT_TIMEOUT: element timeout
562 * @NFT_SET_EXT_EXPIRATION: element expiration time
563 * @NFT_SET_EXT_USERDATA: user data associated with the element
564 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
565 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
566 * @NFT_SET_EXT_NUM: number of extension types
568 enum nft_set_extensions
{
574 NFT_SET_EXT_EXPIRATION
,
575 NFT_SET_EXT_USERDATA
,
576 NFT_SET_EXT_EXPRESSIONS
,
582 * struct nft_set_ext_type - set extension type
584 * @len: fixed part length of the extension
585 * @align: alignment requirements of the extension
587 struct nft_set_ext_type
{
592 extern const struct nft_set_ext_type nft_set_ext_types
[];
595 * struct nft_set_ext_tmpl - set extension template
597 * @len: length of extension area
598 * @offset: offsets of individual extension types
600 struct nft_set_ext_tmpl
{
602 u8 offset
[NFT_SET_EXT_NUM
];
606 * struct nft_set_ext - set extensions
608 * @genmask: generation mask
609 * @offset: offsets of individual extension types
610 * @data: beginning of extension data
614 u8 offset
[NFT_SET_EXT_NUM
];
618 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl
*tmpl
)
620 memset(tmpl
, 0, sizeof(*tmpl
));
621 tmpl
->len
= sizeof(struct nft_set_ext
);
624 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl
*tmpl
, u8 id
,
627 tmpl
->len
= ALIGN(tmpl
->len
, nft_set_ext_types
[id
].align
);
628 BUG_ON(tmpl
->len
> U8_MAX
);
629 tmpl
->offset
[id
] = tmpl
->len
;
630 tmpl
->len
+= nft_set_ext_types
[id
].len
+ len
;
633 static inline void nft_set_ext_add(struct nft_set_ext_tmpl
*tmpl
, u8 id
)
635 nft_set_ext_add_length(tmpl
, id
, 0);
638 static inline void nft_set_ext_init(struct nft_set_ext
*ext
,
639 const struct nft_set_ext_tmpl
*tmpl
)
641 memcpy(ext
->offset
, tmpl
->offset
, sizeof(ext
->offset
));
644 static inline bool __nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
646 return !!ext
->offset
[id
];
649 static inline bool nft_set_ext_exists(const struct nft_set_ext
*ext
, u8 id
)
651 return ext
&& __nft_set_ext_exists(ext
, id
);
654 static inline void *nft_set_ext(const struct nft_set_ext
*ext
, u8 id
)
656 return (void *)ext
+ ext
->offset
[id
];
659 static inline struct nft_data
*nft_set_ext_key(const struct nft_set_ext
*ext
)
661 return nft_set_ext(ext
, NFT_SET_EXT_KEY
);
664 static inline struct nft_data
*nft_set_ext_key_end(const struct nft_set_ext
*ext
)
666 return nft_set_ext(ext
, NFT_SET_EXT_KEY_END
);
669 static inline struct nft_data
*nft_set_ext_data(const struct nft_set_ext
*ext
)
671 return nft_set_ext(ext
, NFT_SET_EXT_DATA
);
674 static inline u8
*nft_set_ext_flags(const struct nft_set_ext
*ext
)
676 return nft_set_ext(ext
, NFT_SET_EXT_FLAGS
);
679 static inline u64
*nft_set_ext_timeout(const struct nft_set_ext
*ext
)
681 return nft_set_ext(ext
, NFT_SET_EXT_TIMEOUT
);
684 static inline u64
*nft_set_ext_expiration(const struct nft_set_ext
*ext
)
686 return nft_set_ext(ext
, NFT_SET_EXT_EXPIRATION
);
689 static inline struct nft_userdata
*nft_set_ext_userdata(const struct nft_set_ext
*ext
)
691 return nft_set_ext(ext
, NFT_SET_EXT_USERDATA
);
694 static inline struct nft_set_elem_expr
*nft_set_ext_expr(const struct nft_set_ext
*ext
)
696 return nft_set_ext(ext
, NFT_SET_EXT_EXPRESSIONS
);
699 static inline bool nft_set_elem_expired(const struct nft_set_ext
*ext
)
701 return nft_set_ext_exists(ext
, NFT_SET_EXT_EXPIRATION
) &&
702 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext
));
705 static inline struct nft_set_ext
*nft_set_elem_ext(const struct nft_set
*set
,
708 return elem
+ set
->ops
->elemsize
;
711 static inline struct nft_object
**nft_set_ext_obj(const struct nft_set_ext
*ext
)
713 return nft_set_ext(ext
, NFT_SET_EXT_OBJREF
);
716 struct nft_expr
*nft_set_elem_expr_alloc(const struct nft_ctx
*ctx
,
717 const struct nft_set
*set
,
718 const struct nlattr
*attr
);
720 void *nft_set_elem_init(const struct nft_set
*set
,
721 const struct nft_set_ext_tmpl
*tmpl
,
722 const u32
*key
, const u32
*key_end
, const u32
*data
,
723 u64 timeout
, u64 expiration
, gfp_t gfp
);
724 int nft_set_elem_expr_clone(const struct nft_ctx
*ctx
, struct nft_set
*set
,
725 struct nft_expr
*expr_array
[]);
726 void nft_set_elem_destroy(const struct nft_set
*set
, void *elem
,
730 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
733 * @set: set the elements belong to
734 * @cnt: count of elements
736 struct nft_set_gc_batch_head
{
738 const struct nft_set
*set
;
742 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
743 sizeof(struct nft_set_gc_batch_head)) / \
747 * struct nft_set_gc_batch - nf_tables set garbage collection batch
749 * @head: GC batch head
750 * @elems: garbage collection elements
752 struct nft_set_gc_batch
{
753 struct nft_set_gc_batch_head head
;
754 void *elems
[NFT_SET_GC_BATCH_SIZE
];
757 struct nft_set_gc_batch
*nft_set_gc_batch_alloc(const struct nft_set
*set
,
759 void nft_set_gc_batch_release(struct rcu_head
*rcu
);
761 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch
*gcb
)
764 call_rcu(&gcb
->head
.rcu
, nft_set_gc_batch_release
);
767 static inline struct nft_set_gc_batch
*
768 nft_set_gc_batch_check(const struct nft_set
*set
, struct nft_set_gc_batch
*gcb
,
772 if (gcb
->head
.cnt
+ 1 < ARRAY_SIZE(gcb
->elems
))
774 nft_set_gc_batch_complete(gcb
);
776 return nft_set_gc_batch_alloc(set
, gfp
);
779 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch
*gcb
,
782 gcb
->elems
[gcb
->head
.cnt
++] = elem
;
787 * struct nft_expr_type - nf_tables expression type
789 * @select_ops: function to select nft_expr_ops
790 * @release_ops: release nft_expr_ops
791 * @ops: default ops, used when no select_ops functions is present
792 * @list: used internally
794 * @owner: module reference
795 * @policy: netlink attribute policy
796 * @maxattr: highest netlink attribute number
797 * @family: address family for AF-specific types
798 * @flags: expression type flags
800 struct nft_expr_type
{
801 const struct nft_expr_ops
*(*select_ops
)(const struct nft_ctx
*,
802 const struct nlattr
* const tb
[]);
803 void (*release_ops
)(const struct nft_expr_ops
*ops
);
804 const struct nft_expr_ops
*ops
;
805 struct list_head list
;
807 struct module
*owner
;
808 const struct nla_policy
*policy
;
809 unsigned int maxattr
;
814 #define NFT_EXPR_STATEFUL 0x1
815 #define NFT_EXPR_GC 0x2
817 enum nft_trans_phase
{
824 struct nft_flow_rule
;
825 struct nft_offload_ctx
;
828 * struct nft_expr_ops - nf_tables expression operations
830 * @eval: Expression evaluation function
831 * @size: full expression size, including private data size
832 * @init: initialization function
833 * @activate: activate expression in the next generation
834 * @deactivate: deactivate expression in next generation
835 * @destroy: destruction function, called after synchronize_rcu
836 * @dump: function to dump parameters
837 * @type: expression type
838 * @validate: validate expression, called during loop detection
839 * @data: extra data to attach to this expression operation
841 struct nft_expr_ops
{
842 void (*eval
)(const struct nft_expr
*expr
,
843 struct nft_regs
*regs
,
844 const struct nft_pktinfo
*pkt
);
845 int (*clone
)(struct nft_expr
*dst
,
846 const struct nft_expr
*src
);
849 int (*init
)(const struct nft_ctx
*ctx
,
850 const struct nft_expr
*expr
,
851 const struct nlattr
* const tb
[]);
852 void (*activate
)(const struct nft_ctx
*ctx
,
853 const struct nft_expr
*expr
);
854 void (*deactivate
)(const struct nft_ctx
*ctx
,
855 const struct nft_expr
*expr
,
856 enum nft_trans_phase phase
);
857 void (*destroy
)(const struct nft_ctx
*ctx
,
858 const struct nft_expr
*expr
);
859 void (*destroy_clone
)(const struct nft_ctx
*ctx
,
860 const struct nft_expr
*expr
);
861 int (*dump
)(struct sk_buff
*skb
,
862 const struct nft_expr
*expr
);
863 int (*validate
)(const struct nft_ctx
*ctx
,
864 const struct nft_expr
*expr
,
865 const struct nft_data
**data
);
866 bool (*gc
)(struct net
*net
,
867 const struct nft_expr
*expr
);
868 int (*offload
)(struct nft_offload_ctx
*ctx
,
869 struct nft_flow_rule
*flow
,
870 const struct nft_expr
*expr
);
871 void (*offload_stats
)(struct nft_expr
*expr
,
872 const struct flow_stats
*stats
);
874 const struct nft_expr_type
*type
;
879 * struct nft_rule - nf_tables rule
881 * @list: used internally
882 * @handle: rule handle
883 * @genmask: generation mask
884 * @dlen: length of expression data
885 * @udata: user data is appended to the rule
886 * @data: expression data
889 struct list_head list
;
895 __attribute__((aligned(__alignof__(struct nft_expr
))));
898 static inline struct nft_expr
*nft_expr_first(const struct nft_rule
*rule
)
900 return (struct nft_expr
*)&rule
->data
[0];
903 static inline struct nft_expr
*nft_expr_next(const struct nft_expr
*expr
)
905 return ((void *)expr
) + expr
->ops
->size
;
908 static inline struct nft_expr
*nft_expr_last(const struct nft_rule
*rule
)
910 return (struct nft_expr
*)&rule
->data
[rule
->dlen
];
913 static inline bool nft_expr_more(const struct nft_rule
*rule
,
914 const struct nft_expr
*expr
)
916 return expr
!= nft_expr_last(rule
) && expr
->ops
;
919 static inline struct nft_userdata
*nft_userdata(const struct nft_rule
*rule
)
921 return (void *)&rule
->data
[rule
->dlen
];
924 void nf_tables_rule_release(const struct nft_ctx
*ctx
, struct nft_rule
*rule
);
926 static inline void nft_set_elem_update_expr(const struct nft_set_ext
*ext
,
927 struct nft_regs
*regs
,
928 const struct nft_pktinfo
*pkt
)
930 struct nft_set_elem_expr
*elem_expr
;
931 struct nft_expr
*expr
;
934 if (__nft_set_ext_exists(ext
, NFT_SET_EXT_EXPRESSIONS
)) {
935 elem_expr
= nft_set_ext_expr(ext
);
936 nft_setelem_expr_foreach(expr
, elem_expr
, size
) {
937 expr
->ops
->eval(expr
, regs
, pkt
);
938 if (regs
->verdict
.code
== NFT_BREAK
)
945 * The last pointer isn't really necessary, but the compiler isn't able to
946 * determine that the result of nft_expr_last() is always the same since it
947 * can't assume that the dlen value wasn't changed within calls in the loop.
949 #define nft_rule_for_each_expr(expr, last, rule) \
950 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
952 (expr) = nft_expr_next(expr))
954 #define NFT_CHAIN_POLICY_UNSET U8_MAX
957 * struct nft_chain - nf_tables chain
959 * @rules: list of rules in the chain
960 * @list: used internally
961 * @rhlhead: used internally
962 * @table: table that this chain belongs to
963 * @handle: chain handle
964 * @use: number of jump references to this chain
965 * @flags: bitmask of enum nft_chain_flags
966 * @name: name of the chain
969 struct nft_rule
*__rcu
*rules_gen_0
;
970 struct nft_rule
*__rcu
*rules_gen_1
;
971 struct list_head rules
;
972 struct list_head list
;
973 struct rhlist_head rhlhead
;
974 struct nft_table
*table
;
984 /* Only used during control plane commit phase: */
985 struct nft_rule
**rules_next
;
988 int nft_chain_validate(const struct nft_ctx
*ctx
, const struct nft_chain
*chain
);
990 enum nft_chain_types
{
991 NFT_CHAIN_T_DEFAULT
= 0,
998 * struct nft_chain_type - nf_tables chain type info
1000 * @name: name of the type
1001 * @type: numeric identifier
1002 * @family: address family
1003 * @owner: module owner
1004 * @hook_mask: mask of valid hooks
1005 * @hooks: array of hook functions
1006 * @ops_register: base chain register function
1007 * @ops_unregister: base chain unregister function
1009 struct nft_chain_type
{
1011 enum nft_chain_types type
;
1013 struct module
*owner
;
1014 unsigned int hook_mask
;
1015 nf_hookfn
*hooks
[NFT_MAX_HOOKS
];
1016 int (*ops_register
)(struct net
*net
, const struct nf_hook_ops
*ops
);
1017 void (*ops_unregister
)(struct net
*net
, const struct nf_hook_ops
*ops
);
1020 int nft_chain_validate_dependency(const struct nft_chain
*chain
,
1021 enum nft_chain_types type
);
1022 int nft_chain_validate_hooks(const struct nft_chain
*chain
,
1023 unsigned int hook_flags
);
1025 static inline bool nft_chain_is_bound(struct nft_chain
*chain
)
1027 return (chain
->flags
& NFT_CHAIN_BINDING
) && chain
->bound
;
1030 void nft_chain_del(struct nft_chain
*chain
);
1031 void nf_tables_chain_destroy(struct nft_ctx
*ctx
);
1036 struct u64_stats_sync syncp
;
1040 struct list_head list
;
1042 struct nf_hook_ops ops
;
1043 struct rcu_head rcu
;
1047 * struct nft_base_chain - nf_tables base chain
1049 * @ops: netfilter hook ops
1050 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1052 * @policy: default policy
1053 * @stats: per-cpu chain stats
1055 * @flow_block: flow block (for hardware offload)
1057 struct nft_base_chain
{
1058 struct nf_hook_ops ops
;
1059 struct list_head hook_list
;
1060 const struct nft_chain_type
*type
;
1063 struct nft_stats __percpu
*stats
;
1064 struct nft_chain chain
;
1065 struct flow_block flow_block
;
1068 static inline struct nft_base_chain
*nft_base_chain(const struct nft_chain
*chain
)
1070 return container_of(chain
, struct nft_base_chain
, chain
);
1073 static inline bool nft_is_base_chain(const struct nft_chain
*chain
)
1075 return chain
->flags
& NFT_CHAIN_BASE
;
1078 int __nft_release_basechain(struct nft_ctx
*ctx
);
1080 unsigned int nft_do_chain(struct nft_pktinfo
*pkt
, void *priv
);
1083 * struct nft_table - nf_tables table
1085 * @list: used internally
1086 * @chains_ht: chains in the table
1087 * @chains: same, for stable walks
1088 * @sets: sets in the table
1089 * @objects: stateful objects in the table
1090 * @flowtables: flow tables in the table
1091 * @hgenerator: handle generator state
1092 * @handle: table handle
1093 * @use: number of chain references to this table
1094 * @flags: table flag (see enum nft_table_flags)
1095 * @genmask: generation mask
1096 * @afinfo: address family info
1097 * @name: name of the table
1100 struct list_head list
;
1101 struct rhltable chains_ht
;
1102 struct list_head chains
;
1103 struct list_head sets
;
1104 struct list_head objects
;
1105 struct list_head flowtables
;
1118 static inline bool nft_table_has_owner(const struct nft_table
*table
)
1120 return table
->flags
& NFT_TABLE_F_OWNER
;
1123 static inline bool nft_base_chain_netdev(int family
, u32 hooknum
)
1125 return family
== NFPROTO_NETDEV
||
1126 (family
== NFPROTO_INET
&& hooknum
== NF_INET_INGRESS
);
1129 void nft_register_chain_type(const struct nft_chain_type
*);
1130 void nft_unregister_chain_type(const struct nft_chain_type
*);
1132 int nft_register_expr(struct nft_expr_type
*);
1133 void nft_unregister_expr(struct nft_expr_type
*);
1135 int nft_verdict_dump(struct sk_buff
*skb
, int type
,
1136 const struct nft_verdict
*v
);
1139 * struct nft_object_hash_key - key to lookup nft_object
1141 * @name: name of the stateful object to look up
1142 * @table: table the object belongs to
1144 struct nft_object_hash_key
{
1146 const struct nft_table
*table
;
1150 * struct nft_object - nf_tables stateful object
1152 * @list: table stateful object list node
1153 * @key: keys that identify this object
1154 * @rhlhead: nft_objname_ht node
1155 * @genmask: generation mask
1156 * @use: number of references to this stateful object
1157 * @handle: unique object handle
1158 * @ops: object operations
1159 * @data: object data, layout depends on type
1162 struct list_head list
;
1163 struct rhlist_head rhlhead
;
1164 struct nft_object_hash_key key
;
1170 /* runtime data below here */
1171 const struct nft_object_ops
*ops ____cacheline_aligned
;
1172 unsigned char data
[]
1173 __attribute__((aligned(__alignof__(u64
))));
1176 static inline void *nft_obj_data(const struct nft_object
*obj
)
1178 return (void *)obj
->data
;
1181 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1183 struct nft_object
*nft_obj_lookup(const struct net
*net
,
1184 const struct nft_table
*table
,
1185 const struct nlattr
*nla
, u32 objtype
,
1188 void nft_obj_notify(struct net
*net
, const struct nft_table
*table
,
1189 struct nft_object
*obj
, u32 portid
, u32 seq
,
1190 int event
, int family
, int report
, gfp_t gfp
);
1193 * struct nft_object_type - stateful object type
1195 * @select_ops: function to select nft_object_ops
1196 * @ops: default ops, used when no select_ops functions is present
1197 * @list: list node in list of object types
1198 * @type: stateful object numeric type
1199 * @owner: module owner
1200 * @maxattr: maximum netlink attribute
1201 * @policy: netlink attribute policy
1203 struct nft_object_type
{
1204 const struct nft_object_ops
*(*select_ops
)(const struct nft_ctx
*,
1205 const struct nlattr
* const tb
[]);
1206 const struct nft_object_ops
*ops
;
1207 struct list_head list
;
1209 unsigned int maxattr
;
1210 struct module
*owner
;
1211 const struct nla_policy
*policy
;
1215 * struct nft_object_ops - stateful object operations
1217 * @eval: stateful object evaluation function
1218 * @size: stateful object size
1219 * @init: initialize object from netlink attributes
1220 * @destroy: release existing stateful object
1221 * @dump: netlink dump stateful object
1222 * @update: update stateful object
1224 struct nft_object_ops
{
1225 void (*eval
)(struct nft_object
*obj
,
1226 struct nft_regs
*regs
,
1227 const struct nft_pktinfo
*pkt
);
1229 int (*init
)(const struct nft_ctx
*ctx
,
1230 const struct nlattr
*const tb
[],
1231 struct nft_object
*obj
);
1232 void (*destroy
)(const struct nft_ctx
*ctx
,
1233 struct nft_object
*obj
);
1234 int (*dump
)(struct sk_buff
*skb
,
1235 struct nft_object
*obj
,
1237 void (*update
)(struct nft_object
*obj
,
1238 struct nft_object
*newobj
);
1239 const struct nft_object_type
*type
;
1242 int nft_register_obj(struct nft_object_type
*obj_type
);
1243 void nft_unregister_obj(struct nft_object_type
*obj_type
);
1245 #define NFT_NETDEVICE_MAX 256
1248 * struct nft_flowtable - nf_tables flow table
1250 * @list: flow table list node in table list
1251 * @table: the table the flow table is contained in
1252 * @name: name of this flow table
1253 * @hooknum: hook number
1254 * @ops_len: number of hooks in array
1255 * @genmask: generation mask
1256 * @use: number of references to this flow table
1257 * @handle: unique object handle
1258 * @dev_name: array of device names
1259 * @data: rhashtable and garbage collector
1260 * @ops: array of hooks
1262 struct nft_flowtable
{
1263 struct list_head list
;
1264 struct nft_table
*table
;
1271 /* runtime data below here */
1272 struct list_head hook_list ____cacheline_aligned
;
1273 struct nf_flowtable data
;
1276 struct nft_flowtable
*nft_flowtable_lookup(const struct nft_table
*table
,
1277 const struct nlattr
*nla
,
1280 void nf_tables_deactivate_flowtable(const struct nft_ctx
*ctx
,
1281 struct nft_flowtable
*flowtable
,
1282 enum nft_trans_phase phase
);
1284 void nft_register_flowtable_type(struct nf_flowtable_type
*type
);
1285 void nft_unregister_flowtable_type(struct nf_flowtable_type
*type
);
1288 * struct nft_traceinfo - nft tracing information and state
1290 * @pkt: pktinfo currently processed
1291 * @basechain: base chain currently processed
1292 * @chain: chain currently processed
1293 * @rule: rule that was evaluated
1294 * @verdict: verdict given by rule
1295 * @type: event type (enum nft_trace_types)
1296 * @packet_dumped: packet headers sent in a previous traceinfo message
1297 * @trace: other struct members are initialised
1299 struct nft_traceinfo
{
1300 const struct nft_pktinfo
*pkt
;
1301 const struct nft_base_chain
*basechain
;
1302 const struct nft_chain
*chain
;
1303 const struct nft_rule
*rule
;
1304 const struct nft_verdict
*verdict
;
1305 enum nft_trace_types type
;
1310 void nft_trace_init(struct nft_traceinfo
*info
, const struct nft_pktinfo
*pkt
,
1311 const struct nft_verdict
*verdict
,
1312 const struct nft_chain
*basechain
);
1314 void nft_trace_notify(struct nft_traceinfo
*info
);
1316 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1317 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1319 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1320 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1322 #define MODULE_ALIAS_NFT_EXPR(name) \
1323 MODULE_ALIAS("nft-expr-" name)
1325 #define MODULE_ALIAS_NFT_OBJ(type) \
1326 MODULE_ALIAS("nft-obj-" __stringify(type))
1328 #if IS_ENABLED(CONFIG_NF_TABLES)
1331 * The gencursor defines two generations, the currently active and the
1332 * next one. Objects contain a bitmask of 2 bits specifying the generations
1333 * they're active in. A set bit means they're inactive in the generation
1334 * represented by that bit.
1336 * New objects start out as inactive in the current and active in the
1337 * next generation. When committing the ruleset the bitmask is cleared,
1338 * meaning they're active in all generations. When removing an object,
1339 * it is set inactive in the next generation. After committing the ruleset,
1340 * the objects are removed.
1342 static inline unsigned int nft_gencursor_next(const struct net
*net
)
1344 return net
->nft
.gencursor
+ 1 == 1 ? 1 : 0;
1347 static inline u8
nft_genmask_next(const struct net
*net
)
1349 return 1 << nft_gencursor_next(net
);
1352 static inline u8
nft_genmask_cur(const struct net
*net
)
1354 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1355 return 1 << READ_ONCE(net
->nft
.gencursor
);
1358 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1361 * Generic transaction helpers
1364 /* Check if this object is currently active. */
1365 #define nft_is_active(__net, __obj) \
1366 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1368 /* Check if this object is active in the next generation. */
1369 #define nft_is_active_next(__net, __obj) \
1370 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1372 /* This object becomes active in the next generation. */
1373 #define nft_activate_next(__net, __obj) \
1374 (__obj)->genmask = nft_genmask_cur(__net)
1376 /* This object becomes inactive in the next generation. */
1377 #define nft_deactivate_next(__net, __obj) \
1378 (__obj)->genmask = nft_genmask_next(__net)
1380 /* After committing the ruleset, clear the stale generation bit. */
1381 #define nft_clear(__net, __obj) \
1382 (__obj)->genmask &= ~nft_genmask_next(__net)
1383 #define nft_active_genmask(__obj, __genmask) \
1384 !((__obj)->genmask & __genmask)
1387 * Set element transaction helpers
1390 static inline bool nft_set_elem_active(const struct nft_set_ext
*ext
,
1393 return !(ext
->genmask
& genmask
);
1396 static inline void nft_set_elem_change_active(const struct net
*net
,
1397 const struct nft_set
*set
,
1398 struct nft_set_ext
*ext
)
1400 ext
->genmask
^= nft_genmask_next(net
);
1403 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1406 * We use a free bit in the genmask field to indicate the element
1407 * is busy, meaning it is currently being processed either by
1408 * the netlink API or GC.
1410 * Even though the genmask is only a single byte wide, this works
1411 * because the extension structure if fully constant once initialized,
1412 * so there are no non-atomic write accesses unless it is already
1415 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1417 #if defined(__LITTLE_ENDIAN_BITFIELD)
1418 #define NFT_SET_ELEM_BUSY_BIT 2
1419 #elif defined(__BIG_ENDIAN_BITFIELD)
1420 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1425 static inline int nft_set_elem_mark_busy(struct nft_set_ext
*ext
)
1427 unsigned long *word
= (unsigned long *)ext
;
1429 BUILD_BUG_ON(offsetof(struct nft_set_ext
, genmask
) != 0);
1430 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1433 static inline void nft_set_elem_clear_busy(struct nft_set_ext
*ext
)
1435 unsigned long *word
= (unsigned long *)ext
;
1437 clear_bit(NFT_SET_ELEM_BUSY_BIT
, word
);
1441 * struct nft_trans - nf_tables object update in transaction
1443 * @list: used internally
1444 * @msg_type: message type
1445 * @put_net: ctx->net needs to be put
1446 * @ctx: transaction context
1447 * @data: internal information related to the transaction
1450 struct list_head list
;
1457 struct nft_trans_rule
{
1458 struct nft_rule
*rule
;
1459 struct nft_flow_rule
*flow
;
1463 #define nft_trans_rule(trans) \
1464 (((struct nft_trans_rule *)trans->data)->rule)
1465 #define nft_trans_flow_rule(trans) \
1466 (((struct nft_trans_rule *)trans->data)->flow)
1467 #define nft_trans_rule_id(trans) \
1468 (((struct nft_trans_rule *)trans->data)->rule_id)
1470 struct nft_trans_set
{
1471 struct nft_set
*set
;
1476 #define nft_trans_set(trans) \
1477 (((struct nft_trans_set *)trans->data)->set)
1478 #define nft_trans_set_id(trans) \
1479 (((struct nft_trans_set *)trans->data)->set_id)
1480 #define nft_trans_set_bound(trans) \
1481 (((struct nft_trans_set *)trans->data)->bound)
1483 struct nft_trans_chain
{
1486 struct nft_stats __percpu
*stats
;
1491 #define nft_trans_chain_update(trans) \
1492 (((struct nft_trans_chain *)trans->data)->update)
1493 #define nft_trans_chain_name(trans) \
1494 (((struct nft_trans_chain *)trans->data)->name)
1495 #define nft_trans_chain_stats(trans) \
1496 (((struct nft_trans_chain *)trans->data)->stats)
1497 #define nft_trans_chain_policy(trans) \
1498 (((struct nft_trans_chain *)trans->data)->policy)
1499 #define nft_trans_chain_id(trans) \
1500 (((struct nft_trans_chain *)trans->data)->chain_id)
1502 struct nft_trans_table
{
1508 #define nft_trans_table_update(trans) \
1509 (((struct nft_trans_table *)trans->data)->update)
1510 #define nft_trans_table_state(trans) \
1511 (((struct nft_trans_table *)trans->data)->state)
1512 #define nft_trans_table_flags(trans) \
1513 (((struct nft_trans_table *)trans->data)->flags)
1515 struct nft_trans_elem
{
1516 struct nft_set
*set
;
1517 struct nft_set_elem elem
;
1521 #define nft_trans_elem_set(trans) \
1522 (((struct nft_trans_elem *)trans->data)->set)
1523 #define nft_trans_elem(trans) \
1524 (((struct nft_trans_elem *)trans->data)->elem)
1525 #define nft_trans_elem_set_bound(trans) \
1526 (((struct nft_trans_elem *)trans->data)->bound)
1528 struct nft_trans_obj
{
1529 struct nft_object
*obj
;
1530 struct nft_object
*newobj
;
1534 #define nft_trans_obj(trans) \
1535 (((struct nft_trans_obj *)trans->data)->obj)
1536 #define nft_trans_obj_newobj(trans) \
1537 (((struct nft_trans_obj *)trans->data)->newobj)
1538 #define nft_trans_obj_update(trans) \
1539 (((struct nft_trans_obj *)trans->data)->update)
1541 struct nft_trans_flowtable
{
1542 struct nft_flowtable
*flowtable
;
1544 struct list_head hook_list
;
1548 #define nft_trans_flowtable(trans) \
1549 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1550 #define nft_trans_flowtable_update(trans) \
1551 (((struct nft_trans_flowtable *)trans->data)->update)
1552 #define nft_trans_flowtable_hooks(trans) \
1553 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1554 #define nft_trans_flowtable_flags(trans) \
1555 (((struct nft_trans_flowtable *)trans->data)->flags)
1557 int __init
nft_chain_filter_init(void);
1558 void nft_chain_filter_fini(void);
1560 void __init
nft_chain_route_init(void);
1561 void nft_chain_route_fini(void);
1563 void nf_tables_trans_destroy_flush_work(void);
1565 int nf_msecs_to_jiffies64(const struct nlattr
*nla
, u64
*result
);
1566 __be64
nf_jiffies64_to_msecs(u64 input
);
1568 #ifdef CONFIG_MODULES
1569 __printf(2, 3) int nft_request_module(struct net
*net
, const char *fmt
, ...);
1571 static inline int nft_request_module(struct net
*net
, const char *fmt
, ...) { return -ENOENT
; }
1574 struct nftables_pernet
{
1575 struct list_head tables
;
1576 struct list_head commit_list
;
1577 struct list_head module_list
;
1578 struct list_head notify_list
;
1579 struct mutex commit_mutex
;
1580 unsigned int base_seq
;
1584 extern unsigned int nf_tables_net_id
;
1586 static inline struct nftables_pernet
*nft_pernet(const struct net
*net
)
1588 return net_generic(net
, nf_tables_net_id
);
1591 #endif /* _NET_NF_TABLES_H */