1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/netfilter.h>
6 #include <linux/rhashtable.h>
7 #include <linux/netdevice.h>
9 #include <net/ip6_route.h>
10 #include <net/netfilter/nf_tables.h>
11 #include <net/netfilter/nf_flow_table.h>
12 #include <net/netfilter/nf_conntrack.h>
13 #include <net/netfilter/nf_conntrack_core.h>
14 #include <net/netfilter/nf_conntrack_l4proto.h>
15 #include <net/netfilter/nf_conntrack_tuple.h>
17 static DEFINE_MUTEX(flowtable_lock);
18 static LIST_HEAD(flowtables);
21 flow_offload_fill_dir(struct flow_offload *flow,
22 enum flow_offload_tuple_dir dir)
24 struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
25 struct nf_conntrack_tuple *ctt = &flow->ct->tuplehash[dir].tuple;
29 switch (ctt->src.l3num) {
31 ft->src_v4 = ctt->src.u3.in;
32 ft->dst_v4 = ctt->dst.u3.in;
35 ft->src_v6 = ctt->src.u3.in6;
36 ft->dst_v6 = ctt->dst.u3.in6;
40 ft->l3proto = ctt->src.l3num;
41 ft->l4proto = ctt->dst.protonum;
42 ft->src_port = ctt->src.u.tcp.port;
43 ft->dst_port = ctt->dst.u.tcp.port;
46 struct flow_offload *flow_offload_alloc(struct nf_conn *ct)
48 struct flow_offload *flow;
50 if (unlikely(nf_ct_is_dying(ct) ||
51 !refcount_inc_not_zero(&ct->ct_general.use)))
54 flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
60 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
61 flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_REPLY);
63 if (ct->status & IPS_SRC_NAT)
64 __set_bit(NF_FLOW_SNAT, &flow->flags);
65 if (ct->status & IPS_DST_NAT)
66 __set_bit(NF_FLOW_DNAT, &flow->flags);
75 EXPORT_SYMBOL_GPL(flow_offload_alloc);
77 static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
79 const struct rt6_info *rt;
81 if (flow_tuple->l3proto == NFPROTO_IPV6) {
82 rt = (const struct rt6_info *)flow_tuple->dst_cache;
83 return rt6_get_cookie(rt);
89 static int flow_offload_fill_route(struct flow_offload *flow,
90 const struct nf_flow_route *route,
91 enum flow_offload_tuple_dir dir)
93 struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
94 struct dst_entry *dst = route->tuple[dir].dst;
97 switch (flow_tuple->l3proto) {
99 flow_tuple->mtu = ip_dst_mtu_maybe_forward(dst, true);
102 flow_tuple->mtu = ip6_dst_mtu_maybe_forward(dst, true);
106 flow_tuple->iifidx = route->tuple[dir].in.ifindex;
107 for (i = route->tuple[dir].in.num_encaps - 1; i >= 0; i--) {
108 flow_tuple->encap[j].id = route->tuple[dir].in.encap[i].id;
109 flow_tuple->encap[j].proto = route->tuple[dir].in.encap[i].proto;
110 if (route->tuple[dir].in.ingress_vlans & BIT(i))
111 flow_tuple->in_vlan_ingress |= BIT(j);
114 flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
116 switch (route->tuple[dir].xmit_type) {
117 case FLOW_OFFLOAD_XMIT_DIRECT:
118 memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,
120 memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
122 flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
123 flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
125 case FLOW_OFFLOAD_XMIT_XFRM:
126 case FLOW_OFFLOAD_XMIT_NEIGH:
127 if (!dst_hold_safe(route->tuple[dir].dst))
130 flow_tuple->dst_cache = dst;
131 flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
137 flow_tuple->xmit_type = route->tuple[dir].xmit_type;
142 static void nft_flow_dst_release(struct flow_offload *flow,
143 enum flow_offload_tuple_dir dir)
145 if (flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
146 flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)
147 dst_release(flow->tuplehash[dir].tuple.dst_cache);
150 int flow_offload_route_init(struct flow_offload *flow,
151 const struct nf_flow_route *route)
155 err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
159 err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
161 goto err_route_reply;
163 flow->type = NF_FLOW_OFFLOAD_ROUTE;
168 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
172 EXPORT_SYMBOL_GPL(flow_offload_route_init);
174 static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
176 tcp->seen[0].td_maxwin = 0;
177 tcp->seen[1].td_maxwin = 0;
180 static void flow_offload_fixup_ct(struct nf_conn *ct)
182 struct net *net = nf_ct_net(ct);
183 int l4num = nf_ct_protonum(ct);
186 if (l4num == IPPROTO_TCP) {
187 struct nf_tcp_net *tn = nf_tcp_pernet(net);
189 flow_offload_fixup_tcp(&ct->proto.tcp);
191 timeout = tn->timeouts[ct->proto.tcp.state];
192 timeout -= tn->offload_timeout;
193 } else if (l4num == IPPROTO_UDP) {
194 struct nf_udp_net *tn = nf_udp_pernet(net);
196 timeout = tn->timeouts[UDP_CT_REPLIED];
197 timeout -= tn->offload_timeout;
205 if (nf_flow_timeout_delta(READ_ONCE(ct->timeout)) > (__s32)timeout)
206 WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);
209 static void flow_offload_route_release(struct flow_offload *flow)
211 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
212 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
215 void flow_offload_free(struct flow_offload *flow)
217 switch (flow->type) {
218 case NF_FLOW_OFFLOAD_ROUTE:
219 flow_offload_route_release(flow);
225 kfree_rcu(flow, rcu_head);
227 EXPORT_SYMBOL_GPL(flow_offload_free);
229 static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
231 const struct flow_offload_tuple *tuple = data;
233 return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
236 static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
238 const struct flow_offload_tuple_rhash *tuplehash = data;
240 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
243 static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
246 const struct flow_offload_tuple *tuple = arg->key;
247 const struct flow_offload_tuple_rhash *x = ptr;
249 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
255 static const struct rhashtable_params nf_flow_offload_rhash_params = {
256 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
257 .hashfn = flow_offload_hash,
258 .obj_hashfn = flow_offload_hash_obj,
259 .obj_cmpfn = flow_offload_hash_cmp,
260 .automatic_shrinking = true,
263 unsigned long flow_offload_get_timeout(struct flow_offload *flow)
265 unsigned long timeout = NF_FLOW_TIMEOUT;
266 struct net *net = nf_ct_net(flow->ct);
267 int l4num = nf_ct_protonum(flow->ct);
269 if (l4num == IPPROTO_TCP) {
270 struct nf_tcp_net *tn = nf_tcp_pernet(net);
272 timeout = tn->offload_timeout;
273 } else if (l4num == IPPROTO_UDP) {
274 struct nf_udp_net *tn = nf_udp_pernet(net);
276 timeout = tn->offload_timeout;
282 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
286 flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
288 err = rhashtable_insert_fast(&flow_table->rhashtable,
289 &flow->tuplehash[0].node,
290 nf_flow_offload_rhash_params);
294 err = rhashtable_insert_fast(&flow_table->rhashtable,
295 &flow->tuplehash[1].node,
296 nf_flow_offload_rhash_params);
298 rhashtable_remove_fast(&flow_table->rhashtable,
299 &flow->tuplehash[0].node,
300 nf_flow_offload_rhash_params);
304 nf_ct_offload_timeout(flow->ct);
306 if (nf_flowtable_hw_offload(flow_table)) {
307 __set_bit(NF_FLOW_HW, &flow->flags);
308 nf_flow_offload_add(flow_table, flow);
313 EXPORT_SYMBOL_GPL(flow_offload_add);
315 void flow_offload_refresh(struct nf_flowtable *flow_table,
316 struct flow_offload *flow)
320 timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
321 if (timeout - READ_ONCE(flow->timeout) > HZ)
322 WRITE_ONCE(flow->timeout, timeout);
326 if (likely(!nf_flowtable_hw_offload(flow_table)))
329 nf_flow_offload_add(flow_table, flow);
331 EXPORT_SYMBOL_GPL(flow_offload_refresh);
333 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
335 return nf_flow_timeout_delta(flow->timeout) <= 0;
338 static void flow_offload_del(struct nf_flowtable *flow_table,
339 struct flow_offload *flow)
341 rhashtable_remove_fast(&flow_table->rhashtable,
342 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
343 nf_flow_offload_rhash_params);
344 rhashtable_remove_fast(&flow_table->rhashtable,
345 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
346 nf_flow_offload_rhash_params);
347 flow_offload_free(flow);
350 void flow_offload_teardown(struct flow_offload *flow)
352 clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
353 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
354 flow_offload_fixup_ct(flow->ct);
356 EXPORT_SYMBOL_GPL(flow_offload_teardown);
358 struct flow_offload_tuple_rhash *
359 flow_offload_lookup(struct nf_flowtable *flow_table,
360 struct flow_offload_tuple *tuple)
362 struct flow_offload_tuple_rhash *tuplehash;
363 struct flow_offload *flow;
366 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
367 nf_flow_offload_rhash_params);
371 dir = tuplehash->tuple.dir;
372 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
373 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
376 if (unlikely(nf_ct_is_dying(flow->ct)))
381 EXPORT_SYMBOL_GPL(flow_offload_lookup);
384 nf_flow_table_iterate(struct nf_flowtable *flow_table,
385 void (*iter)(struct nf_flowtable *flowtable,
386 struct flow_offload *flow, void *data),
389 struct flow_offload_tuple_rhash *tuplehash;
390 struct rhashtable_iter hti;
391 struct flow_offload *flow;
394 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
395 rhashtable_walk_start(&hti);
397 while ((tuplehash = rhashtable_walk_next(&hti))) {
398 if (IS_ERR(tuplehash)) {
399 if (PTR_ERR(tuplehash) != -EAGAIN) {
400 err = PTR_ERR(tuplehash);
405 if (tuplehash->tuple.dir)
408 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
410 iter(flow_table, flow, data);
412 rhashtable_walk_stop(&hti);
413 rhashtable_walk_exit(&hti);
418 static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,
419 struct flow_offload *flow, void *data)
421 if (nf_flow_has_expired(flow) ||
422 nf_ct_is_dying(flow->ct))
423 flow_offload_teardown(flow);
425 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
426 if (test_bit(NF_FLOW_HW, &flow->flags)) {
427 if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
428 nf_flow_offload_del(flow_table, flow);
429 else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
430 flow_offload_del(flow_table, flow);
432 flow_offload_del(flow_table, flow);
434 } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
435 nf_flow_offload_stats(flow_table, flow);
439 void nf_flow_table_gc_run(struct nf_flowtable *flow_table)
441 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, NULL);
444 static void nf_flow_offload_work_gc(struct work_struct *work)
446 struct nf_flowtable *flow_table;
448 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
449 nf_flow_table_gc_run(flow_table);
450 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
453 static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
454 __be16 port, __be16 new_port)
458 tcph = (void *)(skb_network_header(skb) + thoff);
459 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
462 static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
463 __be16 port, __be16 new_port)
467 udph = (void *)(skb_network_header(skb) + thoff);
468 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
469 inet_proto_csum_replace2(&udph->check, skb, port,
472 udph->check = CSUM_MANGLED_0;
476 static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
477 u8 protocol, __be16 port, __be16 new_port)
481 nf_flow_nat_port_tcp(skb, thoff, port, new_port);
484 nf_flow_nat_port_udp(skb, thoff, port, new_port);
489 void nf_flow_snat_port(const struct flow_offload *flow,
490 struct sk_buff *skb, unsigned int thoff,
491 u8 protocol, enum flow_offload_tuple_dir dir)
493 struct flow_ports *hdr;
494 __be16 port, new_port;
496 hdr = (void *)(skb_network_header(skb) + thoff);
499 case FLOW_OFFLOAD_DIR_ORIGINAL:
501 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
502 hdr->source = new_port;
504 case FLOW_OFFLOAD_DIR_REPLY:
506 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
507 hdr->dest = new_port;
511 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
513 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
515 void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
516 unsigned int thoff, u8 protocol,
517 enum flow_offload_tuple_dir dir)
519 struct flow_ports *hdr;
520 __be16 port, new_port;
522 hdr = (void *)(skb_network_header(skb) + thoff);
525 case FLOW_OFFLOAD_DIR_ORIGINAL:
527 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
528 hdr->dest = new_port;
530 case FLOW_OFFLOAD_DIR_REPLY:
532 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
533 hdr->source = new_port;
537 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
539 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
541 int nf_flow_table_init(struct nf_flowtable *flowtable)
545 INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
546 flow_block_init(&flowtable->flow_block);
547 init_rwsem(&flowtable->flow_block_lock);
549 err = rhashtable_init(&flowtable->rhashtable,
550 &nf_flow_offload_rhash_params);
554 queue_delayed_work(system_power_efficient_wq,
555 &flowtable->gc_work, HZ);
557 mutex_lock(&flowtable_lock);
558 list_add(&flowtable->list, &flowtables);
559 mutex_unlock(&flowtable_lock);
563 EXPORT_SYMBOL_GPL(nf_flow_table_init);
565 static void nf_flow_table_do_cleanup(struct nf_flowtable *flow_table,
566 struct flow_offload *flow, void *data)
568 struct net_device *dev = data;
571 flow_offload_teardown(flow);
575 if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
576 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
577 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
578 flow_offload_teardown(flow);
581 void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
582 struct net_device *dev)
584 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
585 flush_delayed_work(&flowtable->gc_work);
586 nf_flow_table_offload_flush(flowtable);
589 void nf_flow_table_cleanup(struct net_device *dev)
591 struct nf_flowtable *flowtable;
593 mutex_lock(&flowtable_lock);
594 list_for_each_entry(flowtable, &flowtables, list)
595 nf_flow_table_gc_cleanup(flowtable, dev);
596 mutex_unlock(&flowtable_lock);
598 EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
600 void nf_flow_table_free(struct nf_flowtable *flow_table)
602 mutex_lock(&flowtable_lock);
603 list_del(&flow_table->list);
604 mutex_unlock(&flowtable_lock);
606 cancel_delayed_work_sync(&flow_table->gc_work);
607 nf_flow_table_offload_flush(flow_table);
608 /* ... no more pending work after this stage ... */
609 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
610 nf_flow_table_gc_run(flow_table);
611 nf_flow_table_offload_flush_cleanup(flow_table);
612 rhashtable_destroy(&flow_table->rhashtable);
614 EXPORT_SYMBOL_GPL(nf_flow_table_free);
616 static int __init nf_flow_table_module_init(void)
618 return nf_flow_table_offload_init();
621 static void __exit nf_flow_table_module_exit(void)
623 nf_flow_table_offload_exit();
626 module_init(nf_flow_table_module_init);
627 module_exit(nf_flow_table_module_exit);
629 MODULE_LICENSE("GPL");
630 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
631 MODULE_DESCRIPTION("Netfilter flow table module");