1 #ifndef _NF_FLOW_TABLE_H
2 #define _NF_FLOW_TABLE_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>
17 enum flow_offload_tuple_dir;
20 struct flow_dissector_key_meta meta;
21 struct flow_dissector_key_control control;
22 struct flow_dissector_key_control enc_control;
23 struct flow_dissector_key_basic basic;
24 struct flow_dissector_key_vlan vlan;
25 struct flow_dissector_key_vlan cvlan;
27 struct flow_dissector_key_ipv4_addrs ipv4;
28 struct flow_dissector_key_ipv6_addrs ipv6;
30 struct flow_dissector_key_keyid enc_key_id;
32 struct flow_dissector_key_ipv4_addrs enc_ipv4;
33 struct flow_dissector_key_ipv6_addrs enc_ipv6;
35 struct flow_dissector_key_tcp tcp;
36 struct flow_dissector_key_ports tp;
37 } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
39 struct nf_flow_match {
40 struct flow_dissector dissector;
41 struct nf_flow_key key;
42 struct nf_flow_key mask;
46 struct nf_flow_match match;
47 struct flow_rule *rule;
50 struct nf_flowtable_type {
51 struct list_head list;
53 int (*init)(struct nf_flowtable *ft);
54 int (*setup)(struct nf_flowtable *ft,
55 struct net_device *dev,
56 enum flow_block_command cmd);
57 int (*action)(struct net *net,
58 const struct flow_offload *flow,
59 enum flow_offload_tuple_dir dir,
60 struct nf_flow_rule *flow_rule);
61 void (*free)(struct nf_flowtable *ft);
66 enum nf_flowtable_flags {
67 NF_FLOWTABLE_HW_OFFLOAD = 0x1, /* NFT_FLOWTABLE_HW_OFFLOAD */
68 NF_FLOWTABLE_COUNTER = 0x2, /* NFT_FLOWTABLE_COUNTER */
72 struct list_head list;
73 struct rhashtable rhashtable;
75 const struct nf_flowtable_type *type;
76 struct delayed_work gc_work;
78 struct flow_block flow_block;
79 struct rw_semaphore flow_block_lock; /* Guards flow_block */
83 static inline bool nf_flowtable_hw_offload(struct nf_flowtable *flowtable)
85 return flowtable->flags & NF_FLOWTABLE_HW_OFFLOAD;
88 enum flow_offload_tuple_dir {
89 FLOW_OFFLOAD_DIR_ORIGINAL = IP_CT_DIR_ORIGINAL,
90 FLOW_OFFLOAD_DIR_REPLY = IP_CT_DIR_REPLY,
92 #define FLOW_OFFLOAD_DIR_MAX IP_CT_DIR_MAX
94 enum flow_offload_xmit_type {
95 FLOW_OFFLOAD_XMIT_UNSPEC = 0,
96 FLOW_OFFLOAD_XMIT_NEIGH,
97 FLOW_OFFLOAD_XMIT_XFRM,
98 FLOW_OFFLOAD_XMIT_DIRECT,
101 #define NF_FLOW_TABLE_ENCAP_MAX 2
103 struct flow_offload_tuple {
105 struct in_addr src_v4;
106 struct in6_addr src_v6;
109 struct in_addr dst_v4;
110 struct in6_addr dst_v6;
124 } encap[NF_FLOW_TABLE_ENCAP_MAX];
126 /* All members above are keys for lookups, see flow_offload_hash(). */
136 struct dst_entry *dst_cache;
142 u8 h_source[ETH_ALEN];
148 struct flow_offload_tuple_rhash {
149 struct rhash_head node;
150 struct flow_offload_tuple tuple;
163 enum flow_offload_type {
164 NF_FLOW_OFFLOAD_UNSPEC = 0,
165 NF_FLOW_OFFLOAD_ROUTE,
168 struct flow_offload {
169 struct flow_offload_tuple_rhash tuplehash[FLOW_OFFLOAD_DIR_MAX];
174 struct rcu_head rcu_head;
177 #define NF_FLOW_TIMEOUT (30 * HZ)
178 #define nf_flowtable_time_stamp (u32)jiffies
180 static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
182 return (__s32)(timeout - nf_flowtable_time_stamp);
185 struct nf_flow_route {
187 struct dst_entry *dst;
193 } encap[NF_FLOW_TABLE_ENCAP_MAX];
200 u8 h_source[ETH_ALEN];
203 enum flow_offload_xmit_type xmit_type;
204 } tuple[FLOW_OFFLOAD_DIR_MAX];
207 struct flow_offload *flow_offload_alloc(struct nf_conn *ct);
208 void flow_offload_free(struct flow_offload *flow);
211 nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
212 flow_setup_cb_t *cb, void *cb_priv)
214 struct flow_block *block = &flow_table->flow_block;
215 struct flow_block_cb *block_cb;
218 down_write(&flow_table->flow_block_lock);
219 block_cb = flow_block_cb_lookup(block, cb, cb_priv);
225 block_cb = flow_block_cb_alloc(cb, cb_priv, cb_priv, NULL);
226 if (IS_ERR(block_cb)) {
227 err = PTR_ERR(block_cb);
231 list_add_tail(&block_cb->list, &block->cb_list);
234 up_write(&flow_table->flow_block_lock);
239 nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
240 flow_setup_cb_t *cb, void *cb_priv)
242 struct flow_block *block = &flow_table->flow_block;
243 struct flow_block_cb *block_cb;
245 down_write(&flow_table->flow_block_lock);
246 block_cb = flow_block_cb_lookup(block, cb, cb_priv);
248 list_del(&block_cb->list);
249 flow_block_cb_free(block_cb);
253 up_write(&flow_table->flow_block_lock);
256 int flow_offload_route_init(struct flow_offload *flow,
257 const struct nf_flow_route *route);
259 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
260 void flow_offload_refresh(struct nf_flowtable *flow_table,
261 struct flow_offload *flow);
263 struct flow_offload_tuple_rhash *flow_offload_lookup(struct nf_flowtable *flow_table,
264 struct flow_offload_tuple *tuple);
265 void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
266 struct net_device *dev);
267 void nf_flow_table_cleanup(struct net_device *dev);
269 int nf_flow_table_init(struct nf_flowtable *flow_table);
270 void nf_flow_table_free(struct nf_flowtable *flow_table);
272 void flow_offload_teardown(struct flow_offload *flow);
274 void nf_flow_snat_port(const struct flow_offload *flow,
275 struct sk_buff *skb, unsigned int thoff,
276 u8 protocol, enum flow_offload_tuple_dir dir);
277 void nf_flow_dnat_port(const struct flow_offload *flow,
278 struct sk_buff *skb, unsigned int thoff,
279 u8 protocol, enum flow_offload_tuple_dir dir);
285 unsigned int nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
286 const struct nf_hook_state *state);
287 unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
288 const struct nf_hook_state *state);
290 #define MODULE_ALIAS_NF_FLOWTABLE(family) \
291 MODULE_ALIAS("nf-flowtable-" __stringify(family))
293 void nf_flow_offload_add(struct nf_flowtable *flowtable,
294 struct flow_offload *flow);
295 void nf_flow_offload_del(struct nf_flowtable *flowtable,
296 struct flow_offload *flow);
297 void nf_flow_offload_stats(struct nf_flowtable *flowtable,
298 struct flow_offload *flow);
300 void nf_flow_table_offload_flush(struct nf_flowtable *flowtable);
301 int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
302 struct net_device *dev,
303 enum flow_block_command cmd);
304 int nf_flow_rule_route_ipv4(struct net *net, const struct flow_offload *flow,
305 enum flow_offload_tuple_dir dir,
306 struct nf_flow_rule *flow_rule);
307 int nf_flow_rule_route_ipv6(struct net *net, const struct flow_offload *flow,
308 enum flow_offload_tuple_dir dir,
309 struct nf_flow_rule *flow_rule);
311 int nf_flow_table_offload_init(void);
312 void nf_flow_table_offload_exit(void);
314 #endif /* _NF_FLOW_TABLE_H */