1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <linux/list.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter/nfnetlink.h>
8 #include <linux/netfilter/x_tables.h>
9 #include <linux/netfilter/nf_tables.h>
10 #include <linux/u64_stats_sync.h>
11 #include <linux/rhashtable.h>
12 #include <net/netfilter/nf_flow_table.h>
13 #include <net/netlink.h>
14 #include <net/flow_offload.h>
18 #define NFT_JUMP_STACK_SIZE 16
24 /* for x_tables compatibility */
25 struct xt_action_param xt;
28 #if IS_ENABLED(CONFIG_NETFILTER)
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;
63 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt,
66 pkt->tprot_set = false;
73 * struct nft_verdict - nf_tables verdict
75 * @code: nf_tables/netfilter verdict code
76 * @chain: destination chain for NFT_JUMP/NFT_GOTO
80 struct nft_chain *chain;
86 struct nft_verdict verdict;
88 } __attribute__((aligned(__alignof__(u64))));
91 * struct nft_regs - nf_tables register set
93 * @data: data registers
94 * @verdict: verdict register
96 * The first four data registers alias to the verdict register.
101 struct nft_verdict verdict;
105 /* Store/load an u16 or u8 integer to/from the u32 data register.
107 * Note, when using concatenations, register allocation happens at 32-bit
108 * level. So for store instruction, pad the rest part with zero to avoid
112 static inline void nft_reg_store16(u32 *dreg, u16 val)
118 static inline void nft_reg_store8(u32 *dreg, u8 val)
124 static inline u16 nft_reg_load16(u32 *sreg)
129 static inline u8 nft_reg_load8(u32 *sreg)
134 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
137 memcpy(dst, src, len);
140 static inline void nft_data_debug(const struct nft_data *data)
142 pr_debug("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n",
143 data->data[0], data->data[1],
144 data->data[2], data->data[3]);
148 * struct nft_ctx - nf_tables rule/set context
150 * @net: net namespace
151 * @table: the table the chain is contained in
152 * @chain: the chain the rule is contained in
153 * @nla: netlink attributes
154 * @portid: netlink portID of the original message
155 * @seq: netlink sequence number
156 * @family: protocol family
157 * @level: depth of the chains
158 * @report: notify via unicast netlink message
162 struct nft_table *table;
163 struct nft_chain *chain;
164 const struct nlattr * const *nla;
173 struct nft_data_desc {
174 enum nft_data_types type;
178 int nft_data_init(const struct nft_ctx *ctx,
179 struct nft_data *data, unsigned int size,
180 struct nft_data_desc *desc, const struct nlattr *nla);
181 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
182 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
183 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
184 enum nft_data_types type, unsigned int len);
186 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
188 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
191 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
193 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
196 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
197 unsigned int nft_parse_register(const struct nlattr *attr);
198 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
200 int nft_validate_register_load(enum nft_registers reg, unsigned int len);
201 int nft_validate_register_store(const struct nft_ctx *ctx,
202 enum nft_registers reg,
203 const struct nft_data *data,
204 enum nft_data_types type, unsigned int len);
207 * struct nft_userdata - user defined data associated with an object
209 * @len: length of the data
212 * The presence of user data is indicated in an object specific fashion,
213 * so a length of zero can't occur and the value "len" indicates data
216 struct nft_userdata {
218 unsigned char data[0];
222 * struct nft_set_elem - generic representation of set elements
225 * @priv: element private data and extensions
227 struct nft_set_elem {
229 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
236 struct nft_set_iter {
241 int (*fn)(const struct nft_ctx *ctx,
243 const struct nft_set_iter *iter,
244 struct nft_set_elem *elem);
248 * struct nft_set_desc - description of set elements
252 * @size: number of set elements
254 struct nft_set_desc {
261 * enum nft_set_class - performance class
263 * @NFT_LOOKUP_O_1: constant, O(1)
264 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
265 * @NFT_LOOKUP_O_N: linear, O(N)
269 NFT_SET_CLASS_O_LOG_N,
274 * struct nft_set_estimate - estimation of memory and performance
277 * @size: required memory
278 * @lookup: lookup performance class
279 * @space: memory class
281 struct nft_set_estimate {
283 enum nft_set_class lookup;
284 enum nft_set_class space;
291 * struct nft_set_ops - nf_tables set operations
293 * @lookup: look up an element within the set
294 * @insert: insert new element into set
295 * @activate: activate new element in the next generation
296 * @deactivate: lookup for element and deactivate it in the next generation
297 * @flush: deactivate element in the next generation
298 * @remove: remove element from set
299 * @walk: iterate over all set elemeennts
300 * @get: get set elements
301 * @privsize: function to return size of set private data
302 * @init: initialize private data of new set instance
303 * @destroy: destroy private data of set instance
304 * @elemsize: element private size
307 bool (*lookup)(const struct net *net,
308 const struct nft_set *set,
310 const struct nft_set_ext **ext);
311 bool (*update)(struct nft_set *set,
313 void *(*new)(struct nft_set *,
314 const struct nft_expr *,
316 const struct nft_expr *expr,
317 struct nft_regs *regs,
318 const struct nft_set_ext **ext);
320 int (*insert)(const struct net *net,
321 const struct nft_set *set,
322 const struct nft_set_elem *elem,
323 struct nft_set_ext **ext);
324 void (*activate)(const struct net *net,
325 const struct nft_set *set,
326 const struct nft_set_elem *elem);
327 void * (*deactivate)(const struct net *net,
328 const struct nft_set *set,
329 const struct nft_set_elem *elem);
330 bool (*flush)(const struct net *net,
331 const struct nft_set *set,
333 void (*remove)(const struct net *net,
334 const struct nft_set *set,
335 const struct nft_set_elem *elem);
336 void (*walk)(const struct nft_ctx *ctx,
338 struct nft_set_iter *iter);
339 void * (*get)(const struct net *net,
340 const struct nft_set *set,
341 const struct nft_set_elem *elem,
344 u64 (*privsize)(const struct nlattr * const nla[],
345 const struct nft_set_desc *desc);
346 bool (*estimate)(const struct nft_set_desc *desc,
348 struct nft_set_estimate *est);
349 int (*init)(const struct nft_set *set,
350 const struct nft_set_desc *desc,
351 const struct nlattr * const nla[]);
352 void (*destroy)(const struct nft_set *set);
353 void (*gc_init)(const struct nft_set *set);
355 unsigned int elemsize;
359 * struct nft_set_type - nf_tables set type
361 * @ops: set ops for this type
362 * @list: used internally
363 * @owner: module reference
364 * @features: features supported by the implementation
366 struct nft_set_type {
367 const struct nft_set_ops ops;
368 struct list_head list;
369 struct module *owner;
372 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
374 int nft_register_set(struct nft_set_type *type);
375 void nft_unregister_set(struct nft_set_type *type);
378 * struct nft_set - nf_tables set instance
380 * @list: table set list node
381 * @bindings: list of set bindings
382 * @table: table this set belongs to
383 * @net: netnamespace this set belongs to
384 * @name: name of the set
385 * @handle: unique handle of the set
386 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
387 * @dtype: data type (verdict or numeric type defined by userspace)
388 * @objtype: object type (see NFT_OBJECT_* definitions)
389 * @size: maximum set size
390 * @use: number of rules references to this set
391 * @nelems: number of elements
392 * @ndeact: number of deactivated elements queued for removal
393 * @timeout: default timeout value in jiffies
394 * @gc_int: garbage collection interval in msecs
395 * @policy: set parameterization (see enum nft_set_policies)
396 * @udlen: user data length
400 * @genmask: generation mask
403 * @data: private set data
406 struct list_head list;
407 struct list_head bindings;
408 struct nft_table *table;
423 unsigned char *udata;
424 /* runtime data below here */
425 const struct nft_set_ops *ops ____cacheline_aligned;
431 __attribute__((aligned(__alignof__(u64))));
434 static inline bool nft_set_is_anonymous(const struct nft_set *set)
436 return set->flags & NFT_SET_ANONYMOUS;
439 static inline void *nft_set_priv(const struct nft_set *set)
441 return (void *)set->data;
444 static inline struct nft_set *nft_set_container_of(const void *priv)
446 return (void *)priv - offsetof(struct nft_set, data);
449 struct nft_set *nft_set_lookup_global(const struct net *net,
450 const struct nft_table *table,
451 const struct nlattr *nla_set_name,
452 const struct nlattr *nla_set_id,
455 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
457 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
461 * struct nft_set_binding - nf_tables set binding
463 * @list: set bindings list node
464 * @chain: chain containing the rule bound to the set
465 * @flags: set action flags
467 * A set binding contains all information necessary for validation
468 * of new elements added to a bound set.
470 struct nft_set_binding {
471 struct list_head list;
472 const struct nft_chain *chain;
476 enum nft_trans_phase;
477 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
478 struct nft_set_binding *binding,
479 enum nft_trans_phase phase);
480 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
481 struct nft_set_binding *binding);
482 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
485 * enum nft_set_extensions - set extension type IDs
487 * @NFT_SET_EXT_KEY: element key
488 * @NFT_SET_EXT_DATA: mapping data
489 * @NFT_SET_EXT_FLAGS: element flags
490 * @NFT_SET_EXT_TIMEOUT: element timeout
491 * @NFT_SET_EXT_EXPIRATION: element expiration time
492 * @NFT_SET_EXT_USERDATA: user data associated with the element
493 * @NFT_SET_EXT_EXPR: expression assiociated with the element
494 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
495 * @NFT_SET_EXT_NUM: number of extension types
497 enum nft_set_extensions {
502 NFT_SET_EXT_EXPIRATION,
503 NFT_SET_EXT_USERDATA,
510 * struct nft_set_ext_type - set extension type
512 * @len: fixed part length of the extension
513 * @align: alignment requirements of the extension
515 struct nft_set_ext_type {
520 extern const struct nft_set_ext_type nft_set_ext_types[];
523 * struct nft_set_ext_tmpl - set extension template
525 * @len: length of extension area
526 * @offset: offsets of individual extension types
528 struct nft_set_ext_tmpl {
530 u8 offset[NFT_SET_EXT_NUM];
534 * struct nft_set_ext - set extensions
536 * @genmask: generation mask
537 * @offset: offsets of individual extension types
538 * @data: beginning of extension data
542 u8 offset[NFT_SET_EXT_NUM];
546 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
548 memset(tmpl, 0, sizeof(*tmpl));
549 tmpl->len = sizeof(struct nft_set_ext);
552 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
555 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
556 BUG_ON(tmpl->len > U8_MAX);
557 tmpl->offset[id] = tmpl->len;
558 tmpl->len += nft_set_ext_types[id].len + len;
561 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
563 nft_set_ext_add_length(tmpl, id, 0);
566 static inline void nft_set_ext_init(struct nft_set_ext *ext,
567 const struct nft_set_ext_tmpl *tmpl)
569 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
572 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
574 return !!ext->offset[id];
577 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
579 return ext && __nft_set_ext_exists(ext, id);
582 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
584 return (void *)ext + ext->offset[id];
587 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
589 return nft_set_ext(ext, NFT_SET_EXT_KEY);
592 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
594 return nft_set_ext(ext, NFT_SET_EXT_DATA);
597 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
599 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
602 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
604 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
607 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
609 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
612 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
614 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
617 static inline struct nft_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
619 return nft_set_ext(ext, NFT_SET_EXT_EXPR);
622 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
624 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
625 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
628 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
631 return elem + set->ops->elemsize;
634 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
636 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
639 void *nft_set_elem_init(const struct nft_set *set,
640 const struct nft_set_ext_tmpl *tmpl,
641 const u32 *key, const u32 *data,
642 u64 timeout, u64 expiration, gfp_t gfp);
643 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
647 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
650 * @set: set the elements belong to
651 * @cnt: count of elements
653 struct nft_set_gc_batch_head {
655 const struct nft_set *set;
659 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
660 sizeof(struct nft_set_gc_batch_head)) / \
664 * struct nft_set_gc_batch - nf_tables set garbage collection batch
666 * @head: GC batch head
667 * @elems: garbage collection elements
669 struct nft_set_gc_batch {
670 struct nft_set_gc_batch_head head;
671 void *elems[NFT_SET_GC_BATCH_SIZE];
674 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
676 void nft_set_gc_batch_release(struct rcu_head *rcu);
678 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
681 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
684 static inline struct nft_set_gc_batch *
685 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
689 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
691 nft_set_gc_batch_complete(gcb);
693 return nft_set_gc_batch_alloc(set, gfp);
696 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
699 gcb->elems[gcb->head.cnt++] = elem;
704 * struct nft_expr_type - nf_tables expression type
706 * @select_ops: function to select nft_expr_ops
707 * @release_ops: release nft_expr_ops
708 * @ops: default ops, used when no select_ops functions is present
709 * @list: used internally
711 * @owner: module reference
712 * @policy: netlink attribute policy
713 * @maxattr: highest netlink attribute number
714 * @family: address family for AF-specific types
715 * @flags: expression type flags
717 struct nft_expr_type {
718 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
719 const struct nlattr * const tb[]);
720 void (*release_ops)(const struct nft_expr_ops *ops);
721 const struct nft_expr_ops *ops;
722 struct list_head list;
724 struct module *owner;
725 const struct nla_policy *policy;
726 unsigned int maxattr;
731 #define NFT_EXPR_STATEFUL 0x1
732 #define NFT_EXPR_GC 0x2
734 enum nft_trans_phase {
741 struct nft_flow_rule;
742 struct nft_offload_ctx;
745 * struct nft_expr_ops - nf_tables expression operations
747 * @eval: Expression evaluation function
748 * @size: full expression size, including private data size
749 * @init: initialization function
750 * @activate: activate expression in the next generation
751 * @deactivate: deactivate expression in next generation
752 * @destroy: destruction function, called after synchronize_rcu
753 * @dump: function to dump parameters
754 * @type: expression type
755 * @validate: validate expression, called during loop detection
756 * @data: extra data to attach to this expression operation
759 struct nft_expr_ops {
760 void (*eval)(const struct nft_expr *expr,
761 struct nft_regs *regs,
762 const struct nft_pktinfo *pkt);
763 int (*clone)(struct nft_expr *dst,
764 const struct nft_expr *src);
767 int (*init)(const struct nft_ctx *ctx,
768 const struct nft_expr *expr,
769 const struct nlattr * const tb[]);
770 void (*activate)(const struct nft_ctx *ctx,
771 const struct nft_expr *expr);
772 void (*deactivate)(const struct nft_ctx *ctx,
773 const struct nft_expr *expr,
774 enum nft_trans_phase phase);
775 void (*destroy)(const struct nft_ctx *ctx,
776 const struct nft_expr *expr);
777 void (*destroy_clone)(const struct nft_ctx *ctx,
778 const struct nft_expr *expr);
779 int (*dump)(struct sk_buff *skb,
780 const struct nft_expr *expr);
781 int (*validate)(const struct nft_ctx *ctx,
782 const struct nft_expr *expr,
783 const struct nft_data **data);
784 bool (*gc)(struct net *net,
785 const struct nft_expr *expr);
786 int (*offload)(struct nft_offload_ctx *ctx,
787 struct nft_flow_rule *flow,
788 const struct nft_expr *expr);
790 const struct nft_expr_type *type;
794 #define NFT_EXPR_MAXATTR 16
795 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
796 ALIGN(size, __alignof__(struct nft_expr)))
799 * struct nft_expr - nf_tables expression
801 * @ops: expression ops
802 * @data: expression private data
805 const struct nft_expr_ops *ops;
806 unsigned char data[];
809 static inline void *nft_expr_priv(const struct nft_expr *expr)
811 return (void *)expr->data;
814 struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
815 const struct nlattr *nla);
816 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
817 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
818 const struct nft_expr *expr);
821 * struct nft_rule - nf_tables rule
823 * @list: used internally
824 * @handle: rule handle
825 * @genmask: generation mask
826 * @dlen: length of expression data
827 * @udata: user data is appended to the rule
828 * @data: expression data
831 struct list_head list;
837 __attribute__((aligned(__alignof__(struct nft_expr))));
840 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
842 return (struct nft_expr *)&rule->data[0];
845 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
847 return ((void *)expr) + expr->ops->size;
850 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
852 return (struct nft_expr *)&rule->data[rule->dlen];
855 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
857 return (void *)&rule->data[rule->dlen];
861 * The last pointer isn't really necessary, but the compiler isn't able to
862 * determine that the result of nft_expr_last() is always the same since it
863 * can't assume that the dlen value wasn't changed within calls in the loop.
865 #define nft_rule_for_each_expr(expr, last, rule) \
866 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
868 (expr) = nft_expr_next(expr))
870 enum nft_chain_flags {
871 NFT_BASE_CHAIN = 0x1,
872 NFT_CHAIN_HW_OFFLOAD = 0x2,
876 * struct nft_chain - nf_tables chain
878 * @rules: list of rules in the chain
879 * @list: used internally
880 * @rhlhead: used internally
881 * @table: table that this chain belongs to
882 * @handle: chain handle
883 * @use: number of jump references to this chain
884 * @flags: bitmask of enum nft_chain_flags
885 * @name: name of the chain
888 struct nft_rule *__rcu *rules_gen_0;
889 struct nft_rule *__rcu *rules_gen_1;
890 struct list_head rules;
891 struct list_head list;
892 struct rhlist_head rhlhead;
893 struct nft_table *table;
900 /* Only used during control plane commit phase: */
901 struct nft_rule **rules_next;
904 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
906 enum nft_chain_types {
907 NFT_CHAIN_T_DEFAULT = 0,
914 * struct nft_chain_type - nf_tables chain type info
916 * @name: name of the type
917 * @type: numeric identifier
918 * @family: address family
919 * @owner: module owner
920 * @hook_mask: mask of valid hooks
921 * @hooks: array of hook functions
922 * @ops_register: base chain register function
923 * @ops_unregister: base chain unregister function
925 struct nft_chain_type {
927 enum nft_chain_types type;
929 struct module *owner;
930 unsigned int hook_mask;
931 #if IS_ENABLED(CONFIG_NETFILTER)
932 nf_hookfn *hooks[NF_MAX_HOOKS];
933 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
934 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
938 int nft_chain_validate_dependency(const struct nft_chain *chain,
939 enum nft_chain_types type);
940 int nft_chain_validate_hooks(const struct nft_chain *chain,
941 unsigned int hook_flags);
946 struct u64_stats_sync syncp;
950 * struct nft_base_chain - nf_tables base chain
952 * @ops: netfilter hook ops
954 * @policy: default policy
955 * @stats: per-cpu chain stats
957 * @dev_name: device name that this base chain is attached to (if any)
958 * @flow_block: flow block (for hardware offload)
960 struct nft_base_chain {
961 #if IS_ENABLED(CONFIG_NETFILTER)
962 struct nf_hook_ops ops;
964 const struct nft_chain_type *type;
967 struct nft_stats __percpu *stats;
968 struct nft_chain chain;
969 char dev_name[IFNAMSIZ];
970 struct flow_block flow_block;
973 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
975 return container_of(chain, struct nft_base_chain, chain);
978 static inline bool nft_is_base_chain(const struct nft_chain *chain)
980 return chain->flags & NFT_BASE_CHAIN;
983 int __nft_release_basechain(struct nft_ctx *ctx);
985 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
988 * struct nft_table - nf_tables table
990 * @list: used internally
991 * @chains_ht: chains in the table
992 * @chains: same, for stable walks
993 * @sets: sets in the table
994 * @objects: stateful objects in the table
995 * @flowtables: flow tables in the table
996 * @hgenerator: handle generator state
997 * @handle: table handle
998 * @use: number of chain references to this table
999 * @flags: table flag (see enum nft_table_flags)
1000 * @genmask: generation mask
1001 * @afinfo: address family info
1002 * @name: name of the table
1005 struct list_head list;
1006 struct rhltable chains_ht;
1007 struct list_head chains;
1008 struct list_head sets;
1009 struct list_head objects;
1010 struct list_head flowtables;
1020 void nft_register_chain_type(const struct nft_chain_type *);
1021 void nft_unregister_chain_type(const struct nft_chain_type *);
1023 int nft_register_expr(struct nft_expr_type *);
1024 void nft_unregister_expr(struct nft_expr_type *);
1026 int nft_verdict_dump(struct sk_buff *skb, int type,
1027 const struct nft_verdict *v);
1030 * struct nft_object_hash_key - key to lookup nft_object
1032 * @name: name of the stateful object to look up
1033 * @table: table the object belongs to
1035 struct nft_object_hash_key {
1037 const struct nft_table *table;
1041 * struct nft_object - nf_tables stateful object
1043 * @list: table stateful object list node
1044 * @key: keys that identify this object
1045 * @rhlhead: nft_objname_ht node
1046 * @genmask: generation mask
1047 * @use: number of references to this stateful object
1048 * @handle: unique object handle
1049 * @ops: object operations
1050 * @data: object data, layout depends on type
1053 struct list_head list;
1054 struct rhlist_head rhlhead;
1055 struct nft_object_hash_key key;
1059 /* runtime data below here */
1060 const struct nft_object_ops *ops ____cacheline_aligned;
1061 unsigned char data[]
1062 __attribute__((aligned(__alignof__(u64))));
1065 static inline void *nft_obj_data(const struct nft_object *obj)
1067 return (void *)obj->data;
1070 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1072 struct nft_object *nft_obj_lookup(const struct net *net,
1073 const struct nft_table *table,
1074 const struct nlattr *nla, u32 objtype,
1077 void nft_obj_notify(struct net *net, const struct nft_table *table,
1078 struct nft_object *obj, u32 portid, u32 seq,
1079 int event, int family, int report, gfp_t gfp);
1082 * struct nft_object_type - stateful object type
1084 * @select_ops: function to select nft_object_ops
1085 * @ops: default ops, used when no select_ops functions is present
1086 * @list: list node in list of object types
1087 * @type: stateful object numeric type
1088 * @owner: module owner
1089 * @maxattr: maximum netlink attribute
1090 * @policy: netlink attribute policy
1092 struct nft_object_type {
1093 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1094 const struct nlattr * const tb[]);
1095 const struct nft_object_ops *ops;
1096 struct list_head list;
1098 unsigned int maxattr;
1099 struct module *owner;
1100 const struct nla_policy *policy;
1104 * struct nft_object_ops - stateful object operations
1106 * @eval: stateful object evaluation function
1107 * @size: stateful object size
1108 * @init: initialize object from netlink attributes
1109 * @destroy: release existing stateful object
1110 * @dump: netlink dump stateful object
1112 struct nft_object_ops {
1113 void (*eval)(struct nft_object *obj,
1114 struct nft_regs *regs,
1115 const struct nft_pktinfo *pkt);
1117 int (*init)(const struct nft_ctx *ctx,
1118 const struct nlattr *const tb[],
1119 struct nft_object *obj);
1120 void (*destroy)(const struct nft_ctx *ctx,
1121 struct nft_object *obj);
1122 int (*dump)(struct sk_buff *skb,
1123 struct nft_object *obj,
1125 const struct nft_object_type *type;
1128 int nft_register_obj(struct nft_object_type *obj_type);
1129 void nft_unregister_obj(struct nft_object_type *obj_type);
1131 #define NFT_FLOWTABLE_DEVICE_MAX 8
1134 * struct nft_flowtable - nf_tables flow table
1136 * @list: flow table list node in table list
1137 * @table: the table the flow table is contained in
1138 * @name: name of this flow table
1139 * @hooknum: hook number
1140 * @priority: hook priority
1141 * @ops_len: number of hooks in array
1142 * @genmask: generation mask
1143 * @use: number of references to this flow table
1144 * @handle: unique object handle
1145 * @dev_name: array of device names
1146 * @data: rhashtable and garbage collector
1147 * @ops: array of hooks
1149 struct nft_flowtable {
1150 struct list_head list;
1151 struct nft_table *table;
1159 /* runtime data below here */
1160 #if IS_ENABLED(CONFIG_NETFILTER)
1161 struct nf_hook_ops *ops ____cacheline_aligned;
1163 struct nf_flowtable data;
1166 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1167 const struct nlattr *nla,
1170 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1171 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1174 * struct nft_traceinfo - nft tracing information and state
1176 * @pkt: pktinfo currently processed
1177 * @basechain: base chain currently processed
1178 * @chain: chain currently processed
1179 * @rule: rule that was evaluated
1180 * @verdict: verdict given by rule
1181 * @type: event type (enum nft_trace_types)
1182 * @packet_dumped: packet headers sent in a previous traceinfo message
1183 * @trace: other struct members are initialised
1185 struct nft_traceinfo {
1186 const struct nft_pktinfo *pkt;
1187 const struct nft_base_chain *basechain;
1188 const struct nft_chain *chain;
1189 const struct nft_rule *rule;
1190 const struct nft_verdict *verdict;
1191 enum nft_trace_types type;
1196 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1197 const struct nft_verdict *verdict,
1198 const struct nft_chain *basechain);
1200 void nft_trace_notify(struct nft_traceinfo *info);
1202 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1203 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1205 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1206 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1208 #define MODULE_ALIAS_NFT_EXPR(name) \
1209 MODULE_ALIAS("nft-expr-" name)
1211 #define MODULE_ALIAS_NFT_SET() \
1212 MODULE_ALIAS("nft-set")
1214 #define MODULE_ALIAS_NFT_OBJ(type) \
1215 MODULE_ALIAS("nft-obj-" __stringify(type))
1217 #if IS_ENABLED(CONFIG_NF_TABLES)
1220 * The gencursor defines two generations, the currently active and the
1221 * next one. Objects contain a bitmask of 2 bits specifying the generations
1222 * they're active in. A set bit means they're inactive in the generation
1223 * represented by that bit.
1225 * New objects start out as inactive in the current and active in the
1226 * next generation. When committing the ruleset the bitmask is cleared,
1227 * meaning they're active in all generations. When removing an object,
1228 * it is set inactive in the next generation. After committing the ruleset,
1229 * the objects are removed.
1231 static inline unsigned int nft_gencursor_next(const struct net *net)
1233 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1236 static inline u8 nft_genmask_next(const struct net *net)
1238 return 1 << nft_gencursor_next(net);
1241 static inline u8 nft_genmask_cur(const struct net *net)
1243 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1244 return 1 << READ_ONCE(net->nft.gencursor);
1247 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1250 * Generic transaction helpers
1253 /* Check if this object is currently active. */
1254 #define nft_is_active(__net, __obj) \
1255 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1257 /* Check if this object is active in the next generation. */
1258 #define nft_is_active_next(__net, __obj) \
1259 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1261 /* This object becomes active in the next generation. */
1262 #define nft_activate_next(__net, __obj) \
1263 (__obj)->genmask = nft_genmask_cur(__net)
1265 /* This object becomes inactive in the next generation. */
1266 #define nft_deactivate_next(__net, __obj) \
1267 (__obj)->genmask = nft_genmask_next(__net)
1269 /* After committing the ruleset, clear the stale generation bit. */
1270 #define nft_clear(__net, __obj) \
1271 (__obj)->genmask &= ~nft_genmask_next(__net)
1272 #define nft_active_genmask(__obj, __genmask) \
1273 !((__obj)->genmask & __genmask)
1276 * Set element transaction helpers
1279 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1282 return !(ext->genmask & genmask);
1285 static inline void nft_set_elem_change_active(const struct net *net,
1286 const struct nft_set *set,
1287 struct nft_set_ext *ext)
1289 ext->genmask ^= nft_genmask_next(net);
1292 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1295 * We use a free bit in the genmask field to indicate the element
1296 * is busy, meaning it is currently being processed either by
1297 * the netlink API or GC.
1299 * Even though the genmask is only a single byte wide, this works
1300 * because the extension structure if fully constant once initialized,
1301 * so there are no non-atomic write accesses unless it is already
1304 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1306 #if defined(__LITTLE_ENDIAN_BITFIELD)
1307 #define NFT_SET_ELEM_BUSY_BIT 2
1308 #elif defined(__BIG_ENDIAN_BITFIELD)
1309 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1314 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1316 unsigned long *word = (unsigned long *)ext;
1318 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1319 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1322 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1324 unsigned long *word = (unsigned long *)ext;
1326 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1330 * struct nft_trans - nf_tables object update in transaction
1332 * @list: used internally
1333 * @msg_type: message type
1334 * @put_net: ctx->net needs to be put
1335 * @ctx: transaction context
1336 * @data: internal information related to the transaction
1339 struct list_head list;
1346 struct nft_trans_rule {
1347 struct nft_rule *rule;
1348 struct nft_flow_rule *flow;
1352 #define nft_trans_rule(trans) \
1353 (((struct nft_trans_rule *)trans->data)->rule)
1354 #define nft_trans_flow_rule(trans) \
1355 (((struct nft_trans_rule *)trans->data)->flow)
1356 #define nft_trans_rule_id(trans) \
1357 (((struct nft_trans_rule *)trans->data)->rule_id)
1359 struct nft_trans_set {
1360 struct nft_set *set;
1365 #define nft_trans_set(trans) \
1366 (((struct nft_trans_set *)trans->data)->set)
1367 #define nft_trans_set_id(trans) \
1368 (((struct nft_trans_set *)trans->data)->set_id)
1369 #define nft_trans_set_bound(trans) \
1370 (((struct nft_trans_set *)trans->data)->bound)
1372 struct nft_trans_chain {
1375 struct nft_stats __percpu *stats;
1379 #define nft_trans_chain_update(trans) \
1380 (((struct nft_trans_chain *)trans->data)->update)
1381 #define nft_trans_chain_name(trans) \
1382 (((struct nft_trans_chain *)trans->data)->name)
1383 #define nft_trans_chain_stats(trans) \
1384 (((struct nft_trans_chain *)trans->data)->stats)
1385 #define nft_trans_chain_policy(trans) \
1386 (((struct nft_trans_chain *)trans->data)->policy)
1388 struct nft_trans_table {
1393 #define nft_trans_table_update(trans) \
1394 (((struct nft_trans_table *)trans->data)->update)
1395 #define nft_trans_table_enable(trans) \
1396 (((struct nft_trans_table *)trans->data)->enable)
1398 struct nft_trans_elem {
1399 struct nft_set *set;
1400 struct nft_set_elem elem;
1404 #define nft_trans_elem_set(trans) \
1405 (((struct nft_trans_elem *)trans->data)->set)
1406 #define nft_trans_elem(trans) \
1407 (((struct nft_trans_elem *)trans->data)->elem)
1408 #define nft_trans_elem_set_bound(trans) \
1409 (((struct nft_trans_elem *)trans->data)->bound)
1411 struct nft_trans_obj {
1412 struct nft_object *obj;
1415 #define nft_trans_obj(trans) \
1416 (((struct nft_trans_obj *)trans->data)->obj)
1418 struct nft_trans_flowtable {
1419 struct nft_flowtable *flowtable;
1422 #define nft_trans_flowtable(trans) \
1423 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1425 int __init nft_chain_filter_init(void);
1426 void nft_chain_filter_fini(void);
1428 void __init nft_chain_route_init(void);
1429 void nft_chain_route_fini(void);
1430 #endif /* _NET_NF_TABLES_H */