1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Fri, 16 Feb 2018 11:08:47 +0100
3 Subject: [PATCH] netfilter: nf_flow_table: rename nf_flow_table.c to
6 Preparation for adding more code to the same module
8 Signed-off-by: Felix Fietkau <nbd@nbd.name>
10 rename net/netfilter/{nf_flow_table.c => nf_flow_table_core.c} (100%)
12 --- a/net/netfilter/Makefile
13 +++ b/net/netfilter/Makefile
14 @@ -113,6 +113,8 @@ obj-$(CONFIG_NFT_FWD_NETDEV) += nft_fwd_
16 # flow table infrastructure
17 obj-$(CONFIG_NF_FLOW_TABLE) += nf_flow_table.o
18 +nf_flow_table-objs := nf_flow_table_core.o
20 obj-$(CONFIG_NF_FLOW_TABLE_INET) += nf_flow_table_inet.o
23 --- a/net/netfilter/nf_flow_table.c
26 -#include <linux/kernel.h>
27 -#include <linux/init.h>
28 -#include <linux/module.h>
29 -#include <linux/netfilter.h>
30 -#include <linux/rhashtable.h>
31 -#include <linux/netdevice.h>
33 -#include <net/ip6_route.h>
34 -#include <net/netfilter/nf_tables.h>
35 -#include <net/netfilter/nf_flow_table.h>
36 -#include <net/netfilter/nf_conntrack.h>
37 -#include <net/netfilter/nf_conntrack_core.h>
38 -#include <net/netfilter/nf_conntrack_tuple.h>
40 -struct flow_offload_entry {
41 - struct flow_offload flow;
43 - struct rcu_head rcu_head;
47 -flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
48 - struct nf_flow_route *route,
49 - enum flow_offload_tuple_dir dir)
51 - struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
52 - struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
53 - struct dst_entry *dst = route->tuple[dir].dst;
57 - switch (ctt->src.l3num) {
59 - ft->src_v4 = ctt->src.u3.in;
60 - ft->dst_v4 = ctt->dst.u3.in;
61 - ft->mtu = ip_dst_mtu_maybe_forward(dst, true);
64 - ft->src_v6 = ctt->src.u3.in6;
65 - ft->dst_v6 = ctt->dst.u3.in6;
66 - ft->mtu = ip6_dst_mtu_forward(dst);
70 - ft->l3proto = ctt->src.l3num;
71 - ft->l4proto = ctt->dst.protonum;
72 - ft->src_port = ctt->src.u.tcp.port;
73 - ft->dst_port = ctt->dst.u.tcp.port;
75 - ft->iifidx = route->tuple[dir].ifindex;
76 - ft->oifidx = route->tuple[!dir].ifindex;
77 - ft->dst_cache = dst;
80 -struct flow_offload *
81 -flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
83 - struct flow_offload_entry *entry;
84 - struct flow_offload *flow;
86 - if (unlikely(nf_ct_is_dying(ct) ||
87 - !atomic_inc_not_zero(&ct->ct_general.use)))
90 - entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
94 - flow = &entry->flow;
96 - if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst))
97 - goto err_dst_cache_original;
99 - if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst))
100 - goto err_dst_cache_reply;
104 - flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
105 - flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
107 - if (ct->status & IPS_SRC_NAT)
108 - flow->flags |= FLOW_OFFLOAD_SNAT;
109 - else if (ct->status & IPS_DST_NAT)
110 - flow->flags |= FLOW_OFFLOAD_DNAT;
114 -err_dst_cache_reply:
115 - dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
116 -err_dst_cache_original:
123 -EXPORT_SYMBOL_GPL(flow_offload_alloc);
125 -void flow_offload_free(struct flow_offload *flow)
127 - struct flow_offload_entry *e;
129 - dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
130 - dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
131 - e = container_of(flow, struct flow_offload_entry, flow);
132 - nf_ct_delete(e->ct, 0, 0);
134 - kfree_rcu(e, rcu_head);
136 -EXPORT_SYMBOL_GPL(flow_offload_free);
138 -void flow_offload_dead(struct flow_offload *flow)
140 - flow->flags |= FLOW_OFFLOAD_DYING;
142 -EXPORT_SYMBOL_GPL(flow_offload_dead);
144 -int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
146 - flow->timeout = (u32)jiffies;
148 - rhashtable_insert_fast(&flow_table->rhashtable,
149 - &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
150 - *flow_table->type->params);
151 - rhashtable_insert_fast(&flow_table->rhashtable,
152 - &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
153 - *flow_table->type->params);
156 -EXPORT_SYMBOL_GPL(flow_offload_add);
158 -static void flow_offload_del(struct nf_flowtable *flow_table,
159 - struct flow_offload *flow)
161 - rhashtable_remove_fast(&flow_table->rhashtable,
162 - &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
163 - *flow_table->type->params);
164 - rhashtable_remove_fast(&flow_table->rhashtable,
165 - &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
166 - *flow_table->type->params);
168 - flow_offload_free(flow);
171 -struct flow_offload_tuple_rhash *
172 -flow_offload_lookup(struct nf_flowtable *flow_table,
173 - struct flow_offload_tuple *tuple)
175 - return rhashtable_lookup_fast(&flow_table->rhashtable, tuple,
176 - *flow_table->type->params);
178 -EXPORT_SYMBOL_GPL(flow_offload_lookup);
180 -int nf_flow_table_iterate(struct nf_flowtable *flow_table,
181 - void (*iter)(struct flow_offload *flow, void *data),
184 - struct flow_offload_tuple_rhash *tuplehash;
185 - struct rhashtable_iter hti;
186 - struct flow_offload *flow;
189 - err = rhashtable_walk_init(&flow_table->rhashtable, &hti, GFP_KERNEL);
193 - rhashtable_walk_start(&hti);
195 - while ((tuplehash = rhashtable_walk_next(&hti))) {
196 - if (IS_ERR(tuplehash)) {
197 - err = PTR_ERR(tuplehash);
198 - if (err != -EAGAIN)
203 - if (tuplehash->tuple.dir)
206 - flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
211 - rhashtable_walk_stop(&hti);
212 - rhashtable_walk_exit(&hti);
216 -EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
218 -static inline bool nf_flow_has_expired(const struct flow_offload *flow)
220 - return (__s32)(flow->timeout - (u32)jiffies) <= 0;
223 -static inline bool nf_flow_is_dying(const struct flow_offload *flow)
225 - return flow->flags & FLOW_OFFLOAD_DYING;
228 -static int nf_flow_offload_gc_step(struct nf_flowtable *flow_table)
230 - struct flow_offload_tuple_rhash *tuplehash;
231 - struct rhashtable_iter hti;
232 - struct flow_offload *flow;
235 - err = rhashtable_walk_init(&flow_table->rhashtable, &hti, GFP_KERNEL);
239 - rhashtable_walk_start(&hti);
241 - while ((tuplehash = rhashtable_walk_next(&hti))) {
242 - if (IS_ERR(tuplehash)) {
243 - err = PTR_ERR(tuplehash);
244 - if (err != -EAGAIN)
249 - if (tuplehash->tuple.dir)
252 - flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
254 - if (nf_flow_has_expired(flow) ||
255 - nf_flow_is_dying(flow))
256 - flow_offload_del(flow_table, flow);
259 - rhashtable_walk_stop(&hti);
260 - rhashtable_walk_exit(&hti);
265 -void nf_flow_offload_work_gc(struct work_struct *work)
267 - struct nf_flowtable *flow_table;
269 - flow_table = container_of(work, struct nf_flowtable, gc_work.work);
270 - nf_flow_offload_gc_step(flow_table);
271 - queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
273 -EXPORT_SYMBOL_GPL(nf_flow_offload_work_gc);
275 -static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
277 - const struct flow_offload_tuple *tuple = data;
279 - return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
282 -static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
284 - const struct flow_offload_tuple_rhash *tuplehash = data;
286 - return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
289 -static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
292 - const struct flow_offload_tuple *tuple = arg->key;
293 - const struct flow_offload_tuple_rhash *x = ptr;
295 - if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
301 -const struct rhashtable_params nf_flow_offload_rhash_params = {
302 - .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
303 - .hashfn = flow_offload_hash,
304 - .obj_hashfn = flow_offload_hash_obj,
305 - .obj_cmpfn = flow_offload_hash_cmp,
306 - .automatic_shrinking = true,
308 -EXPORT_SYMBOL_GPL(nf_flow_offload_rhash_params);
310 -static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
311 - __be16 port, __be16 new_port)
313 - struct tcphdr *tcph;
315 - if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
316 - skb_try_make_writable(skb, thoff + sizeof(*tcph)))
319 - tcph = (void *)(skb_network_header(skb) + thoff);
320 - inet_proto_csum_replace2(&tcph->check, skb, port, new_port, true);
325 -static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
326 - __be16 port, __be16 new_port)
328 - struct udphdr *udph;
330 - if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
331 - skb_try_make_writable(skb, thoff + sizeof(*udph)))
334 - udph = (void *)(skb_network_header(skb) + thoff);
335 - if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
336 - inet_proto_csum_replace2(&udph->check, skb, port,
339 - udph->check = CSUM_MANGLED_0;
345 -static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
346 - u8 protocol, __be16 port, __be16 new_port)
348 - switch (protocol) {
350 - if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
354 - if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
362 -int nf_flow_snat_port(const struct flow_offload *flow,
363 - struct sk_buff *skb, unsigned int thoff,
364 - u8 protocol, enum flow_offload_tuple_dir dir)
366 - struct flow_ports *hdr;
367 - __be16 port, new_port;
369 - if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
370 - skb_try_make_writable(skb, thoff + sizeof(*hdr)))
373 - hdr = (void *)(skb_network_header(skb) + thoff);
376 - case FLOW_OFFLOAD_DIR_ORIGINAL:
377 - port = hdr->source;
378 - new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
379 - hdr->source = new_port;
381 - case FLOW_OFFLOAD_DIR_REPLY:
383 - new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
384 - hdr->dest = new_port;
390 - return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
392 -EXPORT_SYMBOL_GPL(nf_flow_snat_port);
394 -int nf_flow_dnat_port(const struct flow_offload *flow,
395 - struct sk_buff *skb, unsigned int thoff,
396 - u8 protocol, enum flow_offload_tuple_dir dir)
398 - struct flow_ports *hdr;
399 - __be16 port, new_port;
401 - if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
402 - skb_try_make_writable(skb, thoff + sizeof(*hdr)))
405 - hdr = (void *)(skb_network_header(skb) + thoff);
408 - case FLOW_OFFLOAD_DIR_ORIGINAL:
410 - new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
411 - hdr->dest = new_port;
413 - case FLOW_OFFLOAD_DIR_REPLY:
414 - port = hdr->source;
415 - new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
416 - hdr->source = new_port;
422 - return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
424 -EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
426 -static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
428 - struct net_device *dev = data;
430 - if (dev && flow->tuplehash[0].tuple.iifidx != dev->ifindex)
433 - flow_offload_dead(flow);
436 -static void nf_flow_table_iterate_cleanup(struct nf_flowtable *flowtable,
439 - nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, data);
440 - flush_delayed_work(&flowtable->gc_work);
443 -void nf_flow_table_cleanup(struct net *net, struct net_device *dev)
445 - nft_flow_table_iterate(net, nf_flow_table_iterate_cleanup, dev);
447 -EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
449 -void nf_flow_table_free(struct nf_flowtable *flow_table)
451 - nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
452 - WARN_ON(!nf_flow_offload_gc_step(flow_table));
454 -EXPORT_SYMBOL_GPL(nf_flow_table_free);
456 -static int nf_flow_table_netdev_event(struct notifier_block *this,
457 - unsigned long event, void *ptr)
459 - struct net_device *dev = netdev_notifier_info_to_dev(ptr);
461 - if (event != NETDEV_DOWN)
462 - return NOTIFY_DONE;
464 - nf_flow_table_cleanup(dev_net(dev), dev);
466 - return NOTIFY_DONE;
469 -static struct notifier_block flow_offload_netdev_notifier = {
470 - .notifier_call = nf_flow_table_netdev_event,
473 -static int __init nf_flow_table_module_init(void)
475 - return register_netdevice_notifier(&flow_offload_netdev_notifier);
478 -static void __exit nf_flow_table_module_exit(void)
480 - unregister_netdevice_notifier(&flow_offload_netdev_notifier);
483 -module_init(nf_flow_table_module_init);
484 -module_exit(nf_flow_table_module_exit);
486 -MODULE_LICENSE("GPL");
487 -MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
489 +++ b/net/netfilter/nf_flow_table_core.c
491 +#include <linux/kernel.h>
492 +#include <linux/init.h>
493 +#include <linux/module.h>
494 +#include <linux/netfilter.h>
495 +#include <linux/rhashtable.h>
496 +#include <linux/netdevice.h>
498 +#include <net/ip6_route.h>
499 +#include <net/netfilter/nf_tables.h>
500 +#include <net/netfilter/nf_flow_table.h>
501 +#include <net/netfilter/nf_conntrack.h>
502 +#include <net/netfilter/nf_conntrack_core.h>
503 +#include <net/netfilter/nf_conntrack_tuple.h>
505 +struct flow_offload_entry {
506 + struct flow_offload flow;
507 + struct nf_conn *ct;
508 + struct rcu_head rcu_head;
512 +flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
513 + struct nf_flow_route *route,
514 + enum flow_offload_tuple_dir dir)
516 + struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
517 + struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
518 + struct dst_entry *dst = route->tuple[dir].dst;
522 + switch (ctt->src.l3num) {
524 + ft->src_v4 = ctt->src.u3.in;
525 + ft->dst_v4 = ctt->dst.u3.in;
526 + ft->mtu = ip_dst_mtu_maybe_forward(dst, true);
529 + ft->src_v6 = ctt->src.u3.in6;
530 + ft->dst_v6 = ctt->dst.u3.in6;
531 + ft->mtu = ip6_dst_mtu_forward(dst);
535 + ft->l3proto = ctt->src.l3num;
536 + ft->l4proto = ctt->dst.protonum;
537 + ft->src_port = ctt->src.u.tcp.port;
538 + ft->dst_port = ctt->dst.u.tcp.port;
540 + ft->iifidx = route->tuple[dir].ifindex;
541 + ft->oifidx = route->tuple[!dir].ifindex;
542 + ft->dst_cache = dst;
545 +struct flow_offload *
546 +flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
548 + struct flow_offload_entry *entry;
549 + struct flow_offload *flow;
551 + if (unlikely(nf_ct_is_dying(ct) ||
552 + !atomic_inc_not_zero(&ct->ct_general.use)))
555 + entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
557 + goto err_ct_refcnt;
559 + flow = &entry->flow;
561 + if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst))
562 + goto err_dst_cache_original;
564 + if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst))
565 + goto err_dst_cache_reply;
569 + flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
570 + flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
572 + if (ct->status & IPS_SRC_NAT)
573 + flow->flags |= FLOW_OFFLOAD_SNAT;
574 + else if (ct->status & IPS_DST_NAT)
575 + flow->flags |= FLOW_OFFLOAD_DNAT;
579 +err_dst_cache_reply:
580 + dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
581 +err_dst_cache_original:
588 +EXPORT_SYMBOL_GPL(flow_offload_alloc);
590 +void flow_offload_free(struct flow_offload *flow)
592 + struct flow_offload_entry *e;
594 + dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
595 + dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
596 + e = container_of(flow, struct flow_offload_entry, flow);
597 + nf_ct_delete(e->ct, 0, 0);
599 + kfree_rcu(e, rcu_head);
601 +EXPORT_SYMBOL_GPL(flow_offload_free);
603 +void flow_offload_dead(struct flow_offload *flow)
605 + flow->flags |= FLOW_OFFLOAD_DYING;
607 +EXPORT_SYMBOL_GPL(flow_offload_dead);
609 +int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
611 + flow->timeout = (u32)jiffies;
613 + rhashtable_insert_fast(&flow_table->rhashtable,
614 + &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
615 + *flow_table->type->params);
616 + rhashtable_insert_fast(&flow_table->rhashtable,
617 + &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
618 + *flow_table->type->params);
621 +EXPORT_SYMBOL_GPL(flow_offload_add);
623 +static void flow_offload_del(struct nf_flowtable *flow_table,
624 + struct flow_offload *flow)
626 + rhashtable_remove_fast(&flow_table->rhashtable,
627 + &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
628 + *flow_table->type->params);
629 + rhashtable_remove_fast(&flow_table->rhashtable,
630 + &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
631 + *flow_table->type->params);
633 + flow_offload_free(flow);
636 +struct flow_offload_tuple_rhash *
637 +flow_offload_lookup(struct nf_flowtable *flow_table,
638 + struct flow_offload_tuple *tuple)
640 + return rhashtable_lookup_fast(&flow_table->rhashtable, tuple,
641 + *flow_table->type->params);
643 +EXPORT_SYMBOL_GPL(flow_offload_lookup);
645 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
646 + void (*iter)(struct flow_offload *flow, void *data),
649 + struct flow_offload_tuple_rhash *tuplehash;
650 + struct rhashtable_iter hti;
651 + struct flow_offload *flow;
654 + err = rhashtable_walk_init(&flow_table->rhashtable, &hti, GFP_KERNEL);
658 + rhashtable_walk_start(&hti);
660 + while ((tuplehash = rhashtable_walk_next(&hti))) {
661 + if (IS_ERR(tuplehash)) {
662 + err = PTR_ERR(tuplehash);
663 + if (err != -EAGAIN)
668 + if (tuplehash->tuple.dir)
671 + flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
676 + rhashtable_walk_stop(&hti);
677 + rhashtable_walk_exit(&hti);
681 +EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
683 +static inline bool nf_flow_has_expired(const struct flow_offload *flow)
685 + return (__s32)(flow->timeout - (u32)jiffies) <= 0;
688 +static inline bool nf_flow_is_dying(const struct flow_offload *flow)
690 + return flow->flags & FLOW_OFFLOAD_DYING;
693 +static int nf_flow_offload_gc_step(struct nf_flowtable *flow_table)
695 + struct flow_offload_tuple_rhash *tuplehash;
696 + struct rhashtable_iter hti;
697 + struct flow_offload *flow;
700 + err = rhashtable_walk_init(&flow_table->rhashtable, &hti, GFP_KERNEL);
704 + rhashtable_walk_start(&hti);
706 + while ((tuplehash = rhashtable_walk_next(&hti))) {
707 + if (IS_ERR(tuplehash)) {
708 + err = PTR_ERR(tuplehash);
709 + if (err != -EAGAIN)
714 + if (tuplehash->tuple.dir)
717 + flow = container_of(tuplehash, struct flow_offload, tuplehash[0]);
719 + if (nf_flow_has_expired(flow) ||
720 + nf_flow_is_dying(flow))
721 + flow_offload_del(flow_table, flow);
724 + rhashtable_walk_stop(&hti);
725 + rhashtable_walk_exit(&hti);
730 +void nf_flow_offload_work_gc(struct work_struct *work)
732 + struct nf_flowtable *flow_table;
734 + flow_table = container_of(work, struct nf_flowtable, gc_work.work);
735 + nf_flow_offload_gc_step(flow_table);
736 + queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
738 +EXPORT_SYMBOL_GPL(nf_flow_offload_work_gc);
740 +static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
742 + const struct flow_offload_tuple *tuple = data;
744 + return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
747 +static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
749 + const struct flow_offload_tuple_rhash *tuplehash = data;
751 + return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
754 +static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
757 + const struct flow_offload_tuple *tuple = arg->key;
758 + const struct flow_offload_tuple_rhash *x = ptr;
760 + if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
766 +const struct rhashtable_params nf_flow_offload_rhash_params = {
767 + .head_offset = offsetof(struct flow_offload_tuple_rhash, node),
768 + .hashfn = flow_offload_hash,
769 + .obj_hashfn = flow_offload_hash_obj,
770 + .obj_cmpfn = flow_offload_hash_cmp,
771 + .automatic_shrinking = true,
773 +EXPORT_SYMBOL_GPL(nf_flow_offload_rhash_params);
775 +static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
776 + __be16 port, __be16 new_port)
778 + struct tcphdr *tcph;
780 + if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
781 + skb_try_make_writable(skb, thoff + sizeof(*tcph)))
784 + tcph = (void *)(skb_network_header(skb) + thoff);
785 + inet_proto_csum_replace2(&tcph->check, skb, port, new_port, true);
790 +static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
791 + __be16 port, __be16 new_port)
793 + struct udphdr *udph;
795 + if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
796 + skb_try_make_writable(skb, thoff + sizeof(*udph)))
799 + udph = (void *)(skb_network_header(skb) + thoff);
800 + if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
801 + inet_proto_csum_replace2(&udph->check, skb, port,
804 + udph->check = CSUM_MANGLED_0;
810 +static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
811 + u8 protocol, __be16 port, __be16 new_port)
813 + switch (protocol) {
815 + if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
819 + if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
827 +int nf_flow_snat_port(const struct flow_offload *flow,
828 + struct sk_buff *skb, unsigned int thoff,
829 + u8 protocol, enum flow_offload_tuple_dir dir)
831 + struct flow_ports *hdr;
832 + __be16 port, new_port;
834 + if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
835 + skb_try_make_writable(skb, thoff + sizeof(*hdr)))
838 + hdr = (void *)(skb_network_header(skb) + thoff);
841 + case FLOW_OFFLOAD_DIR_ORIGINAL:
842 + port = hdr->source;
843 + new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port;
844 + hdr->source = new_port;
846 + case FLOW_OFFLOAD_DIR_REPLY:
848 + new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
849 + hdr->dest = new_port;
855 + return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
857 +EXPORT_SYMBOL_GPL(nf_flow_snat_port);
859 +int nf_flow_dnat_port(const struct flow_offload *flow,
860 + struct sk_buff *skb, unsigned int thoff,
861 + u8 protocol, enum flow_offload_tuple_dir dir)
863 + struct flow_ports *hdr;
864 + __be16 port, new_port;
866 + if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
867 + skb_try_make_writable(skb, thoff + sizeof(*hdr)))
870 + hdr = (void *)(skb_network_header(skb) + thoff);
873 + case FLOW_OFFLOAD_DIR_ORIGINAL:
875 + new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port;
876 + hdr->dest = new_port;
878 + case FLOW_OFFLOAD_DIR_REPLY:
879 + port = hdr->source;
880 + new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
881 + hdr->source = new_port;
887 + return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
889 +EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
891 +static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
893 + struct net_device *dev = data;
895 + if (dev && flow->tuplehash[0].tuple.iifidx != dev->ifindex)
898 + flow_offload_dead(flow);
901 +static void nf_flow_table_iterate_cleanup(struct nf_flowtable *flowtable,
904 + nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, data);
905 + flush_delayed_work(&flowtable->gc_work);
908 +void nf_flow_table_cleanup(struct net *net, struct net_device *dev)
910 + nft_flow_table_iterate(net, nf_flow_table_iterate_cleanup, dev);
912 +EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
914 +void nf_flow_table_free(struct nf_flowtable *flow_table)
916 + nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
917 + WARN_ON(!nf_flow_offload_gc_step(flow_table));
919 +EXPORT_SYMBOL_GPL(nf_flow_table_free);
921 +static int nf_flow_table_netdev_event(struct notifier_block *this,
922 + unsigned long event, void *ptr)
924 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
926 + if (event != NETDEV_DOWN)
927 + return NOTIFY_DONE;
929 + nf_flow_table_cleanup(dev_net(dev), dev);
931 + return NOTIFY_DONE;
934 +static struct notifier_block flow_offload_netdev_notifier = {
935 + .notifier_call = nf_flow_table_netdev_event,
938 +static int __init nf_flow_table_module_init(void)
940 + return register_netdevice_notifier(&flow_offload_netdev_notifier);
943 +static void __exit nf_flow_table_module_exit(void)
945 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
948 +module_init(nf_flow_table_module_init);
949 +module_exit(nf_flow_table_module_exit);
951 +MODULE_LICENSE("GPL");
952 +MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");