1 #include <linux/kernel.h>
2 #include <linux/init.h>
3 #include <linux/module.h>
4 #include <linux/netfilter.h>
5 #include <linux/rhashtable.h>
6 #include <linux/netdevice.h>
8 #include <net/ip6_route.h>
9 #include <net/netfilter/nf_tables.h>
10 #include <net/netfilter/nf_flow_table.h>
11 #include <net/netfilter/nf_conntrack.h>
12 #include <net/netfilter/nf_conntrack_core.h>
13 #include <net/netfilter/nf_conntrack_tuple.h>
15 struct flow_offload_entry {
16 struct flow_offload flow;
18 struct rcu_head rcu_head;
21 static DEFINE_MUTEX(flowtable_lock);
22 static LIST_HEAD(flowtables);
25 flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
26 struct nf_flow_route *route,
27 enum flow_offload_tuple_dir dir)
29 struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
30 struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
31 struct dst_entry *dst = route->tuple[dir].dst;
35 switch (ctt->src.l3num) {
37 ft->src_v4 = ctt->src.u3.in;
38 ft->dst_v4 = ctt->dst.u3.in;
39 ft->mtu = ip_dst_mtu_maybe_forward(dst, true);
42 ft->src_v6 = ctt->src.u3.in6;
43 ft->dst_v6 = ctt->dst.u3.in6;
44 ft->mtu = ip6_dst_mtu_forward(dst);
48 ft->l3proto = ctt->src.l3num;
49 ft->l4proto = ctt->dst.protonum;
50 ft->src_port = ctt->src.u.tcp.port;
51 ft->dst_port = ctt->dst.u.tcp.port;
53 ft->iifidx = route->tuple[dir].ifindex;
54 ft->oifidx = route->tuple[!dir].ifindex;
59 flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
61 struct flow_offload_entry *entry;
62 struct flow_offload *flow;
64 if (unlikely(nf_ct_is_dying(ct) ||
65 !atomic_inc_not_zero(&ct->ct_general.use)))
68 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
74 if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst))
75 goto err_dst_cache_original;
77 if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst))
78 goto err_dst_cache_reply;
82 flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
83 flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
85 if (ct->status & IPS_SRC_NAT)
86 flow->flags |= FLOW_OFFLOAD_SNAT;
87 if (ct->status & IPS_DST_NAT)
88 flow->flags |= FLOW_OFFLOAD_DNAT;
93 dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
94 err_dst_cache_original:
101 EXPORT_SYMBOL_GPL(flow_offload_alloc);
103 static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
105 tcp->state = TCP_CONNTRACK_ESTABLISHED;
106 tcp->seen[0].td_maxwin = 0;
107 tcp->seen[1].td_maxwin = 0;
110 #define NF_FLOWTABLE_TCP_PICKUP_TIMEOUT (120 * HZ)
111 #define NF_FLOWTABLE_UDP_PICKUP_TIMEOUT (30 * HZ)
113 static void flow_offload_fixup_ct_state(struct nf_conn *ct)
115 const struct nf_conntrack_l4proto *l4proto;
116 unsigned int timeout;
119 l4num = nf_ct_protonum(ct);
120 if (l4num == IPPROTO_TCP)
121 flow_offload_fixup_tcp(&ct->proto.tcp);
123 l4proto = __nf_ct_l4proto_find(l4num);
127 if (l4num == IPPROTO_TCP)
128 timeout = NF_FLOWTABLE_TCP_PICKUP_TIMEOUT;
129 else if (l4num == IPPROTO_UDP)
130 timeout = NF_FLOWTABLE_UDP_PICKUP_TIMEOUT;
134 ct->timeout = nfct_time_stamp + timeout;
137 void flow_offload_free(struct flow_offload *flow)
139 struct flow_offload_entry *e;
141 dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
142 dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
143 e = container_of(flow, struct flow_offload_entry, flow);
144 if (flow->flags & FLOW_OFFLOAD_DYING)
145 nf_ct_delete(e->ct, 0, 0);
147 kfree_rcu(e, rcu_head);
149 EXPORT_SYMBOL_GPL(flow_offload_free);
151 static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
153 const struct flow_offload_tuple *tuple = data;
155 return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
158 static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
160 const struct flow_offload_tuple_rhash *tuplehash = data;
162 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
165 static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
168 const struct flow_offload_tuple *tuple = arg->key;
169 const struct flow_offload_tuple_rhash *x = ptr;
171 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
177 static const struct rhashtable_params nf_flow_offload_rhash_params = {
178 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
179 .hashfn = flow_offload_hash,
180 .obj_hashfn = flow_offload_hash_obj,
181 .obj_cmpfn = flow_offload_hash_cmp,
182 .automatic_shrinking = true,
185 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
187 flow->timeout = (u32)jiffies;
189 rhashtable_insert_fast(&flow_table->rhashtable,
190 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
191 nf_flow_offload_rhash_params);
192 rhashtable_insert_fast(&flow_table->rhashtable,
193 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
194 nf_flow_offload_rhash_params);
197 EXPORT_SYMBOL_GPL(flow_offload_add);
199 static void flow_offload_del(struct nf_flowtable *flow_table,
200 struct flow_offload *flow)
202 struct flow_offload_entry *e;
204 rhashtable_remove_fast(&flow_table->rhashtable,
205 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
206 nf_flow_offload_rhash_params);
207 rhashtable_remove_fast(&flow_table->rhashtable,
208 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
209 nf_flow_offload_rhash_params);
211 e = container_of(flow, struct flow_offload_entry, flow);
212 clear_bit(IPS_OFFLOAD_BIT, &e->ct->status);
214 flow_offload_free(flow);
217 void flow_offload_teardown(struct flow_offload *flow)
219 struct flow_offload_entry *e;
221 flow->flags |= FLOW_OFFLOAD_TEARDOWN;
223 e = container_of(flow, struct flow_offload_entry, flow);
224 flow_offload_fixup_ct_state(e->ct);
226 EXPORT_SYMBOL_GPL(flow_offload_teardown);
228 struct flow_offload_tuple_rhash *
229 flow_offload_lookup(struct nf_flowtable *flow_table,
230 struct flow_offload_tuple *tuple)
232 struct flow_offload_tuple_rhash *tuplehash;
233 struct flow_offload *flow;
236 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
237 nf_flow_offload_rhash_params);
241 dir = tuplehash->tuple.dir;
242 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
243 if (flow->flags & (FLOW_OFFLOAD_DYING | FLOW_OFFLOAD_TEARDOWN))
248 EXPORT_SYMBOL_GPL(flow_offload_lookup);
251 nf_flow_table_iterate(struct nf_flowtable *flow_table,
252 void (*iter)(struct flow_offload *flow, void *data),
255 struct flow_offload_tuple_rhash *tuplehash;
256 struct rhashtable_iter hti;
257 struct flow_offload *flow;
260 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
261 rhashtable_walk_start(&hti);
263 while ((tuplehash = rhashtable_walk_next(&hti))) {
264 if (IS_ERR(tuplehash)) {
265 if (PTR_ERR(tuplehash) != -EAGAIN) {
266 err = PTR_ERR(tuplehash);
271 if (tuplehash->tuple.dir)
274 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
278 rhashtable_walk_stop(&hti);
279 rhashtable_walk_exit(&hti);
284 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
286 return (__s32)(flow->timeout - (u32)jiffies) <= 0;
289 static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
291 struct nf_flowtable *flow_table = data;
293 if (nf_flow_has_expired(flow) ||
294 (flow->flags & (FLOW_OFFLOAD_DYING | FLOW_OFFLOAD_TEARDOWN)))
295 flow_offload_del(flow_table, flow);
298 static void nf_flow_offload_work_gc(struct work_struct *work)
300 struct nf_flowtable *flow_table;
302 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
303 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
304 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
307 static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
308 __be16 port, __be16 new_port)
312 if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
313 skb_try_make_writable(skb, thoff + sizeof(*tcph)))
316 tcph = (void *)(skb_network_header(skb) + thoff);
317 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, true);
322 static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
323 __be16 port, __be16 new_port)
327 if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
328 skb_try_make_writable(skb, thoff + sizeof(*udph)))
331 udph = (void *)(skb_network_header(skb) + thoff);
332 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
333 inet_proto_csum_replace2(&udph->check, skb, port,
336 udph->check = CSUM_MANGLED_0;
342 static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
343 u8 protocol, __be16 port, __be16 new_port)
347 if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
351 if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
359 int nf_flow_snat_port(const struct flow_offload *flow,
360 struct sk_buff *skb, unsigned int thoff,
361 u8 protocol, enum flow_offload_tuple_dir dir)
363 struct flow_ports *hdr;
364 __be16 port, new_port;
366 if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
367 skb_try_make_writable(skb, thoff + sizeof(*hdr)))
370 hdr = (void *)(skb_network_header(skb) + thoff);
373 case FLOW_OFFLOAD_DIR_ORIGINAL:
375 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
376 hdr->source = new_port;
378 case FLOW_OFFLOAD_DIR_REPLY:
380 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
381 hdr->dest = new_port;
387 return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
389 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
391 int nf_flow_dnat_port(const struct flow_offload *flow,
392 struct sk_buff *skb, unsigned int thoff,
393 u8 protocol, enum flow_offload_tuple_dir dir)
395 struct flow_ports *hdr;
396 __be16 port, new_port;
398 if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
399 skb_try_make_writable(skb, thoff + sizeof(*hdr)))
402 hdr = (void *)(skb_network_header(skb) + thoff);
405 case FLOW_OFFLOAD_DIR_ORIGINAL:
407 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
408 hdr->dest = new_port;
410 case FLOW_OFFLOAD_DIR_REPLY:
412 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
413 hdr->source = new_port;
419 return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
421 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
423 int nf_flow_table_init(struct nf_flowtable *flowtable)
427 INIT_DEFERRABLE_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
429 err = rhashtable_init(&flowtable->rhashtable,
430 &nf_flow_offload_rhash_params);
434 queue_delayed_work(system_power_efficient_wq,
435 &flowtable->gc_work, HZ);
437 mutex_lock(&flowtable_lock);
438 list_add(&flowtable->list, &flowtables);
439 mutex_unlock(&flowtable_lock);
443 EXPORT_SYMBOL_GPL(nf_flow_table_init);
445 static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
447 struct net_device *dev = data;
448 struct flow_offload_entry *e;
450 e = container_of(flow, struct flow_offload_entry, flow);
453 flow_offload_teardown(flow);
456 if (net_eq(nf_ct_net(e->ct), dev_net(dev)) &&
457 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
458 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
459 flow_offload_dead(flow);
462 static void nf_flow_table_iterate_cleanup(struct nf_flowtable *flowtable,
463 struct net_device *dev)
465 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
466 flush_delayed_work(&flowtable->gc_work);
469 void nf_flow_table_cleanup(struct net_device *dev)
471 struct nf_flowtable *flowtable;
473 mutex_lock(&flowtable_lock);
474 list_for_each_entry(flowtable, &flowtables, list)
475 nf_flow_table_iterate_cleanup(flowtable, dev);
476 mutex_unlock(&flowtable_lock);
478 EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
480 void nf_flow_table_free(struct nf_flowtable *flow_table)
482 mutex_lock(&flowtable_lock);
483 list_del(&flow_table->list);
484 mutex_unlock(&flowtable_lock);
485 cancel_delayed_work_sync(&flow_table->gc_work);
486 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
487 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
488 rhashtable_destroy(&flow_table->rhashtable);
490 EXPORT_SYMBOL_GPL(nf_flow_table_free);
492 MODULE_LICENSE("GPL");
493 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");