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 !atomic_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_forward(dst);
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->state = TCP_CONNTRACK_ESTABLISHED;
177 tcp->seen[0].td_maxwin = 0;
178 tcp->seen[1].td_maxwin = 0;
181 static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
183 const struct nf_conntrack_l4proto *l4proto;
184 struct net *net = nf_ct_net(ct);
185 int l4num = nf_ct_protonum(ct);
186 unsigned int timeout;
188 l4proto = nf_ct_l4proto_find(l4num);
192 if (l4num == IPPROTO_TCP) {
193 struct nf_tcp_net *tn = nf_tcp_pernet(net);
195 timeout = tn->offload_pickup;
196 } else if (l4num == IPPROTO_UDP) {
197 struct nf_udp_net *tn = nf_udp_pernet(net);
199 timeout = tn->offload_pickup;
204 if (nf_flow_timeout_delta(ct->timeout) > (__s32)timeout)
205 ct->timeout = nfct_time_stamp + timeout;
208 static void flow_offload_fixup_ct_state(struct nf_conn *ct)
210 if (nf_ct_protonum(ct) == IPPROTO_TCP)
211 flow_offload_fixup_tcp(&ct->proto.tcp);
214 static void flow_offload_fixup_ct(struct nf_conn *ct)
216 flow_offload_fixup_ct_state(ct);
217 flow_offload_fixup_ct_timeout(ct);
220 static void flow_offload_route_release(struct flow_offload *flow)
222 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
223 nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
226 void flow_offload_free(struct flow_offload *flow)
228 switch (flow->type) {
229 case NF_FLOW_OFFLOAD_ROUTE:
230 flow_offload_route_release(flow);
236 kfree_rcu(flow, rcu_head);
238 EXPORT_SYMBOL_GPL(flow_offload_free);
240 static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
242 const struct flow_offload_tuple *tuple = data;
244 return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
247 static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
249 const struct flow_offload_tuple_rhash *tuplehash = data;
251 return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
254 static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
257 const struct flow_offload_tuple *tuple = arg->key;
258 const struct flow_offload_tuple_rhash *x = ptr;
260 if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
266 static const struct rhashtable_params nf_flow_offload_rhash_params = {
267 .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
268 .hashfn = flow_offload_hash,
269 .obj_hashfn = flow_offload_hash_obj,
270 .obj_cmpfn = flow_offload_hash_cmp,
271 .automatic_shrinking = true,
274 unsigned long flow_offload_get_timeout(struct flow_offload *flow)
276 const struct nf_conntrack_l4proto *l4proto;
277 unsigned long timeout = NF_FLOW_TIMEOUT;
278 struct net *net = nf_ct_net(flow->ct);
279 int l4num = nf_ct_protonum(flow->ct);
281 l4proto = nf_ct_l4proto_find(l4num);
285 if (l4num == IPPROTO_TCP) {
286 struct nf_tcp_net *tn = nf_tcp_pernet(net);
288 timeout = tn->offload_timeout;
289 } else if (l4num == IPPROTO_UDP) {
290 struct nf_udp_net *tn = nf_udp_pernet(net);
292 timeout = tn->offload_timeout;
298 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
302 flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
304 err = rhashtable_insert_fast(&flow_table->rhashtable,
305 &flow->tuplehash[0].node,
306 nf_flow_offload_rhash_params);
310 err = rhashtable_insert_fast(&flow_table->rhashtable,
311 &flow->tuplehash[1].node,
312 nf_flow_offload_rhash_params);
314 rhashtable_remove_fast(&flow_table->rhashtable,
315 &flow->tuplehash[0].node,
316 nf_flow_offload_rhash_params);
320 nf_ct_offload_timeout(flow->ct);
322 if (nf_flowtable_hw_offload(flow_table)) {
323 __set_bit(NF_FLOW_HW, &flow->flags);
324 nf_flow_offload_add(flow_table, flow);
329 EXPORT_SYMBOL_GPL(flow_offload_add);
331 void flow_offload_refresh(struct nf_flowtable *flow_table,
332 struct flow_offload *flow)
334 flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
336 if (likely(!nf_flowtable_hw_offload(flow_table)))
339 nf_flow_offload_add(flow_table, flow);
341 EXPORT_SYMBOL_GPL(flow_offload_refresh);
343 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
345 return nf_flow_timeout_delta(flow->timeout) <= 0;
348 static void flow_offload_del(struct nf_flowtable *flow_table,
349 struct flow_offload *flow)
351 rhashtable_remove_fast(&flow_table->rhashtable,
352 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
353 nf_flow_offload_rhash_params);
354 rhashtable_remove_fast(&flow_table->rhashtable,
355 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
356 nf_flow_offload_rhash_params);
358 clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
360 if (nf_flow_has_expired(flow))
361 flow_offload_fixup_ct(flow->ct);
363 flow_offload_fixup_ct_timeout(flow->ct);
365 flow_offload_free(flow);
368 void flow_offload_teardown(struct flow_offload *flow)
370 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
372 flow_offload_fixup_ct_state(flow->ct);
374 EXPORT_SYMBOL_GPL(flow_offload_teardown);
376 struct flow_offload_tuple_rhash *
377 flow_offload_lookup(struct nf_flowtable *flow_table,
378 struct flow_offload_tuple *tuple)
380 struct flow_offload_tuple_rhash *tuplehash;
381 struct flow_offload *flow;
384 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
385 nf_flow_offload_rhash_params);
389 dir = tuplehash->tuple.dir;
390 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
391 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
394 if (unlikely(nf_ct_is_dying(flow->ct)))
399 EXPORT_SYMBOL_GPL(flow_offload_lookup);
402 nf_flow_table_iterate(struct nf_flowtable *flow_table,
403 void (*iter)(struct flow_offload *flow, void *data),
406 struct flow_offload_tuple_rhash *tuplehash;
407 struct rhashtable_iter hti;
408 struct flow_offload *flow;
411 rhashtable_walk_enter(&flow_table->rhashtable, &hti);
412 rhashtable_walk_start(&hti);
414 while ((tuplehash = rhashtable_walk_next(&hti))) {
415 if (IS_ERR(tuplehash)) {
416 if (PTR_ERR(tuplehash) != -EAGAIN) {
417 err = PTR_ERR(tuplehash);
422 if (tuplehash->tuple.dir)
425 flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
429 rhashtable_walk_stop(&hti);
430 rhashtable_walk_exit(&hti);
435 static bool flow_offload_stale_dst(struct flow_offload_tuple *tuple)
437 struct dst_entry *dst;
439 if (tuple->xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
440 tuple->xmit_type == FLOW_OFFLOAD_XMIT_XFRM) {
441 dst = tuple->dst_cache;
442 if (!dst_check(dst, tuple->dst_cookie))
449 static bool nf_flow_has_stale_dst(struct flow_offload *flow)
451 return flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
452 flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple);
455 static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
457 struct nf_flowtable *flow_table = data;
459 if (nf_flow_has_expired(flow) ||
460 nf_ct_is_dying(flow->ct) ||
461 nf_flow_has_stale_dst(flow))
462 set_bit(NF_FLOW_TEARDOWN, &flow->flags);
464 if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
465 if (test_bit(NF_FLOW_HW, &flow->flags)) {
466 if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
467 nf_flow_offload_del(flow_table, flow);
468 else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
469 flow_offload_del(flow_table, flow);
471 flow_offload_del(flow_table, flow);
473 } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
474 nf_flow_offload_stats(flow_table, flow);
478 static void nf_flow_offload_work_gc(struct work_struct *work)
480 struct nf_flowtable *flow_table;
482 flow_table = container_of(work, struct nf_flowtable, gc_work.work);
483 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
484 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
487 static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
488 __be16 port, __be16 new_port)
492 tcph = (void *)(skb_network_header(skb) + thoff);
493 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
496 static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
497 __be16 port, __be16 new_port)
501 udph = (void *)(skb_network_header(skb) + thoff);
502 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
503 inet_proto_csum_replace2(&udph->check, skb, port,
506 udph->check = CSUM_MANGLED_0;
510 static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
511 u8 protocol, __be16 port, __be16 new_port)
515 nf_flow_nat_port_tcp(skb, thoff, port, new_port);
518 nf_flow_nat_port_udp(skb, thoff, port, new_port);
523 void nf_flow_snat_port(const struct flow_offload *flow,
524 struct sk_buff *skb, unsigned int thoff,
525 u8 protocol, enum flow_offload_tuple_dir dir)
527 struct flow_ports *hdr;
528 __be16 port, new_port;
530 hdr = (void *)(skb_network_header(skb) + thoff);
533 case FLOW_OFFLOAD_DIR_ORIGINAL:
535 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
536 hdr->source = new_port;
538 case FLOW_OFFLOAD_DIR_REPLY:
540 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
541 hdr->dest = new_port;
545 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
547 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
549 void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
550 unsigned int thoff, u8 protocol,
551 enum flow_offload_tuple_dir dir)
553 struct flow_ports *hdr;
554 __be16 port, new_port;
556 hdr = (void *)(skb_network_header(skb) + thoff);
559 case FLOW_OFFLOAD_DIR_ORIGINAL:
561 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
562 hdr->dest = new_port;
564 case FLOW_OFFLOAD_DIR_REPLY:
566 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
567 hdr->source = new_port;
571 nf_flow_nat_port(skb, thoff, protocol, port, new_port);
573 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
575 int nf_flow_table_init(struct nf_flowtable *flowtable)
579 INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
580 flow_block_init(&flowtable->flow_block);
581 init_rwsem(&flowtable->flow_block_lock);
583 err = rhashtable_init(&flowtable->rhashtable,
584 &nf_flow_offload_rhash_params);
588 queue_delayed_work(system_power_efficient_wq,
589 &flowtable->gc_work, HZ);
591 mutex_lock(&flowtable_lock);
592 list_add(&flowtable->list, &flowtables);
593 mutex_unlock(&flowtable_lock);
597 EXPORT_SYMBOL_GPL(nf_flow_table_init);
599 static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
601 struct net_device *dev = data;
604 flow_offload_teardown(flow);
608 if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
609 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
610 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
611 flow_offload_teardown(flow);
614 void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
615 struct net_device *dev)
617 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
618 flush_delayed_work(&flowtable->gc_work);
619 nf_flow_table_offload_flush(flowtable);
622 void nf_flow_table_cleanup(struct net_device *dev)
624 struct nf_flowtable *flowtable;
626 mutex_lock(&flowtable_lock);
627 list_for_each_entry(flowtable, &flowtables, list)
628 nf_flow_table_gc_cleanup(flowtable, dev);
629 mutex_unlock(&flowtable_lock);
631 EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
633 void nf_flow_table_free(struct nf_flowtable *flow_table)
635 mutex_lock(&flowtable_lock);
636 list_del(&flow_table->list);
637 mutex_unlock(&flowtable_lock);
639 cancel_delayed_work_sync(&flow_table->gc_work);
640 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
641 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
642 nf_flow_table_offload_flush(flow_table);
643 if (nf_flowtable_hw_offload(flow_table))
644 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step,
646 rhashtable_destroy(&flow_table->rhashtable);
648 EXPORT_SYMBOL_GPL(nf_flow_table_free);
650 static int __init nf_flow_table_module_init(void)
652 return nf_flow_table_offload_init();
655 static void __exit nf_flow_table_module_exit(void)
657 nf_flow_table_offload_exit();
660 module_init(nf_flow_table_module_init);
661 module_exit(nf_flow_table_module_exit);
663 MODULE_LICENSE("GPL");
664 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
665 MODULE_DESCRIPTION("Netfilter flow table module");