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>
17 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
21 #define NFT_JUMP_STACK_SIZE 16
27 /* for x_tables compatibility */
28 struct xt_action_param xt;
31 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
33 return pkt->xt.state->net;
36 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
38 return pkt->xt.state->hook;
41 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
43 return pkt->xt.state->pf;
46 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
48 return pkt->xt.state->in;
51 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
53 return pkt->xt.state->out;
56 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
58 const struct nf_hook_state *state)
61 pkt->xt.state = state;
64 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
67 pkt->tprot_set = false;
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 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
108 * Note, when using concatenations, register allocation happens at 32-bit
109 * level. So for store instruction, pad the rest part with zero to avoid
113 static inline void nft_reg_store8(u32 *dreg, u8 val)
119 static inline u8 nft_reg_load8(const u32 *sreg)
124 static inline void nft_reg_store16(u32 *dreg, u16 val)
130 static inline u16 nft_reg_load16(const u32 *sreg)
135 static inline void nft_reg_store64(u32 *dreg, u64 val)
137 put_unaligned(val, (u64 *)dreg);
140 static inline u64 nft_reg_load64(const u32 *sreg)
142 return get_unaligned((u64 *)sreg);
145 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
148 if (len % NFT_REG32_SIZE)
149 dst[len / NFT_REG32_SIZE] = 0;
150 memcpy(dst, src, len);
154 * struct nft_ctx - nf_tables rule/set context
156 * @net: net namespace
157 * @table: the table the chain is contained in
158 * @chain: the chain the rule is contained in
159 * @nla: netlink attributes
160 * @portid: netlink portID of the original message
161 * @seq: netlink sequence number
162 * @family: protocol family
163 * @level: depth of the chains
164 * @report: notify via unicast netlink message
168 struct nft_table *table;
169 struct nft_chain *chain;
170 const struct nlattr * const *nla;
179 struct nft_data_desc {
180 enum nft_data_types type;
184 int nft_data_init(const struct nft_ctx *ctx,
185 struct nft_data *data, unsigned int size,
186 struct nft_data_desc *desc, const struct nlattr *nla);
187 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
188 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
189 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
190 enum nft_data_types type, unsigned int len);
192 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
194 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
197 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
199 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
202 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
203 unsigned int nft_parse_register(const struct nlattr *attr);
204 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
206 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
207 int nft_validate_register_store(const struct nft_ctx *ctx,
208 enum nft_registers reg,
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;
312 * struct nft_set_ops - nf_tables set operations
314 * @lookup: look up an element within the set
315 * @update: update an element if exists, add it if doesn't exist
316 * @delete: delete an element
317 * @insert: insert new element into set
318 * @activate: activate new element in the next generation
319 * @deactivate: lookup for element and deactivate it in the next generation
320 * @flush: deactivate element in the next generation
321 * @remove: remove element from set
322 * @walk: iterate over all set elements
323 * @get: get set elements
324 * @privsize: function to return size of set private data
325 * @init: initialize private data of new set instance
326 * @destroy: destroy private data of set instance
327 * @elemsize: element private size
329 * Operations lookup, update and delete have simpler interfaces, are faster
330 * and currently only used in the packet path. All the rest are slower,
331 * control plane functions.
334 bool (*lookup)(const struct net *net,
335 const struct nft_set *set,
337 const struct nft_set_ext **ext);
338 bool (*update)(struct nft_set *set,
340 void *(*new)(struct nft_set *,
341 const struct nft_expr *,
343 const struct nft_expr *expr,
344 struct nft_regs *regs,
345 const struct nft_set_ext **ext);
346 bool (*delete)(const struct nft_set *set,
349 int (*insert)(const struct net *net,
350 const struct nft_set *set,
351 const struct nft_set_elem *elem,
352 struct nft_set_ext **ext);
353 void (*activate)(const struct net *net,
354 const struct nft_set *set,
355 const struct nft_set_elem *elem);
356 void * (*deactivate)(const struct net *net,
357 const struct nft_set *set,
358 const struct nft_set_elem *elem);
359 bool (*flush)(const struct net *net,
360 const struct nft_set *set,
362 void (*remove)(const struct net *net,
363 const struct nft_set *set,
364 const struct nft_set_elem *elem);
365 void (*walk)(const struct nft_ctx *ctx,
367 struct nft_set_iter *iter);
368 void * (*get)(const struct net *net,
369 const struct nft_set *set,
370 const struct nft_set_elem *elem,
373 u64 (*privsize)(const struct nlattr * const nla[],
374 const struct nft_set_desc *desc);
375 bool (*estimate)(const struct nft_set_desc *desc,
377 struct nft_set_estimate *est);
378 int (*init)(const struct nft_set *set,
379 const struct nft_set_desc *desc,
380 const struct nlattr * const nla[]);
381 void (*destroy)(const struct nft_set *set);
382 void (*gc_init)(const struct nft_set *set);
384 unsigned int elemsize;
388 * struct nft_set_type - nf_tables set type
390 * @ops: set ops for this type
391 * @features: features supported by the implementation
393 struct nft_set_type {
394 const struct nft_set_ops ops;
397 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
400 * struct nft_set - nf_tables set instance
402 * @list: table set list node
403 * @bindings: list of set bindings
404 * @table: table this set belongs to
405 * @net: netnamespace this set belongs to
406 * @name: name of the set
407 * @handle: unique handle of the set
408 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
409 * @dtype: data type (verdict or numeric type defined by userspace)
410 * @objtype: object type (see NFT_OBJECT_* definitions)
411 * @size: maximum set size
412 * @field_len: length of each field in concatenation, bytes
413 * @field_count: number of concatenated fields in element
414 * @use: number of rules references to this set
415 * @nelems: number of elements
416 * @ndeact: number of deactivated elements queued for removal
417 * @timeout: default timeout value in jiffies
418 * @gc_int: garbage collection interval in msecs
419 * @policy: set parameterization (see enum nft_set_policies)
420 * @udlen: user data length
422 * @expr: stateful expression
425 * @genmask: generation mask
428 * @data: private set data
431 struct list_head list;
432 struct list_head bindings;
433 struct nft_table *table;
441 u8 field_len[NFT_REG32_COUNT];
450 unsigned char *udata;
451 struct nft_expr *expr;
452 /* runtime data below here */
453 const struct nft_set_ops *ops ____cacheline_aligned;
459 __attribute__((aligned(__alignof__(u64))));
462 static inline bool nft_set_is_anonymous(const struct nft_set *set)
464 return set->flags & NFT_SET_ANONYMOUS;
467 static inline void *nft_set_priv(const struct nft_set *set)
469 return (void *)set->data;
472 static inline struct nft_set *nft_set_container_of(const void *priv)
474 return (void *)priv - offsetof(struct nft_set, data);
477 struct nft_set *nft_set_lookup_global(const struct net *net,
478 const struct nft_table *table,
479 const struct nlattr *nla_set_name,
480 const struct nlattr *nla_set_id,
483 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
485 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
489 * struct nft_set_binding - nf_tables set binding
491 * @list: set bindings list node
492 * @chain: chain containing the rule bound to the set
493 * @flags: set action flags
495 * A set binding contains all information necessary for validation
496 * of new elements added to a bound set.
498 struct nft_set_binding {
499 struct list_head list;
500 const struct nft_chain *chain;
504 enum nft_trans_phase;
505 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
506 struct nft_set_binding *binding,
507 enum nft_trans_phase phase);
508 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
509 struct nft_set_binding *binding);
510 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
513 * enum nft_set_extensions - set extension type IDs
515 * @NFT_SET_EXT_KEY: element key
516 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
517 * @NFT_SET_EXT_DATA: mapping data
518 * @NFT_SET_EXT_FLAGS: element flags
519 * @NFT_SET_EXT_TIMEOUT: element timeout
520 * @NFT_SET_EXT_EXPIRATION: element expiration time
521 * @NFT_SET_EXT_USERDATA: user data associated with the element
522 * @NFT_SET_EXT_EXPR: expression assiociated with the element
523 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
524 * @NFT_SET_EXT_NUM: number of extension types
526 enum nft_set_extensions {
532 NFT_SET_EXT_EXPIRATION,
533 NFT_SET_EXT_USERDATA,
540 * struct nft_set_ext_type - set extension type
542 * @len: fixed part length of the extension
543 * @align: alignment requirements of the extension
545 struct nft_set_ext_type {
550 extern const struct nft_set_ext_type nft_set_ext_types[];
553 * struct nft_set_ext_tmpl - set extension template
555 * @len: length of extension area
556 * @offset: offsets of individual extension types
558 struct nft_set_ext_tmpl {
560 u8 offset[NFT_SET_EXT_NUM];
564 * struct nft_set_ext - set extensions
566 * @genmask: generation mask
567 * @offset: offsets of individual extension types
568 * @data: beginning of extension data
572 u8 offset[NFT_SET_EXT_NUM];
576 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
578 memset(tmpl, 0, sizeof(*tmpl));
579 tmpl->len = sizeof(struct nft_set_ext);
582 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
585 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
586 BUG_ON(tmpl->len > U8_MAX);
587 tmpl->offset[id] = tmpl->len;
588 tmpl->len += nft_set_ext_types[id].len + len;
591 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
593 nft_set_ext_add_length(tmpl, id, 0);
596 static inline void nft_set_ext_init(struct nft_set_ext *ext,
597 const struct nft_set_ext_tmpl *tmpl)
599 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
602 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
604 return !!ext->offset[id];
607 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
609 return ext && __nft_set_ext_exists(ext, id);
612 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
614 return (void *)ext + ext->offset[id];
617 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
619 return nft_set_ext(ext, NFT_SET_EXT_KEY);
622 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
624 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
627 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
629 return nft_set_ext(ext, NFT_SET_EXT_DATA);
632 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
634 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
637 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
639 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
642 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
644 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
647 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
649 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
652 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
654 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
657 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
659 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
660 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
663 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
666 return elem + set->ops->elemsize;
669 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
671 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
674 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
675 const struct nft_set *set,
676 const struct nlattr *attr);
678 void *nft_set_elem_init(const struct nft_set *set,
679 const struct nft_set_ext_tmpl *tmpl,
680 const u32 *key, const u32 *key_end, const u32 *data,
681 u64 timeout, u64 expiration, gfp_t gfp);
682 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
686 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
689 * @set: set the elements belong to
690 * @cnt: count of elements
692 struct nft_set_gc_batch_head {
694 const struct nft_set *set;
698 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
699 sizeof(struct nft_set_gc_batch_head)) / \
703 * struct nft_set_gc_batch - nf_tables set garbage collection batch
705 * @head: GC batch head
706 * @elems: garbage collection elements
708 struct nft_set_gc_batch {
709 struct nft_set_gc_batch_head head;
710 void *elems[NFT_SET_GC_BATCH_SIZE];
713 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
715 void nft_set_gc_batch_release(struct rcu_head *rcu);
717 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
720 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
723 static inline struct nft_set_gc_batch *
724 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
728 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
730 nft_set_gc_batch_complete(gcb);
732 return nft_set_gc_batch_alloc(set, gfp);
735 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
738 gcb->elems[gcb->head.cnt++] = elem;
743 * struct nft_expr_type - nf_tables expression type
745 * @select_ops: function to select nft_expr_ops
746 * @release_ops: release nft_expr_ops
747 * @ops: default ops, used when no select_ops functions is present
748 * @list: used internally
750 * @owner: module reference
751 * @policy: netlink attribute policy
752 * @maxattr: highest netlink attribute number
753 * @family: address family for AF-specific types
754 * @flags: expression type flags
756 struct nft_expr_type {
757 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
758 const struct nlattr * const tb[]);
759 void (*release_ops)(const struct nft_expr_ops *ops);
760 const struct nft_expr_ops *ops;
761 struct list_head list;
763 struct module *owner;
764 const struct nla_policy *policy;
765 unsigned int maxattr;
770 #define NFT_EXPR_STATEFUL 0x1
771 #define NFT_EXPR_GC 0x2
773 enum nft_trans_phase {
780 struct nft_flow_rule;
781 struct nft_offload_ctx;
784 * struct nft_expr_ops - nf_tables expression operations
786 * @eval: Expression evaluation function
787 * @size: full expression size, including private data size
788 * @init: initialization function
789 * @activate: activate expression in the next generation
790 * @deactivate: deactivate expression in next generation
791 * @destroy: destruction function, called after synchronize_rcu
792 * @dump: function to dump parameters
793 * @type: expression type
794 * @validate: validate expression, called during loop detection
795 * @data: extra data to attach to this expression operation
798 struct nft_expr_ops {
799 void (*eval)(const struct nft_expr *expr,
800 struct nft_regs *regs,
801 const struct nft_pktinfo *pkt);
802 int (*clone)(struct nft_expr *dst,
803 const struct nft_expr *src);
806 int (*init)(const struct nft_ctx *ctx,
807 const struct nft_expr *expr,
808 const struct nlattr * const tb[]);
809 void (*activate)(const struct nft_ctx *ctx,
810 const struct nft_expr *expr);
811 void (*deactivate)(const struct nft_ctx *ctx,
812 const struct nft_expr *expr,
813 enum nft_trans_phase phase);
814 void (*destroy)(const struct nft_ctx *ctx,
815 const struct nft_expr *expr);
816 void (*destroy_clone)(const struct nft_ctx *ctx,
817 const struct nft_expr *expr);
818 int (*dump)(struct sk_buff *skb,
819 const struct nft_expr *expr);
820 int (*validate)(const struct nft_ctx *ctx,
821 const struct nft_expr *expr,
822 const struct nft_data **data);
823 bool (*gc)(struct net *net,
824 const struct nft_expr *expr);
825 int (*offload)(struct nft_offload_ctx *ctx,
826 struct nft_flow_rule *flow,
827 const struct nft_expr *expr);
829 const struct nft_expr_type *type;
833 #define NFT_EXPR_MAXATTR 16
834 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
835 ALIGN(size, __alignof__(struct nft_expr)))
838 * struct nft_expr - nf_tables expression
840 * @ops: expression ops
841 * @data: expression private data
844 const struct nft_expr_ops *ops;
846 __attribute__((aligned(__alignof__(u64))));
849 static inline void *nft_expr_priv(const struct nft_expr *expr)
851 return (void *)expr->data;
854 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
855 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
856 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
857 const struct nft_expr *expr);
860 * struct nft_rule - nf_tables rule
862 * @list: used internally
863 * @handle: rule handle
864 * @genmask: generation mask
865 * @dlen: length of expression data
866 * @udata: user data is appended to the rule
867 * @data: expression data
870 struct list_head list;
876 __attribute__((aligned(__alignof__(struct nft_expr))));
879 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
881 return (struct nft_expr *)&rule->data[0];
884 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
886 return ((void *)expr) + expr->ops->size;
889 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
891 return (struct nft_expr *)&rule->data[rule->dlen];
894 static inline bool nft_expr_more(const struct nft_rule *rule,
895 const struct nft_expr *expr)
897 return expr != nft_expr_last(rule) && expr->ops;
900 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
902 return (void *)&rule->data[rule->dlen];
905 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
907 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
908 struct nft_regs *regs,
909 const struct nft_pktinfo *pkt)
911 struct nft_expr *expr;
913 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
914 expr = nft_set_ext_expr(ext);
915 expr->ops->eval(expr, regs, pkt);
920 * The last pointer isn't really necessary, but the compiler isn't able to
921 * determine that the result of nft_expr_last() is always the same since it
922 * can't assume that the dlen value wasn't changed within calls in the loop.
924 #define nft_rule_for_each_expr(expr, last, rule) \
925 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
927 (expr) = nft_expr_next(expr))
929 #define NFT_CHAIN_POLICY_UNSET U8_MAX
932 * struct nft_chain - nf_tables chain
934 * @rules: list of rules in the chain
935 * @list: used internally
936 * @rhlhead: used internally
937 * @table: table that this chain belongs to
938 * @handle: chain handle
939 * @use: number of jump references to this chain
940 * @flags: bitmask of enum nft_chain_flags
941 * @name: name of the chain
944 struct nft_rule *__rcu *rules_gen_0;
945 struct nft_rule *__rcu *rules_gen_1;
946 struct list_head rules;
947 struct list_head list;
948 struct rhlist_head rhlhead;
949 struct nft_table *table;
959 /* Only used during control plane commit phase: */
960 struct nft_rule **rules_next;
963 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
965 enum nft_chain_types {
966 NFT_CHAIN_T_DEFAULT = 0,
973 * struct nft_chain_type - nf_tables chain type info
975 * @name: name of the type
976 * @type: numeric identifier
977 * @family: address family
978 * @owner: module owner
979 * @hook_mask: mask of valid hooks
980 * @hooks: array of hook functions
981 * @ops_register: base chain register function
982 * @ops_unregister: base chain unregister function
984 struct nft_chain_type {
986 enum nft_chain_types type;
988 struct module *owner;
989 unsigned int hook_mask;
990 nf_hookfn *hooks[NFT_MAX_HOOKS];
991 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
992 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
995 int nft_chain_validate_dependency(const struct nft_chain *chain,
996 enum nft_chain_types type);
997 int nft_chain_validate_hooks(const struct nft_chain *chain,
998 unsigned int hook_flags);
1000 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1002 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1005 void nft_chain_del(struct nft_chain *chain);
1006 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1011 struct u64_stats_sync syncp;
1015 struct list_head list;
1017 struct nf_hook_ops ops;
1018 struct rcu_head rcu;
1022 * struct nft_base_chain - nf_tables base chain
1024 * @ops: netfilter hook ops
1025 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1027 * @policy: default policy
1028 * @stats: per-cpu chain stats
1030 * @flow_block: flow block (for hardware offload)
1032 struct nft_base_chain {
1033 struct nf_hook_ops ops;
1034 struct list_head hook_list;
1035 const struct nft_chain_type *type;
1038 struct nft_stats __percpu *stats;
1039 struct nft_chain chain;
1040 struct flow_block flow_block;
1043 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1045 return container_of(chain, struct nft_base_chain, chain);
1048 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1050 return chain->flags & NFT_CHAIN_BASE;
1053 int __nft_release_basechain(struct nft_ctx *ctx);
1055 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1058 * struct nft_table - nf_tables table
1060 * @list: used internally
1061 * @chains_ht: chains in the table
1062 * @chains: same, for stable walks
1063 * @sets: sets in the table
1064 * @objects: stateful objects in the table
1065 * @flowtables: flow tables in the table
1066 * @hgenerator: handle generator state
1067 * @handle: table handle
1068 * @use: number of chain references to this table
1069 * @flags: table flag (see enum nft_table_flags)
1070 * @genmask: generation mask
1071 * @afinfo: address family info
1072 * @name: name of the table
1075 struct list_head list;
1076 struct rhltable chains_ht;
1077 struct list_head chains;
1078 struct list_head sets;
1079 struct list_head objects;
1080 struct list_head flowtables;
1092 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1094 return family == NFPROTO_NETDEV ||
1095 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1098 void nft_register_chain_type(const struct nft_chain_type *);
1099 void nft_unregister_chain_type(const struct nft_chain_type *);
1101 int nft_register_expr(struct nft_expr_type *);
1102 void nft_unregister_expr(struct nft_expr_type *);
1104 int nft_verdict_dump(struct sk_buff *skb, int type,
1105 const struct nft_verdict *v);
1108 * struct nft_object_hash_key - key to lookup nft_object
1110 * @name: name of the stateful object to look up
1111 * @table: table the object belongs to
1113 struct nft_object_hash_key {
1115 const struct nft_table *table;
1119 * struct nft_object - nf_tables stateful object
1121 * @list: table stateful object list node
1122 * @key: keys that identify this object
1123 * @rhlhead: nft_objname_ht node
1124 * @genmask: generation mask
1125 * @use: number of references to this stateful object
1126 * @handle: unique object handle
1127 * @ops: object operations
1128 * @data: object data, layout depends on type
1131 struct list_head list;
1132 struct rhlist_head rhlhead;
1133 struct nft_object_hash_key key;
1139 /* runtime data below here */
1140 const struct nft_object_ops *ops ____cacheline_aligned;
1141 unsigned char data[]
1142 __attribute__((aligned(__alignof__(u64))));
1145 static inline void *nft_obj_data(const struct nft_object *obj)
1147 return (void *)obj->data;
1150 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1152 struct nft_object *nft_obj_lookup(const struct net *net,
1153 const struct nft_table *table,
1154 const struct nlattr *nla, u32 objtype,
1157 void nft_obj_notify(struct net *net, const struct nft_table *table,
1158 struct nft_object *obj, u32 portid, u32 seq,
1159 int event, int family, int report, gfp_t gfp);
1162 * struct nft_object_type - stateful object type
1164 * @select_ops: function to select nft_object_ops
1165 * @ops: default ops, used when no select_ops functions is present
1166 * @list: list node in list of object types
1167 * @type: stateful object numeric type
1168 * @owner: module owner
1169 * @maxattr: maximum netlink attribute
1170 * @policy: netlink attribute policy
1172 struct nft_object_type {
1173 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1174 const struct nlattr * const tb[]);
1175 const struct nft_object_ops *ops;
1176 struct list_head list;
1178 unsigned int maxattr;
1179 struct module *owner;
1180 const struct nla_policy *policy;
1184 * struct nft_object_ops - stateful object operations
1186 * @eval: stateful object evaluation function
1187 * @size: stateful object size
1188 * @init: initialize object from netlink attributes
1189 * @destroy: release existing stateful object
1190 * @dump: netlink dump stateful object
1191 * @update: update stateful object
1193 struct nft_object_ops {
1194 void (*eval)(struct nft_object *obj,
1195 struct nft_regs *regs,
1196 const struct nft_pktinfo *pkt);
1198 int (*init)(const struct nft_ctx *ctx,
1199 const struct nlattr *const tb[],
1200 struct nft_object *obj);
1201 void (*destroy)(const struct nft_ctx *ctx,
1202 struct nft_object *obj);
1203 int (*dump)(struct sk_buff *skb,
1204 struct nft_object *obj,
1206 void (*update)(struct nft_object *obj,
1207 struct nft_object *newobj);
1208 const struct nft_object_type *type;
1211 int nft_register_obj(struct nft_object_type *obj_type);
1212 void nft_unregister_obj(struct nft_object_type *obj_type);
1214 #define NFT_NETDEVICE_MAX 256
1217 * struct nft_flowtable - nf_tables flow table
1219 * @list: flow table list node in table list
1220 * @table: the table the flow table is contained in
1221 * @name: name of this flow table
1222 * @hooknum: hook number
1223 * @ops_len: number of hooks in array
1224 * @genmask: generation mask
1225 * @use: number of references to this flow table
1226 * @handle: unique object handle
1227 * @dev_name: array of device names
1228 * @data: rhashtable and garbage collector
1229 * @ops: array of hooks
1231 struct nft_flowtable {
1232 struct list_head list;
1233 struct nft_table *table;
1240 /* runtime data below here */
1241 struct list_head hook_list ____cacheline_aligned;
1242 struct nf_flowtable data;
1245 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1246 const struct nlattr *nla,
1249 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1250 struct nft_flowtable *flowtable,
1251 enum nft_trans_phase phase);
1253 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1254 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1257 * struct nft_traceinfo - nft tracing information and state
1259 * @pkt: pktinfo currently processed
1260 * @basechain: base chain currently processed
1261 * @chain: chain currently processed
1262 * @rule: rule that was evaluated
1263 * @verdict: verdict given by rule
1264 * @type: event type (enum nft_trace_types)
1265 * @packet_dumped: packet headers sent in a previous traceinfo message
1266 * @trace: other struct members are initialised
1268 struct nft_traceinfo {
1269 const struct nft_pktinfo *pkt;
1270 const struct nft_base_chain *basechain;
1271 const struct nft_chain *chain;
1272 const struct nft_rule *rule;
1273 const struct nft_verdict *verdict;
1274 enum nft_trace_types type;
1279 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1280 const struct nft_verdict *verdict,
1281 const struct nft_chain *basechain);
1283 void nft_trace_notify(struct nft_traceinfo *info);
1285 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1286 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1288 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1289 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1291 #define MODULE_ALIAS_NFT_EXPR(name) \
1292 MODULE_ALIAS("nft-expr-" name)
1294 #define MODULE_ALIAS_NFT_OBJ(type) \
1295 MODULE_ALIAS("nft-obj-" __stringify(type))
1297 #if IS_ENABLED(CONFIG_NF_TABLES)
1300 * The gencursor defines two generations, the currently active and the
1301 * next one. Objects contain a bitmask of 2 bits specifying the generations
1302 * they're active in. A set bit means they're inactive in the generation
1303 * represented by that bit.
1305 * New objects start out as inactive in the current and active in the
1306 * next generation. When committing the ruleset the bitmask is cleared,
1307 * meaning they're active in all generations. When removing an object,
1308 * it is set inactive in the next generation. After committing the ruleset,
1309 * the objects are removed.
1311 static inline unsigned int nft_gencursor_next(const struct net *net)
1313 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1316 static inline u8 nft_genmask_next(const struct net *net)
1318 return 1 << nft_gencursor_next(net);
1321 static inline u8 nft_genmask_cur(const struct net *net)
1323 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1324 return 1 << READ_ONCE(net->nft.gencursor);
1327 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1330 * Generic transaction helpers
1333 /* Check if this object is currently active. */
1334 #define nft_is_active(__net, __obj) \
1335 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1337 /* Check if this object is active in the next generation. */
1338 #define nft_is_active_next(__net, __obj) \
1339 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1341 /* This object becomes active in the next generation. */
1342 #define nft_activate_next(__net, __obj) \
1343 (__obj)->genmask = nft_genmask_cur(__net)
1345 /* This object becomes inactive in the next generation. */
1346 #define nft_deactivate_next(__net, __obj) \
1347 (__obj)->genmask = nft_genmask_next(__net)
1349 /* After committing the ruleset, clear the stale generation bit. */
1350 #define nft_clear(__net, __obj) \
1351 (__obj)->genmask &= ~nft_genmask_next(__net)
1352 #define nft_active_genmask(__obj, __genmask) \
1353 !((__obj)->genmask & __genmask)
1356 * Set element transaction helpers
1359 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1362 return !(ext->genmask & genmask);
1365 static inline void nft_set_elem_change_active(const struct net *net,
1366 const struct nft_set *set,
1367 struct nft_set_ext *ext)
1369 ext->genmask ^= nft_genmask_next(net);
1372 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1375 * We use a free bit in the genmask field to indicate the element
1376 * is busy, meaning it is currently being processed either by
1377 * the netlink API or GC.
1379 * Even though the genmask is only a single byte wide, this works
1380 * because the extension structure if fully constant once initialized,
1381 * so there are no non-atomic write accesses unless it is already
1384 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1386 #if defined(__LITTLE_ENDIAN_BITFIELD)
1387 #define NFT_SET_ELEM_BUSY_BIT 2
1388 #elif defined(__BIG_ENDIAN_BITFIELD)
1389 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1394 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1396 unsigned long *word = (unsigned long *)ext;
1398 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1399 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1402 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1404 unsigned long *word = (unsigned long *)ext;
1406 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1410 * struct nft_trans - nf_tables object update in transaction
1412 * @list: used internally
1413 * @msg_type: message type
1414 * @put_net: ctx->net needs to be put
1415 * @ctx: transaction context
1416 * @data: internal information related to the transaction
1419 struct list_head list;
1426 struct nft_trans_rule {
1427 struct nft_rule *rule;
1428 struct nft_flow_rule *flow;
1432 #define nft_trans_rule(trans) \
1433 (((struct nft_trans_rule *)trans->data)->rule)
1434 #define nft_trans_flow_rule(trans) \
1435 (((struct nft_trans_rule *)trans->data)->flow)
1436 #define nft_trans_rule_id(trans) \
1437 (((struct nft_trans_rule *)trans->data)->rule_id)
1439 struct nft_trans_set {
1440 struct nft_set *set;
1445 #define nft_trans_set(trans) \
1446 (((struct nft_trans_set *)trans->data)->set)
1447 #define nft_trans_set_id(trans) \
1448 (((struct nft_trans_set *)trans->data)->set_id)
1449 #define nft_trans_set_bound(trans) \
1450 (((struct nft_trans_set *)trans->data)->bound)
1452 struct nft_trans_chain {
1455 struct nft_stats __percpu *stats;
1460 #define nft_trans_chain_update(trans) \
1461 (((struct nft_trans_chain *)trans->data)->update)
1462 #define nft_trans_chain_name(trans) \
1463 (((struct nft_trans_chain *)trans->data)->name)
1464 #define nft_trans_chain_stats(trans) \
1465 (((struct nft_trans_chain *)trans->data)->stats)
1466 #define nft_trans_chain_policy(trans) \
1467 (((struct nft_trans_chain *)trans->data)->policy)
1468 #define nft_trans_chain_id(trans) \
1469 (((struct nft_trans_chain *)trans->data)->chain_id)
1471 struct nft_trans_table {
1476 #define nft_trans_table_update(trans) \
1477 (((struct nft_trans_table *)trans->data)->update)
1478 #define nft_trans_table_enable(trans) \
1479 (((struct nft_trans_table *)trans->data)->enable)
1481 struct nft_trans_elem {
1482 struct nft_set *set;
1483 struct nft_set_elem elem;
1487 #define nft_trans_elem_set(trans) \
1488 (((struct nft_trans_elem *)trans->data)->set)
1489 #define nft_trans_elem(trans) \
1490 (((struct nft_trans_elem *)trans->data)->elem)
1491 #define nft_trans_elem_set_bound(trans) \
1492 (((struct nft_trans_elem *)trans->data)->bound)
1494 struct nft_trans_obj {
1495 struct nft_object *obj;
1496 struct nft_object *newobj;
1500 #define nft_trans_obj(trans) \
1501 (((struct nft_trans_obj *)trans->data)->obj)
1502 #define nft_trans_obj_newobj(trans) \
1503 (((struct nft_trans_obj *)trans->data)->newobj)
1504 #define nft_trans_obj_update(trans) \
1505 (((struct nft_trans_obj *)trans->data)->update)
1507 struct nft_trans_flowtable {
1508 struct nft_flowtable *flowtable;
1510 struct list_head hook_list;
1513 #define nft_trans_flowtable(trans) \
1514 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1515 #define nft_trans_flowtable_update(trans) \
1516 (((struct nft_trans_flowtable *)trans->data)->update)
1517 #define nft_trans_flowtable_hooks(trans) \
1518 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1520 int __init nft_chain_filter_init(void);
1521 void nft_chain_filter_fini(void);
1523 void __init nft_chain_route_init(void);
1524 void nft_chain_route_fini(void);
1526 void nf_tables_trans_destroy_flush_work(void);
1528 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1529 __be64 nf_jiffies64_to_msecs(u64 input);
1531 #endif /* _NET_NF_TABLES_H */