1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Tue, 20 Feb 2018 15:56:02 +0100
3 Subject: [PATCH] netfilter: add xt_OFFLOAD target
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 create mode 100644 net/netfilter/xt_OFFLOAD.c
9 --- a/net/ipv4/netfilter/Kconfig
10 +++ b/net/ipv4/netfilter/Kconfig
11 @@ -63,8 +63,6 @@ config NF_TABLES_ARP
13 This option enables the ARP support for nf_tables.
17 config NF_FLOW_TABLE_IPV4
18 tristate "Netfilter flow table IPv4 module"
19 depends on NF_FLOW_TABLE
20 @@ -73,6 +71,8 @@ config NF_FLOW_TABLE_IPV4
22 To compile it as a module, choose M here.
27 tristate "Netfilter IPv4 packet duplication to alternate destination"
28 depends on !NF_CONNTRACK || NF_CONNTRACK
29 --- a/net/ipv6/netfilter/Kconfig
30 +++ b/net/ipv6/netfilter/Kconfig
31 @@ -80,7 +80,6 @@ config NFT_FIB_IPV6
32 multicast or blackhole.
34 endif # NF_TABLES_IPV6
37 config NF_FLOW_TABLE_IPV6
38 tristate "Netfilter flow table IPv6 module"
39 @@ -90,6 +89,8 @@ config NF_FLOW_TABLE_IPV6
41 To compile it as a module, choose M here.
46 tristate "Netfilter IPv6 packet duplication to alternate destination"
47 depends on !NF_CONNTRACK || NF_CONNTRACK
48 --- a/net/netfilter/Kconfig
49 +++ b/net/netfilter/Kconfig
50 @@ -693,8 +693,6 @@ config NFT_FIB_NETDEV
52 endif # NF_TABLES_NETDEV
56 config NF_FLOW_TABLE_INET
57 tristate "Netfilter flow table mixed IPv4/IPv6 module"
58 depends on NF_FLOW_TABLE
59 @@ -703,11 +701,12 @@ config NF_FLOW_TABLE_INET
61 To compile it as a module, choose M here.
66 tristate "Netfilter flow table module"
67 depends on NETFILTER_INGRESS
68 depends on NF_CONNTRACK
69 - depends on NF_TABLES
71 This option adds the flow table core infrastructure.
73 @@ -996,6 +995,15 @@ config NETFILTER_XT_TARGET_NOTRACK
74 depends on NETFILTER_ADVANCED
75 select NETFILTER_XT_TARGET_CT
77 +config NETFILTER_XT_TARGET_FLOWOFFLOAD
78 + tristate '"FLOWOFFLOAD" target support'
79 + depends on NF_FLOW_TABLE
80 + depends on NETFILTER_INGRESS
82 + This option adds a `FLOWOFFLOAD' target, which uses the nf_flow_offload
83 + module to speed up processing of packets by bypassing the usual
86 config NETFILTER_XT_TARGET_RATEEST
87 tristate '"RATEEST" target support'
88 depends on NETFILTER_ADVANCED
89 --- a/net/netfilter/Makefile
90 +++ b/net/netfilter/Makefile
91 @@ -144,6 +144,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIF
92 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
93 obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
94 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
95 +obj-$(CONFIG_NETFILTER_XT_TARGET_FLOWOFFLOAD) += xt_FLOWOFFLOAD.o
96 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
97 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
98 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
100 +++ b/net/netfilter/xt_FLOWOFFLOAD.c
103 + * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
105 + * This program is free software; you can redistribute it and/or modify
106 + * it under the terms of the GNU General Public License version 2 as
107 + * published by the Free Software Foundation.
109 +#include <linux/module.h>
110 +#include <linux/init.h>
111 +#include <linux/netfilter.h>
112 +#include <linux/netfilter/xt_FLOWOFFLOAD.h>
114 +#include <net/netfilter/nf_conntrack.h>
115 +#include <net/netfilter/nf_conntrack_extend.h>
116 +#include <net/netfilter/nf_conntrack_helper.h>
117 +#include <net/netfilter/nf_flow_table.h>
119 +static struct nf_flowtable nf_flowtable;
120 +static HLIST_HEAD(hooks);
121 +static DEFINE_SPINLOCK(hooks_lock);
122 +static struct delayed_work hook_work;
124 +struct xt_flowoffload_hook {
125 + struct hlist_node list;
126 + struct nf_hook_ops ops;
133 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
134 + const struct nf_hook_state *state)
136 + switch (skb->protocol) {
137 + case htons(ETH_P_IP):
138 + return nf_flow_offload_ip_hook(priv, skb, state);
139 + case htons(ETH_P_IPV6):
140 + return nf_flow_offload_ipv6_hook(priv, skb, state);
147 +xt_flowoffload_create_hook(struct net_device *dev)
149 + struct xt_flowoffload_hook *hook;
150 + struct nf_hook_ops *ops;
152 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
157 + ops->pf = NFPROTO_NETDEV;
158 + ops->hooknum = NF_NETDEV_INGRESS;
159 + ops->priority = 10;
160 + ops->priv = &nf_flowtable;
161 + ops->hook = xt_flowoffload_net_hook;
164 + hlist_add_head(&hook->list, &hooks);
165 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
170 +static struct xt_flowoffload_hook *
171 +flow_offload_lookup_hook(struct net_device *dev)
173 + struct xt_flowoffload_hook *hook;
175 + hlist_for_each_entry(hook, &hooks, list) {
176 + if (hook->ops.dev == dev)
184 +xt_flowoffload_check_device(struct net_device *dev)
186 + struct xt_flowoffload_hook *hook;
188 + spin_lock_bh(&hooks_lock);
189 + hook = flow_offload_lookup_hook(dev);
193 + xt_flowoffload_create_hook(dev);
194 + spin_unlock_bh(&hooks_lock);
198 +xt_flowoffload_register_hooks(void)
200 + struct xt_flowoffload_hook *hook;
203 + hlist_for_each_entry(hook, &hooks, list) {
204 + if (hook->registered)
207 + hook->registered = true;
208 + hook->net = dev_net(hook->ops.dev);
209 + spin_unlock_bh(&hooks_lock);
210 + nf_register_net_hook(hook->net, &hook->ops);
211 + spin_lock_bh(&hooks_lock);
218 +xt_flowoffload_cleanup_hooks(void)
220 + struct xt_flowoffload_hook *hook;
223 + hlist_for_each_entry(hook, &hooks, list) {
224 + if (hook->used || !hook->registered)
227 + hlist_del(&hook->list);
228 + spin_unlock_bh(&hooks_lock);
229 + nf_unregister_net_hook(hook->net, &hook->ops);
231 + spin_lock_bh(&hooks_lock);
238 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
240 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
241 + struct xt_flowoffload_hook *hook;
242 + bool *found = data;
244 + spin_lock_bh(&hooks_lock);
245 + hlist_for_each_entry(hook, &hooks, list) {
246 + if (hook->ops.dev->ifindex != tuple->iifidx &&
247 + hook->ops.dev->ifindex != tuple->oifidx)
253 + spin_unlock_bh(&hooks_lock);
257 +xt_flowoffload_hook_work(struct work_struct *work)
259 + struct xt_flowoffload_hook *hook;
260 + bool found = false;
263 + spin_lock_bh(&hooks_lock);
264 + xt_flowoffload_register_hooks();
265 + hlist_for_each_entry(hook, &hooks, list)
266 + hook->used = false;
267 + spin_unlock_bh(&hooks_lock);
269 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
271 + if (err && err != -EAGAIN)
274 + spin_lock_bh(&hooks_lock);
275 + xt_flowoffload_cleanup_hooks();
276 + spin_unlock_bh(&hooks_lock);
280 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
284 +xt_flowoffload_skip(struct sk_buff *skb, int family)
286 + if (skb_sec_path(skb))
289 + if (family == NFPROTO_IPV4) {
290 + const struct ip_options *opt = &(IPCB(skb)->opt);
292 + if (unlikely(opt->optlen))
299 +static struct dst_entry *
300 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
301 + const struct xt_action_param *par, int ifindex)
303 + struct dst_entry *dst = NULL;
306 + memset(&fl, 0, sizeof(fl));
307 + switch (xt_family(par)) {
309 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
310 + fl.u.ip4.flowi4_oif = ifindex;
313 + fl.u.ip6.saddr = ct->tuplehash[dir].tuple.dst.u3.in6;
314 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
315 + fl.u.ip6.flowi6_oif = ifindex;
319 + nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
325 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
326 + const struct xt_action_param *par,
327 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
329 + struct dst_entry *this_dst, *other_dst;
331 + this_dst = xt_flowoffload_dst(ct, !dir, par, xt_out(par)->ifindex);
332 + other_dst = xt_flowoffload_dst(ct, dir, par, xt_in(par)->ifindex);
334 + route->tuple[dir].dst = this_dst;
335 + route->tuple[!dir].dst = other_dst;
337 + if (!this_dst || !other_dst)
340 + if (dst_xfrm(this_dst) || dst_xfrm(other_dst))
347 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
349 + const struct xt_flowoffload_target_info *info = par->targinfo;
350 + struct tcphdr _tcph, *tcph = NULL;
351 + enum ip_conntrack_info ctinfo;
352 + enum ip_conntrack_dir dir;
353 + struct nf_flow_route route;
354 + struct flow_offload *flow = NULL;
355 + struct nf_conn *ct;
358 + if (xt_flowoffload_skip(skb, xt_family(par)))
359 + return XT_CONTINUE;
361 + ct = nf_ct_get(skb, &ctinfo);
363 + return XT_CONTINUE;
365 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
367 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
368 + return XT_CONTINUE;
370 + tcph = skb_header_pointer(skb, par->thoff,
371 + sizeof(_tcph), &_tcph);
372 + if (unlikely(!tcph || tcph->fin || tcph->rst))
373 + return XT_CONTINUE;
378 + return XT_CONTINUE;
381 + if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
382 + ct->status & IPS_SEQ_ADJUST)
383 + return XT_CONTINUE;
385 + if (!nf_ct_is_confirmed(ct))
386 + return XT_CONTINUE;
388 + if (!xt_in(par) || !xt_out(par))
389 + return XT_CONTINUE;
391 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
392 + return XT_CONTINUE;
394 + dir = CTINFO2DIR(ctinfo);
396 + if (xt_flowoffload_route(skb, ct, par, &route, dir) == 0)
397 + flow = flow_offload_alloc(ct, &route);
399 + dst_release(route.tuple[dir].dst);
400 + dst_release(route.tuple[!dir].dst);
403 + goto err_flow_route;
406 + ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
407 + ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
410 + if (flow_offload_add(&nf_flowtable, flow) < 0)
413 + xt_flowoffload_check_device(xt_in(par));
414 + xt_flowoffload_check_device(xt_out(par));
416 + net = read_pnet(&nf_flowtable.ft_net);
418 + write_pnet(&nf_flowtable.ft_net, xt_net(par));
420 + if (info->flags & XT_FLOWOFFLOAD_HW)
421 + nf_flow_offload_hw_add(xt_net(par), flow, ct);
423 + return XT_CONTINUE;
426 + flow_offload_free(flow);
428 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
429 + return XT_CONTINUE;
433 +static int flowoffload_chk(const struct xt_tgchk_param *par)
435 + struct xt_flowoffload_target_info *info = par->targinfo;
437 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
443 +static struct xt_target offload_tg_reg __read_mostly = {
444 + .family = NFPROTO_UNSPEC,
445 + .name = "FLOWOFFLOAD",
447 + .targetsize = sizeof(struct xt_flowoffload_target_info),
448 + .usersize = sizeof(struct xt_flowoffload_target_info),
449 + .checkentry = flowoffload_chk,
450 + .target = flowoffload_tg,
454 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
456 + table->flags = NF_FLOWTABLE_F_HW;
457 + nf_flow_table_init(table);
461 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
463 + nf_flow_table_free(table);
466 +static int flow_offload_netdev_event(struct notifier_block *this,
467 + unsigned long event, void *ptr)
469 + struct xt_flowoffload_hook *hook = NULL;
470 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
472 + if (event != NETDEV_UNREGISTER)
473 + return NOTIFY_DONE;
475 + spin_lock_bh(&hooks_lock);
476 + hook = flow_offload_lookup_hook(dev);
478 + hlist_del(&hook->list);
480 + spin_unlock_bh(&hooks_lock);
482 + nf_unregister_net_hook(hook->net, &hook->ops);
486 + nf_flow_table_cleanup(dev_net(dev), dev);
488 + return NOTIFY_DONE;
491 +static struct notifier_block flow_offload_netdev_notifier = {
492 + .notifier_call = flow_offload_netdev_event,
495 +static int __init xt_flowoffload_tg_init(void)
499 + register_netdevice_notifier(&flow_offload_netdev_notifier);
501 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
503 + ret = xt_flowoffload_table_init(&nf_flowtable);
507 + ret = xt_register_target(&offload_tg_reg);
509 + xt_flowoffload_table_cleanup(&nf_flowtable);
514 +static void __exit xt_flowoffload_tg_exit(void)
516 + xt_unregister_target(&offload_tg_reg);
517 + xt_flowoffload_table_cleanup(&nf_flowtable);
518 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
521 +MODULE_LICENSE("GPL");
522 +module_init(xt_flowoffload_tg_init);
523 +module_exit(xt_flowoffload_tg_exit);
524 --- a/net/netfilter/nf_flow_table_core.c
525 +++ b/net/netfilter/nf_flow_table_core.c
527 #include <linux/netdevice.h>
529 #include <net/ip6_route.h>
530 -#include <net/netfilter/nf_tables.h>
531 #include <net/netfilter/nf_flow_table.h>
532 #include <net/netfilter/nf_conntrack.h>
533 #include <net/netfilter/nf_conntrack_core.h>
535 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
537 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
538 +#ifndef _XT_FLOWOFFLOAD_H
539 +#define _XT_FLOWOFFLOAD_H
541 +#include <linux/types.h>
544 + XT_FLOWOFFLOAD_HW = 1 << 0,
546 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
549 +struct xt_flowoffload_target_info {
553 +#endif /* _XT_FLOWOFFLOAD_H */