xref: /linux/include/net/netfilter/nf_tables_core.h (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NET_NF_TABLES_CORE_H
3 #define _NET_NF_TABLES_CORE_H
4 
5 #include <net/netfilter/nf_tables.h>
6 #include <linux/indirect_call_wrapper.h>
7 
8 extern struct nft_expr_type nft_imm_type;
9 extern struct nft_expr_type nft_cmp_type;
10 extern struct nft_expr_type nft_counter_type;
11 extern struct nft_expr_type nft_lookup_type;
12 extern struct nft_expr_type nft_bitwise_type;
13 extern struct nft_expr_type nft_byteorder_type;
14 extern struct nft_expr_type nft_payload_type;
15 extern struct nft_expr_type nft_dynset_type;
16 extern struct nft_expr_type nft_range_type;
17 extern struct nft_expr_type nft_meta_type;
18 extern struct nft_expr_type nft_rt_type;
19 extern struct nft_expr_type nft_exthdr_type;
20 extern struct nft_expr_type nft_last_type;
21 extern struct nft_expr_type nft_objref_type;
22 extern struct nft_expr_type nft_inner_type;
23 
24 #ifdef CONFIG_NETWORK_SECMARK
25 extern struct nft_object_type nft_secmark_obj_type;
26 #endif
27 extern struct nft_object_type nft_counter_obj_type;
28 
29 int nf_tables_core_module_init(void);
30 void nf_tables_core_module_exit(void);
31 
32 struct nft_bitwise_fast_expr {
33 	u32			mask;
34 	u32			xor;
35 	u8			sreg;
36 	u8			dreg;
37 };
38 
39 struct nft_cmp_fast_expr {
40 	u32			data;
41 	u32			mask;
42 	u8			sreg;
43 	u8			len;
44 	bool			inv;
45 };
46 
47 struct nft_cmp16_fast_expr {
48 	struct nft_data		data;
49 	struct nft_data		mask;
50 	u8			sreg;
51 	u8			len;
52 	bool			inv;
53 };
54 
55 struct nft_immediate_expr {
56 	struct nft_data		data;
57 	u8			dreg;
58 	u8			dlen;
59 };
60 
61 extern const struct nft_expr_ops nft_cmp_fast_ops;
62 extern const struct nft_expr_ops nft_cmp16_fast_ops;
63 
64 struct nft_ct {
65 	enum nft_ct_keys	key:8;
66 	enum ip_conntrack_dir	dir:8;
67 	u8			len;
68 	union {
69 		u8		dreg;
70 		u8		sreg;
71 	};
72 };
73 
74 struct nft_payload {
75 	enum nft_payload_bases	base:8;
76 	u8			offset;
77 	u8			len;
78 	u8			dreg;
79 };
80 
81 extern const struct nft_expr_ops nft_payload_fast_ops;
82 
83 extern const struct nft_expr_ops nft_bitwise_fast_ops;
84 
85 extern struct static_key_false nft_counters_enabled;
86 extern struct static_key_false nft_trace_enabled;
87 
88 extern const struct nft_set_type nft_set_rhash_type;
89 extern const struct nft_set_type nft_set_hash_type;
90 extern const struct nft_set_type nft_set_hash_fast_type;
91 extern const struct nft_set_type nft_set_rbtree_type;
92 extern const struct nft_set_type nft_set_bitmap_type;
93 extern const struct nft_set_type nft_set_pipapo_type;
94 extern const struct nft_set_type nft_set_pipapo_avx2_type;
95 
96 #ifdef CONFIG_MITIGATION_RETPOLINE
97 const struct nft_set_ext *
98 nft_rhash_lookup(const struct net *net, const struct nft_set *set,
99 		 const u32 *key);
100 const struct nft_set_ext *
101 nft_rbtree_lookup(const struct net *net, const struct nft_set *set,
102 		  const u32 *key);
103 const struct nft_set_ext *
104 nft_bitmap_lookup(const struct net *net, const struct nft_set *set,
105 		  const u32 *key);
106 const struct nft_set_ext *
107 nft_hash_lookup_fast(const struct net *net, const struct nft_set *set,
108 		     const u32 *key);
109 const struct nft_set_ext *
110 nft_hash_lookup(const struct net *net, const struct nft_set *set,
111 		const u32 *key);
112 const struct nft_set_ext *
113 nft_set_do_lookup(const struct net *net, const struct nft_set *set,
114 		  const u32 *key);
115 #else
116 static inline const struct nft_set_ext *
nft_set_do_lookup(const struct net * net,const struct nft_set * set,const u32 * key)117 nft_set_do_lookup(const struct net *net, const struct nft_set *set,
118 		  const u32 *key)
119 {
120 	return set->ops->lookup(net, set, key);
121 }
122 #endif
123 
124 /* called from nft_pipapo_avx2.c */
125 const struct nft_set_ext *
126 nft_pipapo_lookup(const struct net *net, const struct nft_set *set,
127 		  const u32 *key);
128 /* called from nft_set_pipapo.c */
129 const struct nft_set_ext *
130 nft_pipapo_avx2_lookup(const struct net *net, const struct nft_set *set,
131 			const u32 *key);
132 
133 void nft_counter_init_seqcount(void);
134 
135 struct nft_expr;
136 struct nft_regs;
137 struct nft_pktinfo;
138 void nft_meta_get_eval(const struct nft_expr *expr,
139 		       struct nft_regs *regs, const struct nft_pktinfo *pkt);
140 void nft_cmp_eval(const struct nft_expr *expr,
141 		  struct nft_regs *regs, const struct nft_pktinfo *pkt);
142 void nft_lookup_eval(const struct nft_expr *expr,
143 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
144 void nft_payload_eval(const struct nft_expr *expr,
145 		      struct nft_regs *regs, const struct nft_pktinfo *pkt);
146 void nft_immediate_eval(const struct nft_expr *expr,
147 			struct nft_regs *regs, const struct nft_pktinfo *pkt);
148 void nft_bitwise_eval(const struct nft_expr *expr,
149 		      struct nft_regs *regs, const struct nft_pktinfo *pkt);
150 void nft_range_eval(const struct nft_expr *expr,
151 		    struct nft_regs *regs, const struct nft_pktinfo *pkt);
152 void nft_byteorder_eval(const struct nft_expr *expr,
153 			struct nft_regs *regs, const struct nft_pktinfo *pkt);
154 void nft_dynset_eval(const struct nft_expr *expr,
155 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
156 void nft_rt_get_eval(const struct nft_expr *expr,
157 		     struct nft_regs *regs, const struct nft_pktinfo *pkt);
158 void nft_counter_eval(const struct nft_expr *expr, struct nft_regs *regs,
159                       const struct nft_pktinfo *pkt);
160 void nft_ct_get_fast_eval(const struct nft_expr *expr,
161 			  struct nft_regs *regs, const struct nft_pktinfo *pkt);
162 
163 enum {
164 	NFT_PAYLOAD_CTX_INNER_TUN	= (1 << 0),
165 	NFT_PAYLOAD_CTX_INNER_LL	= (1 << 1),
166 	NFT_PAYLOAD_CTX_INNER_NH	= (1 << 2),
167 	NFT_PAYLOAD_CTX_INNER_TH	= (1 << 3),
168 };
169 
170 struct nft_inner_tun_ctx {
171 	unsigned long cookie;
172 	u16	type;
173 	u16	inner_tunoff;
174 	u16	inner_lloff;
175 	u16	inner_nhoff;
176 	u16	inner_thoff;
177 	__be16	llproto;
178 	u8	l4proto;
179 	u8      flags;
180 };
181 
182 int nft_payload_inner_offset(const struct nft_pktinfo *pkt);
183 void nft_payload_inner_eval(const struct nft_expr *expr, struct nft_regs *regs,
184 			    const struct nft_pktinfo *pkt,
185 			    struct nft_inner_tun_ctx *ctx);
186 
187 void nft_objref_eval(const struct nft_expr *expr, struct nft_regs *regs,
188 		     const struct nft_pktinfo *pkt);
189 void nft_objref_map_eval(const struct nft_expr *expr, struct nft_regs *regs,
190 			 const struct nft_pktinfo *pkt);
191 struct nft_elem_priv *nft_dynset_new(struct nft_set *set,
192 				     const struct nft_expr *expr,
193 				     struct nft_regs *regs);
194 #endif /* _NET_NF_TABLES_CORE_H */
195