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>
19 #define NFT_JUMP_STACK_SIZE 16
25 /* for x_tables compatibility */
26 struct xt_action_param xt;
29 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
31 return pkt->xt.state->net;
34 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
36 return pkt->xt.state->hook;
39 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
41 return pkt->xt.state->pf;
44 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
46 return pkt->xt.state->in;
49 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
51 return pkt->xt.state->out;
54 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
56 const struct nf_hook_state *state)
59 pkt->xt.state = state;
62 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
65 pkt->tprot_set = false;
72 * struct nft_verdict - nf_tables verdict
74 * @code: nf_tables/netfilter verdict code
75 * @chain: destination chain for NFT_JUMP/NFT_GOTO
79 struct nft_chain *chain;
85 struct nft_verdict verdict;
87 } __attribute__((aligned(__alignof__(u64))));
90 * struct nft_regs - nf_tables register set
92 * @data: data registers
93 * @verdict: verdict register
95 * The first four data registers alias to the verdict register.
100 struct nft_verdict verdict;
104 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
106 * Note, when using concatenations, register allocation happens at 32-bit
107 * level. So for store instruction, pad the rest part with zero to avoid
111 static inline void nft_reg_store8(u32 *dreg, u8 val)
117 static inline u8 nft_reg_load8(const u32 *sreg)
122 static inline void nft_reg_store16(u32 *dreg, u16 val)
128 static inline u16 nft_reg_load16(const u32 *sreg)
133 static inline void nft_reg_store64(u32 *dreg, u64 val)
135 put_unaligned(val, (u64 *)dreg);
138 static inline u64 nft_reg_load64(const u32 *sreg)
140 return get_unaligned((u64 *)sreg);
143 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
146 memcpy(dst, src, len);
149 static inline void nft_data_debug(const struct nft_data *data)
151 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
152 data->data[0], data->data[1],
153 data->data[2], data->data[3]);
157 * struct nft_ctx - nf_tables rule/set context
159 * @net: net namespace
160 * @table: the table the chain is contained in
161 * @chain: the chain the rule is contained in
162 * @nla: netlink attributes
163 * @portid: netlink portID of the original message
164 * @seq: netlink sequence number
165 * @family: protocol family
166 * @level: depth of the chains
167 * @report: notify via unicast netlink message
171 struct nft_table *table;
172 struct nft_chain *chain;
173 const struct nlattr * const *nla;
182 struct nft_data_desc {
183 enum nft_data_types type;
187 int nft_data_init(const struct nft_ctx *ctx,
188 struct nft_data *data, unsigned int size,
189 struct nft_data_desc *desc, const struct nlattr *nla);
190 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
191 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
192 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
193 enum nft_data_types type, unsigned int len);
195 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
197 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
200 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
202 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
205 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
206 unsigned int nft_parse_register(const struct nlattr *attr);
207 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
209 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
210 int nft_validate_register_store(const struct nft_ctx *ctx,
211 enum nft_registers reg,
212 const struct nft_data *data,
213 enum nft_data_types type, unsigned int len);
216 * struct nft_userdata - user defined data associated with an object
218 * @len: length of the data
221 * The presence of user data is indicated in an object specific fashion,
222 * so a length of zero can't occur and the value "len" indicates data
225 struct nft_userdata {
227 unsigned char data[];
231 * struct nft_set_elem - generic representation of set elements
234 * @key_end: closing element key
235 * @priv: element private data and extensions
237 struct nft_set_elem {
239 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
243 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
247 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
254 struct nft_set_iter {
259 int (*fn)(const struct nft_ctx *ctx,
261 const struct nft_set_iter *iter,
262 struct nft_set_elem *elem);
266 * struct nft_set_desc - description of set elements
270 * @size: number of set elements
271 * @field_len: length of each field in concatenation, bytes
272 * @field_count: number of concatenated fields in element
273 * @expr: set must support for expressions
275 struct nft_set_desc {
279 u8 field_len[NFT_REG32_COUNT];
285 * enum nft_set_class - performance class
287 * @NFT_LOOKUP_O_1: constant, O(1)
288 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
289 * @NFT_LOOKUP_O_N: linear, O(N)
293 NFT_SET_CLASS_O_LOG_N,
298 * struct nft_set_estimate - estimation of memory and performance
301 * @size: required memory
302 * @lookup: lookup performance class
303 * @space: memory class
305 struct nft_set_estimate {
307 enum nft_set_class lookup;
308 enum nft_set_class space;
315 * struct nft_set_ops - nf_tables set operations
317 * @lookup: look up an element within the set
318 * @update: update an element if exists, add it if doesn't exist
319 * @delete: delete an element
320 * @insert: insert new element into set
321 * @activate: activate new element in the next generation
322 * @deactivate: lookup for element and deactivate it in the next generation
323 * @flush: deactivate element in the next generation
324 * @remove: remove element from set
325 * @walk: iterate over all set elements
326 * @get: get set elements
327 * @privsize: function to return size of set private data
328 * @init: initialize private data of new set instance
329 * @destroy: destroy private data of set instance
330 * @elemsize: element private size
332 * Operations lookup, update and delete have simpler interfaces, are faster
333 * and currently only used in the packet path. All the rest are slower,
334 * control plane functions.
337 bool (*lookup)(const struct net *net,
338 const struct nft_set *set,
340 const struct nft_set_ext **ext);
341 bool (*update)(struct nft_set *set,
343 void *(*new)(struct nft_set *,
344 const struct nft_expr *,
346 const struct nft_expr *expr,
347 struct nft_regs *regs,
348 const struct nft_set_ext **ext);
349 bool (*delete)(const struct nft_set *set,
352 int (*insert)(const struct net *net,
353 const struct nft_set *set,
354 const struct nft_set_elem *elem,
355 struct nft_set_ext **ext);
356 void (*activate)(const struct net *net,
357 const struct nft_set *set,
358 const struct nft_set_elem *elem);
359 void * (*deactivate)(const struct net *net,
360 const struct nft_set *set,
361 const struct nft_set_elem *elem);
362 bool (*flush)(const struct net *net,
363 const struct nft_set *set,
365 void (*remove)(const struct net *net,
366 const struct nft_set *set,
367 const struct nft_set_elem *elem);
368 void (*walk)(const struct nft_ctx *ctx,
370 struct nft_set_iter *iter);
371 void * (*get)(const struct net *net,
372 const struct nft_set *set,
373 const struct nft_set_elem *elem,
376 u64 (*privsize)(const struct nlattr * const nla[],
377 const struct nft_set_desc *desc);
378 bool (*estimate)(const struct nft_set_desc *desc,
380 struct nft_set_estimate *est);
381 int (*init)(const struct nft_set *set,
382 const struct nft_set_desc *desc,
383 const struct nlattr * const nla[]);
384 void (*destroy)(const struct nft_set *set);
385 void (*gc_init)(const struct nft_set *set);
387 unsigned int elemsize;
391 * struct nft_set_type - nf_tables set type
393 * @ops: set ops for this type
394 * @features: features supported by the implementation
396 struct nft_set_type {
397 const struct nft_set_ops ops;
400 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
403 * struct nft_set - nf_tables set instance
405 * @list: table set list node
406 * @bindings: list of set bindings
407 * @table: table this set belongs to
408 * @net: netnamespace this set belongs to
409 * @name: name of the set
410 * @handle: unique handle of the set
411 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
412 * @dtype: data type (verdict or numeric type defined by userspace)
413 * @objtype: object type (see NFT_OBJECT_* definitions)
414 * @size: maximum set size
415 * @field_len: length of each field in concatenation, bytes
416 * @field_count: number of concatenated fields in element
417 * @use: number of rules references to this set
418 * @nelems: number of elements
419 * @ndeact: number of deactivated elements queued for removal
420 * @timeout: default timeout value in jiffies
421 * @gc_int: garbage collection interval in msecs
422 * @policy: set parameterization (see enum nft_set_policies)
423 * @udlen: user data length
425 * @expr: stateful expression
428 * @genmask: generation mask
431 * @data: private set data
434 struct list_head list;
435 struct list_head bindings;
436 struct nft_table *table;
444 u8 field_len[NFT_REG32_COUNT];
453 unsigned char *udata;
454 struct nft_expr *expr;
455 /* runtime data below here */
456 const struct nft_set_ops *ops ____cacheline_aligned;
462 __attribute__((aligned(__alignof__(u64))));
465 static inline bool nft_set_is_anonymous(const struct nft_set *set)
467 return set->flags & NFT_SET_ANONYMOUS;
470 static inline void *nft_set_priv(const struct nft_set *set)
472 return (void *)set->data;
475 static inline struct nft_set *nft_set_container_of(const void *priv)
477 return (void *)priv - offsetof(struct nft_set, data);
480 struct nft_set *nft_set_lookup_global(const struct net *net,
481 const struct nft_table *table,
482 const struct nlattr *nla_set_name,
483 const struct nlattr *nla_set_id,
486 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
488 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
492 * struct nft_set_binding - nf_tables set binding
494 * @list: set bindings list node
495 * @chain: chain containing the rule bound to the set
496 * @flags: set action flags
498 * A set binding contains all information necessary for validation
499 * of new elements added to a bound set.
501 struct nft_set_binding {
502 struct list_head list;
503 const struct nft_chain *chain;
507 enum nft_trans_phase;
508 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
509 struct nft_set_binding *binding,
510 enum nft_trans_phase phase);
511 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
512 struct nft_set_binding *binding);
513 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
516 * enum nft_set_extensions - set extension type IDs
518 * @NFT_SET_EXT_KEY: element key
519 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
520 * @NFT_SET_EXT_DATA: mapping data
521 * @NFT_SET_EXT_FLAGS: element flags
522 * @NFT_SET_EXT_TIMEOUT: element timeout
523 * @NFT_SET_EXT_EXPIRATION: element expiration time
524 * @NFT_SET_EXT_USERDATA: user data associated with the element
525 * @NFT_SET_EXT_EXPR: expression assiociated with the element
526 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
527 * @NFT_SET_EXT_NUM: number of extension types
529 enum nft_set_extensions {
535 NFT_SET_EXT_EXPIRATION,
536 NFT_SET_EXT_USERDATA,
543 * struct nft_set_ext_type - set extension type
545 * @len: fixed part length of the extension
546 * @align: alignment requirements of the extension
548 struct nft_set_ext_type {
553 extern const struct nft_set_ext_type nft_set_ext_types[];
556 * struct nft_set_ext_tmpl - set extension template
558 * @len: length of extension area
559 * @offset: offsets of individual extension types
561 struct nft_set_ext_tmpl {
563 u8 offset[NFT_SET_EXT_NUM];
567 * struct nft_set_ext - set extensions
569 * @genmask: generation mask
570 * @offset: offsets of individual extension types
571 * @data: beginning of extension data
575 u8 offset[NFT_SET_EXT_NUM];
579 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
581 memset(tmpl, 0, sizeof(*tmpl));
582 tmpl->len = sizeof(struct nft_set_ext);
585 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
588 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
589 BUG_ON(tmpl->len > U8_MAX);
590 tmpl->offset[id] = tmpl->len;
591 tmpl->len += nft_set_ext_types[id].len + len;
594 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
596 nft_set_ext_add_length(tmpl, id, 0);
599 static inline void nft_set_ext_init(struct nft_set_ext *ext,
600 const struct nft_set_ext_tmpl *tmpl)
602 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
605 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
607 return !!ext->offset[id];
610 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
612 return ext && __nft_set_ext_exists(ext, id);
615 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
617 return (void *)ext + ext->offset[id];
620 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
622 return nft_set_ext(ext, NFT_SET_EXT_KEY);
625 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
627 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
630 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
632 return nft_set_ext(ext, NFT_SET_EXT_DATA);
635 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
637 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
640 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
642 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
645 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
647 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
650 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
652 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
655 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
657 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
660 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
662 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
663 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
666 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
669 return elem + set->ops->elemsize;
672 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
674 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
677 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
678 const struct nft_set *set,
679 const struct nlattr *attr);
681 void *nft_set_elem_init(const struct nft_set *set,
682 const struct nft_set_ext_tmpl *tmpl,
683 const u32 *key, const u32 *key_end, const u32 *data,
684 u64 timeout, u64 expiration, gfp_t gfp);
685 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
689 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
692 * @set: set the elements belong to
693 * @cnt: count of elements
695 struct nft_set_gc_batch_head {
697 const struct nft_set *set;
701 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
702 sizeof(struct nft_set_gc_batch_head)) / \
706 * struct nft_set_gc_batch - nf_tables set garbage collection batch
708 * @head: GC batch head
709 * @elems: garbage collection elements
711 struct nft_set_gc_batch {
712 struct nft_set_gc_batch_head head;
713 void *elems[NFT_SET_GC_BATCH_SIZE];
716 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
718 void nft_set_gc_batch_release(struct rcu_head *rcu);
720 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
723 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
726 static inline struct nft_set_gc_batch *
727 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
731 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
733 nft_set_gc_batch_complete(gcb);
735 return nft_set_gc_batch_alloc(set, gfp);
738 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
741 gcb->elems[gcb->head.cnt++] = elem;
746 * struct nft_expr_type - nf_tables expression type
748 * @select_ops: function to select nft_expr_ops
749 * @release_ops: release nft_expr_ops
750 * @ops: default ops, used when no select_ops functions is present
751 * @list: used internally
753 * @owner: module reference
754 * @policy: netlink attribute policy
755 * @maxattr: highest netlink attribute number
756 * @family: address family for AF-specific types
757 * @flags: expression type flags
759 struct nft_expr_type {
760 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
761 const struct nlattr * const tb[]);
762 void (*release_ops)(const struct nft_expr_ops *ops);
763 const struct nft_expr_ops *ops;
764 struct list_head list;
766 struct module *owner;
767 const struct nla_policy *policy;
768 unsigned int maxattr;
773 #define NFT_EXPR_STATEFUL 0x1
774 #define NFT_EXPR_GC 0x2
776 enum nft_trans_phase {
783 struct nft_flow_rule;
784 struct nft_offload_ctx;
787 * struct nft_expr_ops - nf_tables expression operations
789 * @eval: Expression evaluation function
790 * @size: full expression size, including private data size
791 * @init: initialization function
792 * @activate: activate expression in the next generation
793 * @deactivate: deactivate expression in next generation
794 * @destroy: destruction function, called after synchronize_rcu
795 * @dump: function to dump parameters
796 * @type: expression type
797 * @validate: validate expression, called during loop detection
798 * @data: extra data to attach to this expression operation
801 struct nft_expr_ops {
802 void (*eval)(const struct nft_expr *expr,
803 struct nft_regs *regs,
804 const struct nft_pktinfo *pkt);
805 int (*clone)(struct nft_expr *dst,
806 const struct nft_expr *src);
809 int (*init)(const struct nft_ctx *ctx,
810 const struct nft_expr *expr,
811 const struct nlattr * const tb[]);
812 void (*activate)(const struct nft_ctx *ctx,
813 const struct nft_expr *expr);
814 void (*deactivate)(const struct nft_ctx *ctx,
815 const struct nft_expr *expr,
816 enum nft_trans_phase phase);
817 void (*destroy)(const struct nft_ctx *ctx,
818 const struct nft_expr *expr);
819 void (*destroy_clone)(const struct nft_ctx *ctx,
820 const struct nft_expr *expr);
821 int (*dump)(struct sk_buff *skb,
822 const struct nft_expr *expr);
823 int (*validate)(const struct nft_ctx *ctx,
824 const struct nft_expr *expr,
825 const struct nft_data **data);
826 bool (*gc)(struct net *net,
827 const struct nft_expr *expr);
828 int (*offload)(struct nft_offload_ctx *ctx,
829 struct nft_flow_rule *flow,
830 const struct nft_expr *expr);
832 const struct nft_expr_type *type;
836 #define NFT_EXPR_MAXATTR 16
837 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
838 ALIGN(size, __alignof__(struct nft_expr)))
841 * struct nft_expr - nf_tables expression
843 * @ops: expression ops
844 * @data: expression private data
847 const struct nft_expr_ops *ops;
849 __attribute__((aligned(__alignof__(u64))));
852 static inline void *nft_expr_priv(const struct nft_expr *expr)
854 return (void *)expr->data;
857 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
858 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
859 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
860 const struct nft_expr *expr);
863 * struct nft_rule - nf_tables rule
865 * @list: used internally
866 * @handle: rule handle
867 * @genmask: generation mask
868 * @dlen: length of expression data
869 * @udata: user data is appended to the rule
870 * @data: expression data
873 struct list_head list;
879 __attribute__((aligned(__alignof__(struct nft_expr))));
882 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
884 return (struct nft_expr *)&rule->data[0];
887 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
889 return ((void *)expr) + expr->ops->size;
892 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
894 return (struct nft_expr *)&rule->data[rule->dlen];
897 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
899 return (void *)&rule->data[rule->dlen];
902 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
904 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
905 struct nft_regs *regs,
906 const struct nft_pktinfo *pkt)
908 struct nft_expr *expr;
910 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPR)) {
911 expr = nft_set_ext_expr(ext);
912 expr->ops->eval(expr, regs, pkt);
917 * The last pointer isn't really necessary, but the compiler isn't able to
918 * determine that the result of nft_expr_last() is always the same since it
919 * can't assume that the dlen value wasn't changed within calls in the loop.
921 #define nft_rule_for_each_expr(expr, last, rule) \
922 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
924 (expr) = nft_expr_next(expr))
926 #define NFT_CHAIN_POLICY_UNSET U8_MAX
929 * struct nft_chain - nf_tables chain
931 * @rules: list of rules in the chain
932 * @list: used internally
933 * @rhlhead: used internally
934 * @table: table that this chain belongs to
935 * @handle: chain handle
936 * @use: number of jump references to this chain
937 * @flags: bitmask of enum nft_chain_flags
938 * @name: name of the chain
941 struct nft_rule *__rcu *rules_gen_0;
942 struct nft_rule *__rcu *rules_gen_1;
943 struct list_head rules;
944 struct list_head list;
945 struct rhlist_head rhlhead;
946 struct nft_table *table;
954 /* Only used during control plane commit phase: */
955 struct nft_rule **rules_next;
958 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
960 enum nft_chain_types {
961 NFT_CHAIN_T_DEFAULT = 0,
968 * struct nft_chain_type - nf_tables chain type info
970 * @name: name of the type
971 * @type: numeric identifier
972 * @family: address family
973 * @owner: module owner
974 * @hook_mask: mask of valid hooks
975 * @hooks: array of hook functions
976 * @ops_register: base chain register function
977 * @ops_unregister: base chain unregister function
979 struct nft_chain_type {
981 enum nft_chain_types type;
983 struct module *owner;
984 unsigned int hook_mask;
985 nf_hookfn *hooks[NF_MAX_HOOKS];
986 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
987 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
990 int nft_chain_validate_dependency(const struct nft_chain *chain,
991 enum nft_chain_types type);
992 int nft_chain_validate_hooks(const struct nft_chain *chain,
993 unsigned int hook_flags);
995 static inline bool nft_chain_is_bound(struct nft_chain *chain)
997 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1000 void nft_chain_del(struct nft_chain *chain);
1001 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1006 struct u64_stats_sync syncp;
1010 struct list_head list;
1012 struct nf_hook_ops ops;
1013 struct rcu_head rcu;
1017 * struct nft_base_chain - nf_tables base chain
1019 * @ops: netfilter hook ops
1020 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1022 * @policy: default policy
1023 * @stats: per-cpu chain stats
1025 * @flow_block: flow block (for hardware offload)
1027 struct nft_base_chain {
1028 struct nf_hook_ops ops;
1029 struct list_head hook_list;
1030 const struct nft_chain_type *type;
1033 struct nft_stats __percpu *stats;
1034 struct nft_chain chain;
1035 struct flow_block flow_block;
1038 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1040 return container_of(chain, struct nft_base_chain, chain);
1043 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1045 return chain->flags & NFT_CHAIN_BASE;
1048 int __nft_release_basechain(struct nft_ctx *ctx);
1050 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1053 * struct nft_table - nf_tables table
1055 * @list: used internally
1056 * @chains_ht: chains in the table
1057 * @chains: same, for stable walks
1058 * @sets: sets in the table
1059 * @objects: stateful objects in the table
1060 * @flowtables: flow tables in the table
1061 * @hgenerator: handle generator state
1062 * @handle: table handle
1063 * @use: number of chain references to this table
1064 * @flags: table flag (see enum nft_table_flags)
1065 * @genmask: generation mask
1066 * @afinfo: address family info
1067 * @name: name of the table
1070 struct list_head list;
1071 struct rhltable chains_ht;
1072 struct list_head chains;
1073 struct list_head sets;
1074 struct list_head objects;
1075 struct list_head flowtables;
1085 void nft_register_chain_type(const struct nft_chain_type *);
1086 void nft_unregister_chain_type(const struct nft_chain_type *);
1088 int nft_register_expr(struct nft_expr_type *);
1089 void nft_unregister_expr(struct nft_expr_type *);
1091 int nft_verdict_dump(struct sk_buff *skb, int type,
1092 const struct nft_verdict *v);
1095 * struct nft_object_hash_key - key to lookup nft_object
1097 * @name: name of the stateful object to look up
1098 * @table: table the object belongs to
1100 struct nft_object_hash_key {
1102 const struct nft_table *table;
1106 * struct nft_object - nf_tables stateful object
1108 * @list: table stateful object list node
1109 * @key: keys that identify this object
1110 * @rhlhead: nft_objname_ht node
1111 * @genmask: generation mask
1112 * @use: number of references to this stateful object
1113 * @handle: unique object handle
1114 * @ops: object operations
1115 * @data: object data, layout depends on type
1118 struct list_head list;
1119 struct rhlist_head rhlhead;
1120 struct nft_object_hash_key key;
1124 /* runtime data below here */
1125 const struct nft_object_ops *ops ____cacheline_aligned;
1126 unsigned char data[]
1127 __attribute__((aligned(__alignof__(u64))));
1130 static inline void *nft_obj_data(const struct nft_object *obj)
1132 return (void *)obj->data;
1135 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1137 struct nft_object *nft_obj_lookup(const struct net *net,
1138 const struct nft_table *table,
1139 const struct nlattr *nla, u32 objtype,
1142 void nft_obj_notify(struct net *net, const struct nft_table *table,
1143 struct nft_object *obj, u32 portid, u32 seq,
1144 int event, int family, int report, gfp_t gfp);
1147 * struct nft_object_type - stateful object type
1149 * @select_ops: function to select nft_object_ops
1150 * @ops: default ops, used when no select_ops functions is present
1151 * @list: list node in list of object types
1152 * @type: stateful object numeric type
1153 * @owner: module owner
1154 * @maxattr: maximum netlink attribute
1155 * @policy: netlink attribute policy
1157 struct nft_object_type {
1158 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1159 const struct nlattr * const tb[]);
1160 const struct nft_object_ops *ops;
1161 struct list_head list;
1163 unsigned int maxattr;
1164 struct module *owner;
1165 const struct nla_policy *policy;
1169 * struct nft_object_ops - stateful object operations
1171 * @eval: stateful object evaluation function
1172 * @size: stateful object size
1173 * @init: initialize object from netlink attributes
1174 * @destroy: release existing stateful object
1175 * @dump: netlink dump stateful object
1176 * @update: update stateful object
1178 struct nft_object_ops {
1179 void (*eval)(struct nft_object *obj,
1180 struct nft_regs *regs,
1181 const struct nft_pktinfo *pkt);
1183 int (*init)(const struct nft_ctx *ctx,
1184 const struct nlattr *const tb[],
1185 struct nft_object *obj);
1186 void (*destroy)(const struct nft_ctx *ctx,
1187 struct nft_object *obj);
1188 int (*dump)(struct sk_buff *skb,
1189 struct nft_object *obj,
1191 void (*update)(struct nft_object *obj,
1192 struct nft_object *newobj);
1193 const struct nft_object_type *type;
1196 int nft_register_obj(struct nft_object_type *obj_type);
1197 void nft_unregister_obj(struct nft_object_type *obj_type);
1199 #define NFT_NETDEVICE_MAX 256
1202 * struct nft_flowtable - nf_tables flow table
1204 * @list: flow table list node in table list
1205 * @table: the table the flow table is contained in
1206 * @name: name of this flow table
1207 * @hooknum: hook number
1208 * @ops_len: number of hooks in array
1209 * @genmask: generation mask
1210 * @use: number of references to this flow table
1211 * @handle: unique object handle
1212 * @dev_name: array of device names
1213 * @data: rhashtable and garbage collector
1214 * @ops: array of hooks
1216 struct nft_flowtable {
1217 struct list_head list;
1218 struct nft_table *table;
1225 /* runtime data below here */
1226 struct list_head hook_list ____cacheline_aligned;
1227 struct nf_flowtable data;
1230 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1231 const struct nlattr *nla,
1234 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1235 struct nft_flowtable *flowtable,
1236 enum nft_trans_phase phase);
1238 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1239 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1242 * struct nft_traceinfo - nft tracing information and state
1244 * @pkt: pktinfo currently processed
1245 * @basechain: base chain currently processed
1246 * @chain: chain currently processed
1247 * @rule: rule that was evaluated
1248 * @verdict: verdict given by rule
1249 * @type: event type (enum nft_trace_types)
1250 * @packet_dumped: packet headers sent in a previous traceinfo message
1251 * @trace: other struct members are initialised
1253 struct nft_traceinfo {
1254 const struct nft_pktinfo *pkt;
1255 const struct nft_base_chain *basechain;
1256 const struct nft_chain *chain;
1257 const struct nft_rule *rule;
1258 const struct nft_verdict *verdict;
1259 enum nft_trace_types type;
1264 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1265 const struct nft_verdict *verdict,
1266 const struct nft_chain *basechain);
1268 void nft_trace_notify(struct nft_traceinfo *info);
1270 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1271 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1273 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1274 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1276 #define MODULE_ALIAS_NFT_EXPR(name) \
1277 MODULE_ALIAS("nft-expr-" name)
1279 #define MODULE_ALIAS_NFT_OBJ(type) \
1280 MODULE_ALIAS("nft-obj-" __stringify(type))
1282 #if IS_ENABLED(CONFIG_NF_TABLES)
1285 * The gencursor defines two generations, the currently active and the
1286 * next one. Objects contain a bitmask of 2 bits specifying the generations
1287 * they're active in. A set bit means they're inactive in the generation
1288 * represented by that bit.
1290 * New objects start out as inactive in the current and active in the
1291 * next generation. When committing the ruleset the bitmask is cleared,
1292 * meaning they're active in all generations. When removing an object,
1293 * it is set inactive in the next generation. After committing the ruleset,
1294 * the objects are removed.
1296 static inline unsigned int nft_gencursor_next(const struct net *net)
1298 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1301 static inline u8 nft_genmask_next(const struct net *net)
1303 return 1 << nft_gencursor_next(net);
1306 static inline u8 nft_genmask_cur(const struct net *net)
1308 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1309 return 1 << READ_ONCE(net->nft.gencursor);
1312 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1315 * Generic transaction helpers
1318 /* Check if this object is currently active. */
1319 #define nft_is_active(__net, __obj) \
1320 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1322 /* Check if this object is active in the next generation. */
1323 #define nft_is_active_next(__net, __obj) \
1324 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1326 /* This object becomes active in the next generation. */
1327 #define nft_activate_next(__net, __obj) \
1328 (__obj)->genmask = nft_genmask_cur(__net)
1330 /* This object becomes inactive in the next generation. */
1331 #define nft_deactivate_next(__net, __obj) \
1332 (__obj)->genmask = nft_genmask_next(__net)
1334 /* After committing the ruleset, clear the stale generation bit. */
1335 #define nft_clear(__net, __obj) \
1336 (__obj)->genmask &= ~nft_genmask_next(__net)
1337 #define nft_active_genmask(__obj, __genmask) \
1338 !((__obj)->genmask & __genmask)
1341 * Set element transaction helpers
1344 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1347 return !(ext->genmask & genmask);
1350 static inline void nft_set_elem_change_active(const struct net *net,
1351 const struct nft_set *set,
1352 struct nft_set_ext *ext)
1354 ext->genmask ^= nft_genmask_next(net);
1357 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1360 * We use a free bit in the genmask field to indicate the element
1361 * is busy, meaning it is currently being processed either by
1362 * the netlink API or GC.
1364 * Even though the genmask is only a single byte wide, this works
1365 * because the extension structure if fully constant once initialized,
1366 * so there are no non-atomic write accesses unless it is already
1369 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1371 #if defined(__LITTLE_ENDIAN_BITFIELD)
1372 #define NFT_SET_ELEM_BUSY_BIT 2
1373 #elif defined(__BIG_ENDIAN_BITFIELD)
1374 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1379 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1381 unsigned long *word = (unsigned long *)ext;
1383 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1384 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1387 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1389 unsigned long *word = (unsigned long *)ext;
1391 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1395 * struct nft_trans - nf_tables object update in transaction
1397 * @list: used internally
1398 * @msg_type: message type
1399 * @put_net: ctx->net needs to be put
1400 * @ctx: transaction context
1401 * @data: internal information related to the transaction
1404 struct list_head list;
1411 struct nft_trans_rule {
1412 struct nft_rule *rule;
1413 struct nft_flow_rule *flow;
1417 #define nft_trans_rule(trans) \
1418 (((struct nft_trans_rule *)trans->data)->rule)
1419 #define nft_trans_flow_rule(trans) \
1420 (((struct nft_trans_rule *)trans->data)->flow)
1421 #define nft_trans_rule_id(trans) \
1422 (((struct nft_trans_rule *)trans->data)->rule_id)
1424 struct nft_trans_set {
1425 struct nft_set *set;
1430 #define nft_trans_set(trans) \
1431 (((struct nft_trans_set *)trans->data)->set)
1432 #define nft_trans_set_id(trans) \
1433 (((struct nft_trans_set *)trans->data)->set_id)
1434 #define nft_trans_set_bound(trans) \
1435 (((struct nft_trans_set *)trans->data)->bound)
1437 struct nft_trans_chain {
1440 struct nft_stats __percpu *stats;
1445 #define nft_trans_chain_update(trans) \
1446 (((struct nft_trans_chain *)trans->data)->update)
1447 #define nft_trans_chain_name(trans) \
1448 (((struct nft_trans_chain *)trans->data)->name)
1449 #define nft_trans_chain_stats(trans) \
1450 (((struct nft_trans_chain *)trans->data)->stats)
1451 #define nft_trans_chain_policy(trans) \
1452 (((struct nft_trans_chain *)trans->data)->policy)
1453 #define nft_trans_chain_id(trans) \
1454 (((struct nft_trans_chain *)trans->data)->chain_id)
1456 struct nft_trans_table {
1461 #define nft_trans_table_update(trans) \
1462 (((struct nft_trans_table *)trans->data)->update)
1463 #define nft_trans_table_enable(trans) \
1464 (((struct nft_trans_table *)trans->data)->enable)
1466 struct nft_trans_elem {
1467 struct nft_set *set;
1468 struct nft_set_elem elem;
1472 #define nft_trans_elem_set(trans) \
1473 (((struct nft_trans_elem *)trans->data)->set)
1474 #define nft_trans_elem(trans) \
1475 (((struct nft_trans_elem *)trans->data)->elem)
1476 #define nft_trans_elem_set_bound(trans) \
1477 (((struct nft_trans_elem *)trans->data)->bound)
1479 struct nft_trans_obj {
1480 struct nft_object *obj;
1481 struct nft_object *newobj;
1485 #define nft_trans_obj(trans) \
1486 (((struct nft_trans_obj *)trans->data)->obj)
1487 #define nft_trans_obj_newobj(trans) \
1488 (((struct nft_trans_obj *)trans->data)->newobj)
1489 #define nft_trans_obj_update(trans) \
1490 (((struct nft_trans_obj *)trans->data)->update)
1492 struct nft_trans_flowtable {
1493 struct nft_flowtable *flowtable;
1495 struct list_head hook_list;
1498 #define nft_trans_flowtable(trans) \
1499 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1500 #define nft_trans_flowtable_update(trans) \
1501 (((struct nft_trans_flowtable *)trans->data)->update)
1502 #define nft_trans_flowtable_hooks(trans) \
1503 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1505 int __init nft_chain_filter_init(void);
1506 void nft_chain_filter_fini(void);
1508 void __init nft_chain_route_init(void);
1509 void nft_chain_route_fini(void);
1511 void nf_tables_trans_destroy_flush_work(void);
1512 #endif /* _NET_NF_TABLES_H */