1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_H
3 #define _NET_NF_TABLES_H
5 #include <asm/unaligned.h>
6 #include <linux/list.h>
7 #include <linux/netfilter.h>
8 #include <linux/netfilter/nfnetlink.h>
9 #include <linux/netfilter/x_tables.h>
10 #include <linux/netfilter/nf_tables.h>
11 #include <linux/u64_stats_sync.h>
12 #include <linux/rhashtable.h>
13 #include <net/netfilter/nf_flow_table.h>
14 #include <net/netlink.h>
15 #include <net/flow_offload.h>
16 #include <net/netns/generic.h>
18 #define NFT_MAX_HOOKS (NF_INET_INGRESS + 1)
22 #define NFT_JUMP_STACK_SIZE 16
26 const struct nf_hook_state *state;
33 static inline struct sock *nft_sk(const struct nft_pktinfo *pkt)
35 return pkt->state->sk;
38 static inline unsigned int nft_thoff(const struct nft_pktinfo *pkt)
43 static inline struct net *nft_net(const struct nft_pktinfo *pkt)
45 return pkt->state->net;
48 static inline unsigned int nft_hook(const struct nft_pktinfo *pkt)
50 return pkt->state->hook;
53 static inline u8 nft_pf(const struct nft_pktinfo *pkt)
55 return pkt->state->pf;
58 static inline const struct net_device *nft_in(const struct nft_pktinfo *pkt)
60 return pkt->state->in;
63 static inline const struct net_device *nft_out(const struct nft_pktinfo *pkt)
65 return pkt->state->out;
68 static inline void nft_set_pktinfo(struct nft_pktinfo *pkt,
70 const struct nf_hook_state *state)
76 static inline void nft_set_pktinfo_unspec(struct nft_pktinfo *pkt)
78 pkt->tprot_set = false;
85 * struct nft_verdict - nf_tables verdict
87 * @code: nf_tables/netfilter verdict code
88 * @chain: destination chain for NFT_JUMP/NFT_GOTO
92 struct nft_chain *chain;
98 struct nft_verdict verdict;
100 } __attribute__((aligned(__alignof__(u64))));
103 * struct nft_regs - nf_tables register set
105 * @data: data registers
106 * @verdict: verdict register
108 * The first four data registers alias to the verdict register.
113 struct nft_verdict verdict;
117 /* Store/load an u8, u16 or u64 integer to/from the u32 data register.
119 * Note, when using concatenations, register allocation happens at 32-bit
120 * level. So for store instruction, pad the rest part with zero to avoid
124 static inline void nft_reg_store8(u32 *dreg, u8 val)
130 static inline u8 nft_reg_load8(const u32 *sreg)
135 static inline void nft_reg_store16(u32 *dreg, u16 val)
141 static inline u16 nft_reg_load16(const u32 *sreg)
146 static inline void nft_reg_store64(u32 *dreg, u64 val)
148 put_unaligned(val, (u64 *)dreg);
151 static inline u64 nft_reg_load64(const u32 *sreg)
153 return get_unaligned((u64 *)sreg);
156 static inline void nft_data_copy(u32 *dst, const struct nft_data *src,
159 if (len % NFT_REG32_SIZE)
160 dst[len / NFT_REG32_SIZE] = 0;
161 memcpy(dst, src, len);
165 * struct nft_ctx - nf_tables rule/set context
167 * @net: net namespace
168 * @table: the table the chain is contained in
169 * @chain: the chain the rule is contained in
170 * @nla: netlink attributes
171 * @portid: netlink portID of the original message
172 * @seq: netlink sequence number
173 * @family: protocol family
174 * @level: depth of the chains
175 * @report: notify via unicast netlink message
179 struct nft_table *table;
180 struct nft_chain *chain;
181 const struct nlattr * const *nla;
190 struct nft_data_desc {
191 enum nft_data_types type;
195 int nft_data_init(const struct nft_ctx *ctx,
196 struct nft_data *data, unsigned int size,
197 struct nft_data_desc *desc, const struct nlattr *nla);
198 void nft_data_hold(const struct nft_data *data, enum nft_data_types type);
199 void nft_data_release(const struct nft_data *data, enum nft_data_types type);
200 int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
201 enum nft_data_types type, unsigned int len);
203 static inline enum nft_data_types nft_dreg_to_type(enum nft_registers reg)
205 return reg == NFT_REG_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE;
208 static inline enum nft_registers nft_type_to_reg(enum nft_data_types type)
210 return type == NFT_DATA_VERDICT ? NFT_REG_VERDICT : NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE;
213 int nft_parse_u32_check(const struct nlattr *attr, int max, u32 *dest);
214 int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg);
216 int nft_parse_register_load(const struct nlattr *attr, u8 *sreg, u32 len);
217 int nft_parse_register_store(const struct nft_ctx *ctx,
218 const struct nlattr *attr, u8 *dreg,
219 const struct nft_data *data,
220 enum nft_data_types type, unsigned int len);
223 * struct nft_userdata - user defined data associated with an object
225 * @len: length of the data
228 * The presence of user data is indicated in an object specific fashion,
229 * so a length of zero can't occur and the value "len" indicates data
232 struct nft_userdata {
234 unsigned char data[];
238 * struct nft_set_elem - generic representation of set elements
241 * @key_end: closing element key
242 * @priv: element private data and extensions
244 struct nft_set_elem {
246 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
250 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
254 u32 buf[NFT_DATA_VALUE_MAXLEN / sizeof(u32)];
261 struct nft_set_iter {
266 int (*fn)(const struct nft_ctx *ctx,
268 const struct nft_set_iter *iter,
269 struct nft_set_elem *elem);
273 * struct nft_set_desc - description of set elements
277 * @size: number of set elements
278 * @field_len: length of each field in concatenation, bytes
279 * @field_count: number of concatenated fields in element
280 * @expr: set must support for expressions
282 struct nft_set_desc {
286 u8 field_len[NFT_REG32_COUNT];
292 * enum nft_set_class - performance class
294 * @NFT_LOOKUP_O_1: constant, O(1)
295 * @NFT_LOOKUP_O_LOG_N: logarithmic, O(log N)
296 * @NFT_LOOKUP_O_N: linear, O(N)
300 NFT_SET_CLASS_O_LOG_N,
305 * struct nft_set_estimate - estimation of memory and performance
308 * @size: required memory
309 * @lookup: lookup performance class
310 * @space: memory class
312 struct nft_set_estimate {
314 enum nft_set_class lookup;
315 enum nft_set_class space;
318 #define NFT_EXPR_MAXATTR 16
319 #define NFT_EXPR_SIZE(size) (sizeof(struct nft_expr) + \
320 ALIGN(size, __alignof__(struct nft_expr)))
323 * struct nft_expr - nf_tables expression
325 * @ops: expression ops
326 * @data: expression private data
329 const struct nft_expr_ops *ops;
331 __attribute__((aligned(__alignof__(u64))));
334 static inline void *nft_expr_priv(const struct nft_expr *expr)
336 return (void *)expr->data;
339 int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src);
340 void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr);
341 int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
342 const struct nft_expr *expr);
347 * struct nft_set_ops - nf_tables set operations
349 * @lookup: look up an element within the set
350 * @update: update an element if exists, add it if doesn't exist
351 * @delete: delete an element
352 * @insert: insert new element into set
353 * @activate: activate new element in the next generation
354 * @deactivate: lookup for element and deactivate it in the next generation
355 * @flush: deactivate element in the next generation
356 * @remove: remove element from set
357 * @walk: iterate over all set elements
358 * @get: get set elements
359 * @privsize: function to return size of set private data
360 * @init: initialize private data of new set instance
361 * @destroy: destroy private data of set instance
362 * @elemsize: element private size
364 * Operations lookup, update and delete have simpler interfaces, are faster
365 * and currently only used in the packet path. All the rest are slower,
366 * control plane functions.
369 bool (*lookup)(const struct net *net,
370 const struct nft_set *set,
372 const struct nft_set_ext **ext);
373 bool (*update)(struct nft_set *set,
375 void *(*new)(struct nft_set *,
376 const struct nft_expr *,
378 const struct nft_expr *expr,
379 struct nft_regs *regs,
380 const struct nft_set_ext **ext);
381 bool (*delete)(const struct nft_set *set,
384 int (*insert)(const struct net *net,
385 const struct nft_set *set,
386 const struct nft_set_elem *elem,
387 struct nft_set_ext **ext);
388 void (*activate)(const struct net *net,
389 const struct nft_set *set,
390 const struct nft_set_elem *elem);
391 void * (*deactivate)(const struct net *net,
392 const struct nft_set *set,
393 const struct nft_set_elem *elem);
394 bool (*flush)(const struct net *net,
395 const struct nft_set *set,
397 void (*remove)(const struct net *net,
398 const struct nft_set *set,
399 const struct nft_set_elem *elem);
400 void (*walk)(const struct nft_ctx *ctx,
402 struct nft_set_iter *iter);
403 void * (*get)(const struct net *net,
404 const struct nft_set *set,
405 const struct nft_set_elem *elem,
408 u64 (*privsize)(const struct nlattr * const nla[],
409 const struct nft_set_desc *desc);
410 bool (*estimate)(const struct nft_set_desc *desc,
412 struct nft_set_estimate *est);
413 int (*init)(const struct nft_set *set,
414 const struct nft_set_desc *desc,
415 const struct nlattr * const nla[]);
416 void (*destroy)(const struct nft_set *set);
417 void (*gc_init)(const struct nft_set *set);
419 unsigned int elemsize;
423 * struct nft_set_type - nf_tables set type
425 * @ops: set ops for this type
426 * @features: features supported by the implementation
428 struct nft_set_type {
429 const struct nft_set_ops ops;
432 #define to_set_type(o) container_of(o, struct nft_set_type, ops)
434 struct nft_set_elem_expr {
437 __attribute__((aligned(__alignof__(struct nft_expr))));
440 #define nft_setelem_expr_at(__elem_expr, __offset) \
441 ((struct nft_expr *)&__elem_expr->data[__offset])
443 #define nft_setelem_expr_foreach(__expr, __elem_expr, __size) \
444 for (__expr = nft_setelem_expr_at(__elem_expr, 0), __size = 0; \
445 __size < (__elem_expr)->size; \
446 __size += (__expr)->ops->size, __expr = ((void *)(__expr)) + (__expr)->ops->size)
448 #define NFT_SET_EXPR_MAX 2
451 * struct nft_set - nf_tables set instance
453 * @list: table set list node
454 * @bindings: list of set bindings
455 * @table: table this set belongs to
456 * @net: netnamespace this set belongs to
457 * @name: name of the set
458 * @handle: unique handle of the set
459 * @ktype: key type (numeric type defined by userspace, not used in the kernel)
460 * @dtype: data type (verdict or numeric type defined by userspace)
461 * @objtype: object type (see NFT_OBJECT_* definitions)
462 * @size: maximum set size
463 * @field_len: length of each field in concatenation, bytes
464 * @field_count: number of concatenated fields in element
465 * @use: number of rules references to this set
466 * @nelems: number of elements
467 * @ndeact: number of deactivated elements queued for removal
468 * @timeout: default timeout value in jiffies
469 * @gc_int: garbage collection interval in msecs
470 * @policy: set parameterization (see enum nft_set_policies)
471 * @udlen: user data length
473 * @expr: stateful expression
476 * @genmask: generation mask
479 * @data: private set data
482 struct list_head list;
483 struct list_head bindings;
484 struct nft_table *table;
492 u8 field_len[NFT_REG32_COUNT];
501 unsigned char *udata;
502 /* runtime data below here */
503 const struct nft_set_ops *ops ____cacheline_aligned;
509 struct nft_expr *exprs[NFT_SET_EXPR_MAX];
510 struct list_head catchall_list;
512 __attribute__((aligned(__alignof__(u64))));
515 static inline bool nft_set_is_anonymous(const struct nft_set *set)
517 return set->flags & NFT_SET_ANONYMOUS;
520 static inline void *nft_set_priv(const struct nft_set *set)
522 return (void *)set->data;
525 static inline struct nft_set *nft_set_container_of(const void *priv)
527 return (void *)priv - offsetof(struct nft_set, data);
530 struct nft_set *nft_set_lookup_global(const struct net *net,
531 const struct nft_table *table,
532 const struct nlattr *nla_set_name,
533 const struct nlattr *nla_set_id,
536 struct nft_set_ext *nft_set_catchall_lookup(const struct net *net,
537 const struct nft_set *set);
538 void *nft_set_catchall_gc(const struct nft_set *set);
540 static inline unsigned long nft_set_gc_interval(const struct nft_set *set)
542 return set->gc_int ? msecs_to_jiffies(set->gc_int) : HZ;
546 * struct nft_set_binding - nf_tables set binding
548 * @list: set bindings list node
549 * @chain: chain containing the rule bound to the set
550 * @flags: set action flags
552 * A set binding contains all information necessary for validation
553 * of new elements added to a bound set.
555 struct nft_set_binding {
556 struct list_head list;
557 const struct nft_chain *chain;
561 enum nft_trans_phase;
562 void nf_tables_deactivate_set(const struct nft_ctx *ctx, struct nft_set *set,
563 struct nft_set_binding *binding,
564 enum nft_trans_phase phase);
565 int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
566 struct nft_set_binding *binding);
567 void nf_tables_destroy_set(const struct nft_ctx *ctx, struct nft_set *set);
570 * enum nft_set_extensions - set extension type IDs
572 * @NFT_SET_EXT_KEY: element key
573 * @NFT_SET_EXT_KEY_END: upper bound element key, for ranges
574 * @NFT_SET_EXT_DATA: mapping data
575 * @NFT_SET_EXT_FLAGS: element flags
576 * @NFT_SET_EXT_TIMEOUT: element timeout
577 * @NFT_SET_EXT_EXPIRATION: element expiration time
578 * @NFT_SET_EXT_USERDATA: user data associated with the element
579 * @NFT_SET_EXT_EXPRESSIONS: expressions assiciated with the element
580 * @NFT_SET_EXT_OBJREF: stateful object reference associated with element
581 * @NFT_SET_EXT_NUM: number of extension types
583 enum nft_set_extensions {
589 NFT_SET_EXT_EXPIRATION,
590 NFT_SET_EXT_USERDATA,
591 NFT_SET_EXT_EXPRESSIONS,
597 * struct nft_set_ext_type - set extension type
599 * @len: fixed part length of the extension
600 * @align: alignment requirements of the extension
602 struct nft_set_ext_type {
607 extern const struct nft_set_ext_type nft_set_ext_types[];
610 * struct nft_set_ext_tmpl - set extension template
612 * @len: length of extension area
613 * @offset: offsets of individual extension types
615 struct nft_set_ext_tmpl {
617 u8 offset[NFT_SET_EXT_NUM];
621 * struct nft_set_ext - set extensions
623 * @genmask: generation mask
624 * @offset: offsets of individual extension types
625 * @data: beginning of extension data
629 u8 offset[NFT_SET_EXT_NUM];
633 static inline void nft_set_ext_prepare(struct nft_set_ext_tmpl *tmpl)
635 memset(tmpl, 0, sizeof(*tmpl));
636 tmpl->len = sizeof(struct nft_set_ext);
639 static inline void nft_set_ext_add_length(struct nft_set_ext_tmpl *tmpl, u8 id,
642 tmpl->len = ALIGN(tmpl->len, nft_set_ext_types[id].align);
643 BUG_ON(tmpl->len > U8_MAX);
644 tmpl->offset[id] = tmpl->len;
645 tmpl->len += nft_set_ext_types[id].len + len;
648 static inline void nft_set_ext_add(struct nft_set_ext_tmpl *tmpl, u8 id)
650 nft_set_ext_add_length(tmpl, id, 0);
653 static inline void nft_set_ext_init(struct nft_set_ext *ext,
654 const struct nft_set_ext_tmpl *tmpl)
656 memcpy(ext->offset, tmpl->offset, sizeof(ext->offset));
659 static inline bool __nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
661 return !!ext->offset[id];
664 static inline bool nft_set_ext_exists(const struct nft_set_ext *ext, u8 id)
666 return ext && __nft_set_ext_exists(ext, id);
669 static inline void *nft_set_ext(const struct nft_set_ext *ext, u8 id)
671 return (void *)ext + ext->offset[id];
674 static inline struct nft_data *nft_set_ext_key(const struct nft_set_ext *ext)
676 return nft_set_ext(ext, NFT_SET_EXT_KEY);
679 static inline struct nft_data *nft_set_ext_key_end(const struct nft_set_ext *ext)
681 return nft_set_ext(ext, NFT_SET_EXT_KEY_END);
684 static inline struct nft_data *nft_set_ext_data(const struct nft_set_ext *ext)
686 return nft_set_ext(ext, NFT_SET_EXT_DATA);
689 static inline u8 *nft_set_ext_flags(const struct nft_set_ext *ext)
691 return nft_set_ext(ext, NFT_SET_EXT_FLAGS);
694 static inline u64 *nft_set_ext_timeout(const struct nft_set_ext *ext)
696 return nft_set_ext(ext, NFT_SET_EXT_TIMEOUT);
699 static inline u64 *nft_set_ext_expiration(const struct nft_set_ext *ext)
701 return nft_set_ext(ext, NFT_SET_EXT_EXPIRATION);
704 static inline struct nft_userdata *nft_set_ext_userdata(const struct nft_set_ext *ext)
706 return nft_set_ext(ext, NFT_SET_EXT_USERDATA);
709 static inline struct nft_set_elem_expr *nft_set_ext_expr(const struct nft_set_ext *ext)
711 return nft_set_ext(ext, NFT_SET_EXT_EXPRESSIONS);
714 static inline bool nft_set_elem_expired(const struct nft_set_ext *ext)
716 return nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION) &&
717 time_is_before_eq_jiffies64(*nft_set_ext_expiration(ext));
720 static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,
723 return elem + set->ops->elemsize;
726 static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
728 return nft_set_ext(ext, NFT_SET_EXT_OBJREF);
731 struct nft_expr *nft_set_elem_expr_alloc(const struct nft_ctx *ctx,
732 const struct nft_set *set,
733 const struct nlattr *attr);
735 void *nft_set_elem_init(const struct nft_set *set,
736 const struct nft_set_ext_tmpl *tmpl,
737 const u32 *key, const u32 *key_end, const u32 *data,
738 u64 timeout, u64 expiration, gfp_t gfp);
739 int nft_set_elem_expr_clone(const struct nft_ctx *ctx, struct nft_set *set,
740 struct nft_expr *expr_array[]);
741 void nft_set_elem_destroy(const struct nft_set *set, void *elem,
745 * struct nft_set_gc_batch_head - nf_tables set garbage collection batch
748 * @set: set the elements belong to
749 * @cnt: count of elements
751 struct nft_set_gc_batch_head {
753 const struct nft_set *set;
757 #define NFT_SET_GC_BATCH_SIZE ((PAGE_SIZE - \
758 sizeof(struct nft_set_gc_batch_head)) / \
762 * struct nft_set_gc_batch - nf_tables set garbage collection batch
764 * @head: GC batch head
765 * @elems: garbage collection elements
767 struct nft_set_gc_batch {
768 struct nft_set_gc_batch_head head;
769 void *elems[NFT_SET_GC_BATCH_SIZE];
772 struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
774 void nft_set_gc_batch_release(struct rcu_head *rcu);
776 static inline void nft_set_gc_batch_complete(struct nft_set_gc_batch *gcb)
779 call_rcu(&gcb->head.rcu, nft_set_gc_batch_release);
782 static inline struct nft_set_gc_batch *
783 nft_set_gc_batch_check(const struct nft_set *set, struct nft_set_gc_batch *gcb,
787 if (gcb->head.cnt + 1 < ARRAY_SIZE(gcb->elems))
789 nft_set_gc_batch_complete(gcb);
791 return nft_set_gc_batch_alloc(set, gfp);
794 static inline void nft_set_gc_batch_add(struct nft_set_gc_batch *gcb,
797 gcb->elems[gcb->head.cnt++] = elem;
802 * struct nft_expr_type - nf_tables expression type
804 * @select_ops: function to select nft_expr_ops
805 * @release_ops: release nft_expr_ops
806 * @ops: default ops, used when no select_ops functions is present
807 * @list: used internally
809 * @owner: module reference
810 * @policy: netlink attribute policy
811 * @maxattr: highest netlink attribute number
812 * @family: address family for AF-specific types
813 * @flags: expression type flags
815 struct nft_expr_type {
816 const struct nft_expr_ops *(*select_ops)(const struct nft_ctx *,
817 const struct nlattr * const tb[]);
818 void (*release_ops)(const struct nft_expr_ops *ops);
819 const struct nft_expr_ops *ops;
820 struct list_head list;
822 struct module *owner;
823 const struct nla_policy *policy;
824 unsigned int maxattr;
829 #define NFT_EXPR_STATEFUL 0x1
830 #define NFT_EXPR_GC 0x2
832 enum nft_trans_phase {
839 struct nft_flow_rule;
840 struct nft_offload_ctx;
843 * struct nft_expr_ops - nf_tables expression operations
845 * @eval: Expression evaluation function
846 * @size: full expression size, including private data size
847 * @init: initialization function
848 * @activate: activate expression in the next generation
849 * @deactivate: deactivate expression in next generation
850 * @destroy: destruction function, called after synchronize_rcu
851 * @dump: function to dump parameters
852 * @type: expression type
853 * @validate: validate expression, called during loop detection
854 * @data: extra data to attach to this expression operation
856 struct nft_expr_ops {
857 void (*eval)(const struct nft_expr *expr,
858 struct nft_regs *regs,
859 const struct nft_pktinfo *pkt);
860 int (*clone)(struct nft_expr *dst,
861 const struct nft_expr *src);
864 int (*init)(const struct nft_ctx *ctx,
865 const struct nft_expr *expr,
866 const struct nlattr * const tb[]);
867 void (*activate)(const struct nft_ctx *ctx,
868 const struct nft_expr *expr);
869 void (*deactivate)(const struct nft_ctx *ctx,
870 const struct nft_expr *expr,
871 enum nft_trans_phase phase);
872 void (*destroy)(const struct nft_ctx *ctx,
873 const struct nft_expr *expr);
874 void (*destroy_clone)(const struct nft_ctx *ctx,
875 const struct nft_expr *expr);
876 int (*dump)(struct sk_buff *skb,
877 const struct nft_expr *expr);
878 int (*validate)(const struct nft_ctx *ctx,
879 const struct nft_expr *expr,
880 const struct nft_data **data);
881 bool (*gc)(struct net *net,
882 const struct nft_expr *expr);
883 int (*offload)(struct nft_offload_ctx *ctx,
884 struct nft_flow_rule *flow,
885 const struct nft_expr *expr);
886 void (*offload_stats)(struct nft_expr *expr,
887 const struct flow_stats *stats);
889 const struct nft_expr_type *type;
894 * struct nft_rule - nf_tables rule
896 * @list: used internally
897 * @handle: rule handle
898 * @genmask: generation mask
899 * @dlen: length of expression data
900 * @udata: user data is appended to the rule
901 * @data: expression data
904 struct list_head list;
910 __attribute__((aligned(__alignof__(struct nft_expr))));
913 static inline struct nft_expr *nft_expr_first(const struct nft_rule *rule)
915 return (struct nft_expr *)&rule->data[0];
918 static inline struct nft_expr *nft_expr_next(const struct nft_expr *expr)
920 return ((void *)expr) + expr->ops->size;
923 static inline struct nft_expr *nft_expr_last(const struct nft_rule *rule)
925 return (struct nft_expr *)&rule->data[rule->dlen];
928 static inline bool nft_expr_more(const struct nft_rule *rule,
929 const struct nft_expr *expr)
931 return expr != nft_expr_last(rule) && expr->ops;
934 static inline struct nft_userdata *nft_userdata(const struct nft_rule *rule)
936 return (void *)&rule->data[rule->dlen];
939 void nf_tables_rule_release(const struct nft_ctx *ctx, struct nft_rule *rule);
941 static inline void nft_set_elem_update_expr(const struct nft_set_ext *ext,
942 struct nft_regs *regs,
943 const struct nft_pktinfo *pkt)
945 struct nft_set_elem_expr *elem_expr;
946 struct nft_expr *expr;
949 if (__nft_set_ext_exists(ext, NFT_SET_EXT_EXPRESSIONS)) {
950 elem_expr = nft_set_ext_expr(ext);
951 nft_setelem_expr_foreach(expr, elem_expr, size) {
952 expr->ops->eval(expr, regs, pkt);
953 if (regs->verdict.code == NFT_BREAK)
960 * The last pointer isn't really necessary, but the compiler isn't able to
961 * determine that the result of nft_expr_last() is always the same since it
962 * can't assume that the dlen value wasn't changed within calls in the loop.
964 #define nft_rule_for_each_expr(expr, last, rule) \
965 for ((expr) = nft_expr_first(rule), (last) = nft_expr_last(rule); \
967 (expr) = nft_expr_next(expr))
969 #define NFT_CHAIN_POLICY_UNSET U8_MAX
972 * struct nft_chain - nf_tables chain
974 * @rules: list of rules in the chain
975 * @list: used internally
976 * @rhlhead: used internally
977 * @table: table that this chain belongs to
978 * @handle: chain handle
979 * @use: number of jump references to this chain
980 * @flags: bitmask of enum nft_chain_flags
981 * @name: name of the chain
984 struct nft_rule *__rcu *rules_gen_0;
985 struct nft_rule *__rcu *rules_gen_1;
986 struct list_head rules;
987 struct list_head list;
988 struct rhlist_head rhlhead;
989 struct nft_table *table;
999 /* Only used during control plane commit phase: */
1000 struct nft_rule **rules_next;
1003 int nft_chain_validate(const struct nft_ctx *ctx, const struct nft_chain *chain);
1005 enum nft_chain_types {
1006 NFT_CHAIN_T_DEFAULT = 0,
1013 * struct nft_chain_type - nf_tables chain type info
1015 * @name: name of the type
1016 * @type: numeric identifier
1017 * @family: address family
1018 * @owner: module owner
1019 * @hook_mask: mask of valid hooks
1020 * @hooks: array of hook functions
1021 * @ops_register: base chain register function
1022 * @ops_unregister: base chain unregister function
1024 struct nft_chain_type {
1026 enum nft_chain_types type;
1028 struct module *owner;
1029 unsigned int hook_mask;
1030 nf_hookfn *hooks[NFT_MAX_HOOKS];
1031 int (*ops_register)(struct net *net, const struct nf_hook_ops *ops);
1032 void (*ops_unregister)(struct net *net, const struct nf_hook_ops *ops);
1035 int nft_chain_validate_dependency(const struct nft_chain *chain,
1036 enum nft_chain_types type);
1037 int nft_chain_validate_hooks(const struct nft_chain *chain,
1038 unsigned int hook_flags);
1040 static inline bool nft_chain_is_bound(struct nft_chain *chain)
1042 return (chain->flags & NFT_CHAIN_BINDING) && chain->bound;
1045 void nft_chain_del(struct nft_chain *chain);
1046 void nf_tables_chain_destroy(struct nft_ctx *ctx);
1051 struct u64_stats_sync syncp;
1055 struct list_head list;
1057 struct nf_hook_ops ops;
1058 struct rcu_head rcu;
1062 * struct nft_base_chain - nf_tables base chain
1064 * @ops: netfilter hook ops
1065 * @hook_list: list of netfilter hooks (for NFPROTO_NETDEV family)
1067 * @policy: default policy
1068 * @stats: per-cpu chain stats
1070 * @flow_block: flow block (for hardware offload)
1072 struct nft_base_chain {
1073 struct nf_hook_ops ops;
1074 struct list_head hook_list;
1075 const struct nft_chain_type *type;
1078 struct nft_stats __percpu *stats;
1079 struct nft_chain chain;
1080 struct flow_block flow_block;
1083 static inline struct nft_base_chain *nft_base_chain(const struct nft_chain *chain)
1085 return container_of(chain, struct nft_base_chain, chain);
1088 static inline bool nft_is_base_chain(const struct nft_chain *chain)
1090 return chain->flags & NFT_CHAIN_BASE;
1093 int __nft_release_basechain(struct nft_ctx *ctx);
1095 unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);
1098 * struct nft_table - nf_tables table
1100 * @list: used internally
1101 * @chains_ht: chains in the table
1102 * @chains: same, for stable walks
1103 * @sets: sets in the table
1104 * @objects: stateful objects in the table
1105 * @flowtables: flow tables in the table
1106 * @hgenerator: handle generator state
1107 * @handle: table handle
1108 * @use: number of chain references to this table
1109 * @flags: table flag (see enum nft_table_flags)
1110 * @genmask: generation mask
1111 * @afinfo: address family info
1112 * @name: name of the table
1115 struct list_head list;
1116 struct rhltable chains_ht;
1117 struct list_head chains;
1118 struct list_head sets;
1119 struct list_head objects;
1120 struct list_head flowtables;
1133 static inline bool nft_table_has_owner(const struct nft_table *table)
1135 return table->flags & NFT_TABLE_F_OWNER;
1138 static inline bool nft_base_chain_netdev(int family, u32 hooknum)
1140 return family == NFPROTO_NETDEV ||
1141 (family == NFPROTO_INET && hooknum == NF_INET_INGRESS);
1144 void nft_register_chain_type(const struct nft_chain_type *);
1145 void nft_unregister_chain_type(const struct nft_chain_type *);
1147 int nft_register_expr(struct nft_expr_type *);
1148 void nft_unregister_expr(struct nft_expr_type *);
1150 int nft_verdict_dump(struct sk_buff *skb, int type,
1151 const struct nft_verdict *v);
1154 * struct nft_object_hash_key - key to lookup nft_object
1156 * @name: name of the stateful object to look up
1157 * @table: table the object belongs to
1159 struct nft_object_hash_key {
1161 const struct nft_table *table;
1165 * struct nft_object - nf_tables stateful object
1167 * @list: table stateful object list node
1168 * @key: keys that identify this object
1169 * @rhlhead: nft_objname_ht node
1170 * @genmask: generation mask
1171 * @use: number of references to this stateful object
1172 * @handle: unique object handle
1173 * @ops: object operations
1174 * @data: object data, layout depends on type
1177 struct list_head list;
1178 struct rhlist_head rhlhead;
1179 struct nft_object_hash_key key;
1185 /* runtime data below here */
1186 const struct nft_object_ops *ops ____cacheline_aligned;
1187 unsigned char data[]
1188 __attribute__((aligned(__alignof__(u64))));
1191 static inline void *nft_obj_data(const struct nft_object *obj)
1193 return (void *)obj->data;
1196 #define nft_expr_obj(expr) *((struct nft_object **)nft_expr_priv(expr))
1198 struct nft_object *nft_obj_lookup(const struct net *net,
1199 const struct nft_table *table,
1200 const struct nlattr *nla, u32 objtype,
1203 void nft_obj_notify(struct net *net, const struct nft_table *table,
1204 struct nft_object *obj, u32 portid, u32 seq,
1205 int event, u16 flags, int family, int report, gfp_t gfp);
1208 * struct nft_object_type - stateful object type
1210 * @select_ops: function to select nft_object_ops
1211 * @ops: default ops, used when no select_ops functions is present
1212 * @list: list node in list of object types
1213 * @type: stateful object numeric type
1214 * @owner: module owner
1215 * @maxattr: maximum netlink attribute
1216 * @policy: netlink attribute policy
1218 struct nft_object_type {
1219 const struct nft_object_ops *(*select_ops)(const struct nft_ctx *,
1220 const struct nlattr * const tb[]);
1221 const struct nft_object_ops *ops;
1222 struct list_head list;
1224 unsigned int maxattr;
1225 struct module *owner;
1226 const struct nla_policy *policy;
1230 * struct nft_object_ops - stateful object operations
1232 * @eval: stateful object evaluation function
1233 * @size: stateful object size
1234 * @init: initialize object from netlink attributes
1235 * @destroy: release existing stateful object
1236 * @dump: netlink dump stateful object
1237 * @update: update stateful object
1239 struct nft_object_ops {
1240 void (*eval)(struct nft_object *obj,
1241 struct nft_regs *regs,
1242 const struct nft_pktinfo *pkt);
1244 int (*init)(const struct nft_ctx *ctx,
1245 const struct nlattr *const tb[],
1246 struct nft_object *obj);
1247 void (*destroy)(const struct nft_ctx *ctx,
1248 struct nft_object *obj);
1249 int (*dump)(struct sk_buff *skb,
1250 struct nft_object *obj,
1252 void (*update)(struct nft_object *obj,
1253 struct nft_object *newobj);
1254 const struct nft_object_type *type;
1257 int nft_register_obj(struct nft_object_type *obj_type);
1258 void nft_unregister_obj(struct nft_object_type *obj_type);
1260 #define NFT_NETDEVICE_MAX 256
1263 * struct nft_flowtable - nf_tables flow table
1265 * @list: flow table list node in table list
1266 * @table: the table the flow table is contained in
1267 * @name: name of this flow table
1268 * @hooknum: hook number
1269 * @ops_len: number of hooks in array
1270 * @genmask: generation mask
1271 * @use: number of references to this flow table
1272 * @handle: unique object handle
1273 * @dev_name: array of device names
1274 * @data: rhashtable and garbage collector
1275 * @ops: array of hooks
1277 struct nft_flowtable {
1278 struct list_head list;
1279 struct nft_table *table;
1286 /* runtime data below here */
1287 struct list_head hook_list ____cacheline_aligned;
1288 struct nf_flowtable data;
1291 struct nft_flowtable *nft_flowtable_lookup(const struct nft_table *table,
1292 const struct nlattr *nla,
1295 void nf_tables_deactivate_flowtable(const struct nft_ctx *ctx,
1296 struct nft_flowtable *flowtable,
1297 enum nft_trans_phase phase);
1299 void nft_register_flowtable_type(struct nf_flowtable_type *type);
1300 void nft_unregister_flowtable_type(struct nf_flowtable_type *type);
1303 * struct nft_traceinfo - nft tracing information and state
1305 * @pkt: pktinfo currently processed
1306 * @basechain: base chain currently processed
1307 * @chain: chain currently processed
1308 * @rule: rule that was evaluated
1309 * @verdict: verdict given by rule
1310 * @type: event type (enum nft_trace_types)
1311 * @packet_dumped: packet headers sent in a previous traceinfo message
1312 * @trace: other struct members are initialised
1314 struct nft_traceinfo {
1315 const struct nft_pktinfo *pkt;
1316 const struct nft_base_chain *basechain;
1317 const struct nft_chain *chain;
1318 const struct nft_rule *rule;
1319 const struct nft_verdict *verdict;
1320 enum nft_trace_types type;
1325 void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
1326 const struct nft_verdict *verdict,
1327 const struct nft_chain *basechain);
1329 void nft_trace_notify(struct nft_traceinfo *info);
1331 #define MODULE_ALIAS_NFT_CHAIN(family, name) \
1332 MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
1334 #define MODULE_ALIAS_NFT_AF_EXPR(family, name) \
1335 MODULE_ALIAS("nft-expr-" __stringify(family) "-" name)
1337 #define MODULE_ALIAS_NFT_EXPR(name) \
1338 MODULE_ALIAS("nft-expr-" name)
1340 #define MODULE_ALIAS_NFT_OBJ(type) \
1341 MODULE_ALIAS("nft-obj-" __stringify(type))
1343 #if IS_ENABLED(CONFIG_NF_TABLES)
1346 * The gencursor defines two generations, the currently active and the
1347 * next one. Objects contain a bitmask of 2 bits specifying the generations
1348 * they're active in. A set bit means they're inactive in the generation
1349 * represented by that bit.
1351 * New objects start out as inactive in the current and active in the
1352 * next generation. When committing the ruleset the bitmask is cleared,
1353 * meaning they're active in all generations. When removing an object,
1354 * it is set inactive in the next generation. After committing the ruleset,
1355 * the objects are removed.
1357 static inline unsigned int nft_gencursor_next(const struct net *net)
1359 return net->nft.gencursor + 1 == 1 ? 1 : 0;
1362 static inline u8 nft_genmask_next(const struct net *net)
1364 return 1 << nft_gencursor_next(net);
1367 static inline u8 nft_genmask_cur(const struct net *net)
1369 /* Use READ_ONCE() to prevent refetching the value for atomicity */
1370 return 1 << READ_ONCE(net->nft.gencursor);
1373 #define NFT_GENMASK_ANY ((1 << 0) | (1 << 1))
1376 * Generic transaction helpers
1379 /* Check if this object is currently active. */
1380 #define nft_is_active(__net, __obj) \
1381 (((__obj)->genmask & nft_genmask_cur(__net)) == 0)
1383 /* Check if this object is active in the next generation. */
1384 #define nft_is_active_next(__net, __obj) \
1385 (((__obj)->genmask & nft_genmask_next(__net)) == 0)
1387 /* This object becomes active in the next generation. */
1388 #define nft_activate_next(__net, __obj) \
1389 (__obj)->genmask = nft_genmask_cur(__net)
1391 /* This object becomes inactive in the next generation. */
1392 #define nft_deactivate_next(__net, __obj) \
1393 (__obj)->genmask = nft_genmask_next(__net)
1395 /* After committing the ruleset, clear the stale generation bit. */
1396 #define nft_clear(__net, __obj) \
1397 (__obj)->genmask &= ~nft_genmask_next(__net)
1398 #define nft_active_genmask(__obj, __genmask) \
1399 !((__obj)->genmask & __genmask)
1402 * Set element transaction helpers
1405 static inline bool nft_set_elem_active(const struct nft_set_ext *ext,
1408 return !(ext->genmask & genmask);
1411 static inline void nft_set_elem_change_active(const struct net *net,
1412 const struct nft_set *set,
1413 struct nft_set_ext *ext)
1415 ext->genmask ^= nft_genmask_next(net);
1418 #endif /* IS_ENABLED(CONFIG_NF_TABLES) */
1421 * We use a free bit in the genmask field to indicate the element
1422 * is busy, meaning it is currently being processed either by
1423 * the netlink API or GC.
1425 * Even though the genmask is only a single byte wide, this works
1426 * because the extension structure if fully constant once initialized,
1427 * so there are no non-atomic write accesses unless it is already
1430 #define NFT_SET_ELEM_BUSY_MASK (1 << 2)
1432 #if defined(__LITTLE_ENDIAN_BITFIELD)
1433 #define NFT_SET_ELEM_BUSY_BIT 2
1434 #elif defined(__BIG_ENDIAN_BITFIELD)
1435 #define NFT_SET_ELEM_BUSY_BIT (BITS_PER_LONG - BITS_PER_BYTE + 2)
1440 static inline int nft_set_elem_mark_busy(struct nft_set_ext *ext)
1442 unsigned long *word = (unsigned long *)ext;
1444 BUILD_BUG_ON(offsetof(struct nft_set_ext, genmask) != 0);
1445 return test_and_set_bit(NFT_SET_ELEM_BUSY_BIT, word);
1448 static inline void nft_set_elem_clear_busy(struct nft_set_ext *ext)
1450 unsigned long *word = (unsigned long *)ext;
1452 clear_bit(NFT_SET_ELEM_BUSY_BIT, word);
1456 * struct nft_trans - nf_tables object update in transaction
1458 * @list: used internally
1459 * @msg_type: message type
1460 * @put_net: ctx->net needs to be put
1461 * @ctx: transaction context
1462 * @data: internal information related to the transaction
1465 struct list_head list;
1472 struct nft_trans_rule {
1473 struct nft_rule *rule;
1474 struct nft_flow_rule *flow;
1478 #define nft_trans_rule(trans) \
1479 (((struct nft_trans_rule *)trans->data)->rule)
1480 #define nft_trans_flow_rule(trans) \
1481 (((struct nft_trans_rule *)trans->data)->flow)
1482 #define nft_trans_rule_id(trans) \
1483 (((struct nft_trans_rule *)trans->data)->rule_id)
1485 struct nft_trans_set {
1486 struct nft_set *set;
1491 #define nft_trans_set(trans) \
1492 (((struct nft_trans_set *)trans->data)->set)
1493 #define nft_trans_set_id(trans) \
1494 (((struct nft_trans_set *)trans->data)->set_id)
1495 #define nft_trans_set_bound(trans) \
1496 (((struct nft_trans_set *)trans->data)->bound)
1498 struct nft_trans_chain {
1501 struct nft_stats __percpu *stats;
1506 #define nft_trans_chain_update(trans) \
1507 (((struct nft_trans_chain *)trans->data)->update)
1508 #define nft_trans_chain_name(trans) \
1509 (((struct nft_trans_chain *)trans->data)->name)
1510 #define nft_trans_chain_stats(trans) \
1511 (((struct nft_trans_chain *)trans->data)->stats)
1512 #define nft_trans_chain_policy(trans) \
1513 (((struct nft_trans_chain *)trans->data)->policy)
1514 #define nft_trans_chain_id(trans) \
1515 (((struct nft_trans_chain *)trans->data)->chain_id)
1517 struct nft_trans_table {
1521 #define nft_trans_table_update(trans) \
1522 (((struct nft_trans_table *)trans->data)->update)
1524 struct nft_trans_elem {
1525 struct nft_set *set;
1526 struct nft_set_elem elem;
1530 #define nft_trans_elem_set(trans) \
1531 (((struct nft_trans_elem *)trans->data)->set)
1532 #define nft_trans_elem(trans) \
1533 (((struct nft_trans_elem *)trans->data)->elem)
1534 #define nft_trans_elem_set_bound(trans) \
1535 (((struct nft_trans_elem *)trans->data)->bound)
1537 struct nft_trans_obj {
1538 struct nft_object *obj;
1539 struct nft_object *newobj;
1543 #define nft_trans_obj(trans) \
1544 (((struct nft_trans_obj *)trans->data)->obj)
1545 #define nft_trans_obj_newobj(trans) \
1546 (((struct nft_trans_obj *)trans->data)->newobj)
1547 #define nft_trans_obj_update(trans) \
1548 (((struct nft_trans_obj *)trans->data)->update)
1550 struct nft_trans_flowtable {
1551 struct nft_flowtable *flowtable;
1553 struct list_head hook_list;
1557 #define nft_trans_flowtable(trans) \
1558 (((struct nft_trans_flowtable *)trans->data)->flowtable)
1559 #define nft_trans_flowtable_update(trans) \
1560 (((struct nft_trans_flowtable *)trans->data)->update)
1561 #define nft_trans_flowtable_hooks(trans) \
1562 (((struct nft_trans_flowtable *)trans->data)->hook_list)
1563 #define nft_trans_flowtable_flags(trans) \
1564 (((struct nft_trans_flowtable *)trans->data)->flags)
1566 int __init nft_chain_filter_init(void);
1567 void nft_chain_filter_fini(void);
1569 void __init nft_chain_route_init(void);
1570 void nft_chain_route_fini(void);
1572 void nf_tables_trans_destroy_flush_work(void);
1574 int nf_msecs_to_jiffies64(const struct nlattr *nla, u64 *result);
1575 __be64 nf_jiffies64_to_msecs(u64 input);
1577 #ifdef CONFIG_MODULES
1578 __printf(2, 3) int nft_request_module(struct net *net, const char *fmt, ...);
1580 static inline int nft_request_module(struct net *net, const char *fmt, ...) { return -ENOENT; }
1583 struct nftables_pernet {
1584 struct list_head tables;
1585 struct list_head commit_list;
1586 struct list_head module_list;
1587 struct list_head notify_list;
1588 struct mutex commit_mutex;
1589 unsigned int base_seq;
1593 extern unsigned int nf_tables_net_id;
1595 static inline struct nftables_pernet *nft_pernet(const struct net *net)
1597 return net_generic(net, nf_tables_net_id);
1600 #endif /* _NET_NF_TABLES_H */