xref: /linux/include/net/netfilter/nf_flow_table.h (revision 3dcafe6cb5e1b467a25fde347c8c3949a2866adc)
1 #ifndef _NF_FLOW_TABLE_H
2 #define _NF_FLOW_TABLE_H
3 
4 #include <linux/in.h>
5 #include <linux/in6.h>
6 #include <linux/netdevice.h>
7 #include <linux/rhashtable-types.h>
8 #include <linux/rcupdate.h>
9 #include <linux/netfilter.h>
10 #include <linux/netfilter/nf_conntrack_tuple_common.h>
11 #include <net/flow_offload.h>
12 #include <net/dst.h>
13 #include <linux/if_pppox.h>
14 #include <linux/ppp_defs.h>
15 
16 struct nf_flowtable;
17 struct nf_flow_rule;
18 struct flow_offload;
19 enum flow_offload_tuple_dir;
20 
21 struct nf_flow_key {
22 	struct flow_dissector_key_meta			meta;
23 	struct flow_dissector_key_control		control;
24 	struct flow_dissector_key_control		enc_control;
25 	struct flow_dissector_key_basic			basic;
26 	struct flow_dissector_key_vlan			vlan;
27 	struct flow_dissector_key_vlan			cvlan;
28 	union {
29 		struct flow_dissector_key_ipv4_addrs	ipv4;
30 		struct flow_dissector_key_ipv6_addrs	ipv6;
31 	};
32 	struct flow_dissector_key_keyid			enc_key_id;
33 	union {
34 		struct flow_dissector_key_ipv4_addrs	enc_ipv4;
35 		struct flow_dissector_key_ipv6_addrs	enc_ipv6;
36 	};
37 	struct flow_dissector_key_tcp			tcp;
38 	struct flow_dissector_key_ports			tp;
39 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
40 
41 struct nf_flow_match {
42 	struct flow_dissector	dissector;
43 	struct nf_flow_key	key;
44 	struct nf_flow_key	mask;
45 };
46 
47 struct nf_flow_rule {
48 	struct nf_flow_match	match;
49 	struct flow_rule	*rule;
50 };
51 
52 struct nf_flowtable_type {
53 	struct list_head		list;
54 	int				family;
55 	int				(*init)(struct nf_flowtable *ft);
56 	bool				(*gc)(const struct flow_offload *flow);
57 	int				(*setup)(struct nf_flowtable *ft,
58 						 struct net_device *dev,
59 						 enum flow_block_command cmd);
60 	int				(*action)(struct net *net,
61 						  struct flow_offload *flow,
62 						  enum flow_offload_tuple_dir dir,
63 						  struct nf_flow_rule *flow_rule);
64 	void				(*free)(struct nf_flowtable *ft);
65 	void				(*get)(struct nf_flowtable *ft);
66 	void				(*put)(struct nf_flowtable *ft);
67 	nf_hookfn			*hook;
68 	struct module			*owner;
69 };
70 
71 enum nf_flowtable_flags {
72 	NF_FLOWTABLE_HW_OFFLOAD		= 0x1,	/* NFT_FLOWTABLE_HW_OFFLOAD */
73 	NF_FLOWTABLE_COUNTER		= 0x2,	/* NFT_FLOWTABLE_COUNTER */
74 };
75 
76 struct nf_flowtable {
77 	unsigned int			flags;		/* readonly in datapath */
78 	int				priority;	/* control path (padding hole) */
79 	struct rhashtable		rhashtable;	/* datapath, read-mostly members come first */
80 
81 	struct list_head		list;		/* slowpath parts */
82 	const struct nf_flowtable_type	*type;
83 	struct delayed_work		gc_work;
84 	struct flow_block		flow_block;
85 	struct rw_semaphore		flow_block_lock; /* Guards flow_block */
86 	possible_net_t			net;
87 };
88 
89 static inline bool nf_flowtable_hw_offload(struct nf_flowtable *flowtable)
90 {
91 	return flowtable->flags & NF_FLOWTABLE_HW_OFFLOAD;
92 }
93 
94 enum flow_offload_tuple_dir {
95 	FLOW_OFFLOAD_DIR_ORIGINAL = IP_CT_DIR_ORIGINAL,
96 	FLOW_OFFLOAD_DIR_REPLY = IP_CT_DIR_REPLY,
97 };
98 #define FLOW_OFFLOAD_DIR_MAX	IP_CT_DIR_MAX
99 
100 enum flow_offload_xmit_type {
101 	FLOW_OFFLOAD_XMIT_UNSPEC	= 0,
102 	FLOW_OFFLOAD_XMIT_NEIGH,
103 	FLOW_OFFLOAD_XMIT_XFRM,
104 	FLOW_OFFLOAD_XMIT_DIRECT,
105 	FLOW_OFFLOAD_XMIT_TC,
106 };
107 
108 #define NF_FLOW_TABLE_ENCAP_MAX		2
109 
110 struct flow_offload_tunnel {
111 	union {
112 		struct in_addr	src_v4;
113 		struct in6_addr	src_v6;
114 	};
115 	union {
116 		struct in_addr	dst_v4;
117 		struct in6_addr	dst_v6;
118 	};
119 
120 	u8	inner_proto;
121 	u8	encap_proto;
122 };
123 
124 struct flow_offload_tuple {
125 	union {
126 		struct in_addr		src_v4;
127 		struct in6_addr		src_v6;
128 	};
129 	union {
130 		struct in_addr		dst_v4;
131 		struct in6_addr		dst_v6;
132 	};
133 	struct {
134 		__be16			src_port;
135 		__be16			dst_port;
136 	};
137 
138 	int				iifidx;
139 
140 	u8				l3proto;
141 	u8				l4proto;
142 	struct {
143 		u16			id;
144 		__be16			proto;
145 	} encap[NF_FLOW_TABLE_ENCAP_MAX];
146 
147 	struct flow_offload_tunnel	tun;
148 
149 	/* All members above are keys for lookups, see flow_offload_hash(). */
150 	struct { }			__hash;
151 
152 	u16				dir:2,
153 					xmit_type:3,
154 					encap_num:2,
155 					needs_gso_segment:1,
156 					tun_num:2,
157 					in_vlan_ingress:2;
158 	u16				mtu;
159 	u32				dst_cookie;
160 	struct dst_entry		*dst_cache;
161 
162 	union {
163 		struct {
164 			u32		ifidx;
165 		};
166 		struct {
167 			u32		ifidx;
168 			u8		h_source[ETH_ALEN];
169 			u8		h_dest[ETH_ALEN];
170 		} out;
171 		struct {
172 			u32		iifidx;
173 		} tc;
174 	};
175 };
176 
177 struct flow_offload_tuple_rhash {
178 	struct rhash_head		node;
179 	struct flow_offload_tuple	tuple;
180 };
181 
182 enum nf_flow_flags {
183 	NF_FLOW_SNAT,
184 	NF_FLOW_DNAT,
185 	NF_FLOW_CLOSING,
186 	NF_FLOW_TEARDOWN,
187 	NF_FLOW_PENDING,
188 	NF_FLOW_HW,
189 	NF_FLOW_HW_DYING,
190 	NF_FLOW_HW_DEAD,
191 	NF_FLOW_HW_BIDIRECTIONAL,
192 	NF_FLOW_HW_ESTABLISHED,
193 };
194 
195 enum flow_offload_type {
196 	NF_FLOW_OFFLOAD_UNSPEC	= 0,
197 	NF_FLOW_OFFLOAD_ROUTE,
198 };
199 
200 struct flow_offload {
201 	struct flow_offload_tuple_rhash		tuplehash[FLOW_OFFLOAD_DIR_MAX];
202 	struct nf_conn				*ct;
203 	unsigned long				flags;
204 	u16					type;
205 	u32					timeout;
206 	struct rcu_head				rcu_head;
207 };
208 
209 #define NF_FLOW_TIMEOUT (30 * HZ)
210 #define nf_flowtable_time_stamp	(u32)jiffies
211 
212 unsigned long flow_offload_get_timeout(struct flow_offload *flow);
213 
214 static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
215 {
216 	return (__s32)(timeout - nf_flowtable_time_stamp);
217 }
218 
219 struct nf_flow_route {
220 	struct {
221 		struct dst_entry		*dst;
222 		struct {
223 			u32			ifindex;
224 			struct {
225 				u16		id;
226 				__be16		proto;
227 			} encap[NF_FLOW_TABLE_ENCAP_MAX];
228 			struct flow_offload_tunnel tun;
229 			u8			num_encaps:2,
230 						num_tuns:2,
231 						ingress_vlans:2;
232 		} in;
233 		struct {
234 			u32			ifindex;
235 			u32			hw_ifindex;
236 			u8			h_source[ETH_ALEN];
237 			u8			h_dest[ETH_ALEN];
238 			u8			needs_gso_segment:1;
239 		} out;
240 		enum flow_offload_xmit_type	xmit_type;
241 	} tuple[FLOW_OFFLOAD_DIR_MAX];
242 };
243 
244 struct flow_offload *flow_offload_alloc(struct nf_conn *ct);
245 void flow_offload_free(struct flow_offload *flow);
246 
247 struct nft_flowtable;
248 struct nft_pktinfo;
249 int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
250 		   struct nf_flow_route *route, enum ip_conntrack_dir dir,
251 		   struct nft_flowtable *ft);
252 
253 static inline int
254 nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
255 			     flow_setup_cb_t *cb, void *cb_priv)
256 {
257 	struct flow_block *block = &flow_table->flow_block;
258 	struct flow_block_cb *block_cb;
259 	int err = 0;
260 
261 	down_write(&flow_table->flow_block_lock);
262 	block_cb = flow_block_cb_lookup(block, cb, cb_priv);
263 	if (block_cb) {
264 		err = -EEXIST;
265 		goto unlock;
266 	}
267 
268 	block_cb = flow_block_cb_alloc(cb, cb_priv, cb_priv, NULL);
269 	if (IS_ERR(block_cb)) {
270 		err = PTR_ERR(block_cb);
271 		goto unlock;
272 	}
273 
274 	list_add_tail(&block_cb->list, &block->cb_list);
275 	up_write(&flow_table->flow_block_lock);
276 
277 	if (flow_table->type->get)
278 		flow_table->type->get(flow_table);
279 	return 0;
280 
281 unlock:
282 	up_write(&flow_table->flow_block_lock);
283 	return err;
284 }
285 
286 static inline void
287 nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
288 			     flow_setup_cb_t *cb, void *cb_priv)
289 {
290 	struct flow_block *block = &flow_table->flow_block;
291 	struct flow_block_cb *block_cb;
292 
293 	down_write(&flow_table->flow_block_lock);
294 	block_cb = flow_block_cb_lookup(block, cb, cb_priv);
295 	if (block_cb) {
296 		list_del(&block_cb->list);
297 		flow_block_cb_free(block_cb);
298 	} else {
299 		WARN_ON(true);
300 	}
301 	up_write(&flow_table->flow_block_lock);
302 
303 	if (flow_table->type->put)
304 		flow_table->type->put(flow_table);
305 }
306 
307 void flow_offload_route_init(struct flow_offload *flow,
308 			     struct nf_flow_route *route);
309 
310 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
311 void flow_offload_refresh(struct nf_flowtable *flow_table,
312 			  struct flow_offload *flow, bool force);
313 
314 static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
315 {
316 	if (!tuple->dst_cache)
317 		return true;
318 
319 	return dst_check(tuple->dst_cache, tuple->dst_cookie);
320 }
321 
322 struct flow_offload_tuple_rhash *flow_offload_lookup(struct nf_flowtable *flow_table,
323 						     struct flow_offload_tuple *tuple);
324 void nf_flow_table_gc_run(struct nf_flowtable *flow_table);
325 void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
326 			      struct net_device *dev);
327 void nf_flow_table_cleanup(struct net_device *dev);
328 
329 int nf_flow_table_init(struct nf_flowtable *flow_table);
330 void nf_flow_table_free(struct nf_flowtable *flow_table);
331 
332 void flow_offload_teardown(struct flow_offload *flow);
333 
334 void nf_flow_snat_port(const struct flow_offload *flow,
335 		       struct sk_buff *skb, unsigned int thoff,
336 		       u8 protocol, enum flow_offload_tuple_dir dir);
337 void nf_flow_dnat_port(const struct flow_offload *flow,
338 		       struct sk_buff *skb, unsigned int thoff,
339 		       u8 protocol, enum flow_offload_tuple_dir dir);
340 
341 struct flow_ports {
342 	__be16 source, dest;
343 };
344 
345 struct nf_flowtable *nf_flowtable_by_dev(const struct net_device *dev);
346 int nf_flow_offload_xdp_setup(struct nf_flowtable *flowtable,
347 			      struct net_device *dev,
348 			      enum flow_block_command cmd);
349 
350 unsigned int nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
351 				     const struct nf_hook_state *state);
352 unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
353 				       const struct nf_hook_state *state);
354 
355 #if (IS_BUILTIN(CONFIG_NF_FLOW_TABLE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
356     (IS_MODULE(CONFIG_NF_FLOW_TABLE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
357 extern int nf_flow_register_bpf(void);
358 #else
359 static inline int nf_flow_register_bpf(void)
360 {
361 	return 0;
362 }
363 #endif
364 
365 #define MODULE_ALIAS_NF_FLOWTABLE(family)	\
366 	MODULE_ALIAS("nf-flowtable-" __stringify(family))
367 
368 void nf_flow_offload_add(struct nf_flowtable *flowtable,
369 			 struct flow_offload *flow);
370 void nf_flow_offload_refresh(struct nf_flowtable *flowtable,
371 			     struct flow_offload *flow);
372 void nf_flow_offload_del(struct nf_flowtable *flowtable,
373 			 struct flow_offload *flow);
374 void nf_flow_offload_stats(struct nf_flowtable *flowtable,
375 			   struct flow_offload *flow);
376 
377 void nf_flow_table_offload_flush(struct nf_flowtable *flowtable);
378 void nf_flow_table_offload_flush_cleanup(struct nf_flowtable *flowtable);
379 
380 int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
381 				struct net_device *dev,
382 				enum flow_block_command cmd);
383 int nf_flow_rule_route_ipv4(struct net *net, struct flow_offload *flow,
384 			    enum flow_offload_tuple_dir dir,
385 			    struct nf_flow_rule *flow_rule);
386 int nf_flow_rule_route_ipv6(struct net *net, struct flow_offload *flow,
387 			    enum flow_offload_tuple_dir dir,
388 			    struct nf_flow_rule *flow_rule);
389 
390 int nf_flow_table_offload_init(void);
391 void nf_flow_table_offload_exit(void);
392 
393 static inline __be16 __nf_flow_pppoe_proto(const struct sk_buff *skb)
394 {
395 	__be16 proto;
396 
397 	proto = *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
398 			     sizeof(struct pppoe_hdr)));
399 	switch (proto) {
400 	case htons(PPP_IP):
401 		return htons(ETH_P_IP);
402 	case htons(PPP_IPV6):
403 		return htons(ETH_P_IPV6);
404 	}
405 
406 	return 0;
407 }
408 
409 static inline bool nf_flow_pppoe_proto(struct sk_buff *skb, __be16 *inner_proto)
410 {
411 	if (!pskb_may_pull(skb, ETH_HLEN + PPPOE_SES_HLEN))
412 		return false;
413 
414 	*inner_proto = __nf_flow_pppoe_proto(skb);
415 
416 	return true;
417 }
418 
419 #define NF_FLOW_TABLE_STAT_INC(net, count) __this_cpu_inc((net)->ft.stat->count)
420 #define NF_FLOW_TABLE_STAT_DEC(net, count) __this_cpu_dec((net)->ft.stat->count)
421 #define NF_FLOW_TABLE_STAT_INC_ATOMIC(net, count)	\
422 	this_cpu_inc((net)->ft.stat->count)
423 #define NF_FLOW_TABLE_STAT_DEC_ATOMIC(net, count)	\
424 	this_cpu_dec((net)->ft.stat->count)
425 
426 #ifdef CONFIG_NF_FLOW_TABLE_PROCFS
427 int nf_flow_table_init_proc(struct net *net);
428 void nf_flow_table_fini_proc(struct net *net);
429 #else
430 static inline int nf_flow_table_init_proc(struct net *net)
431 {
432 	return 0;
433 }
434 
435 static inline void nf_flow_table_fini_proc(struct net *net)
436 {
437 }
438 #endif /* CONFIG_NF_FLOW_TABLE_PROCFS */
439 
440 #endif /* _NF_FLOW_TABLE_H */
441