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 @@ -76,8 +76,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 @@ -86,6 +84,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 @@ -97,7 +97,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 @@ -107,6 +106,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 @@ -671,8 +671,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 @@ -681,11 +679,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 @@ -974,6 +973,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 @@ -134,6 +134,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_flow_table.h>
116 +#include <net/netfilter/nf_conntrack_helper.h>
118 +static struct nf_flowtable nf_flowtable;
119 +static HLIST_HEAD(hooks);
120 +static DEFINE_SPINLOCK(hooks_lock);
121 +static struct delayed_work hook_work;
123 +struct xt_flowoffload_hook {
124 + struct hlist_node list;
125 + struct nf_hook_ops ops;
132 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
133 + const struct nf_hook_state *state)
135 + switch (skb->protocol) {
136 + case htons(ETH_P_IP):
137 + return nf_flow_offload_ip_hook(priv, skb, state);
138 + case htons(ETH_P_IPV6):
139 + return nf_flow_offload_ipv6_hook(priv, skb, state);
146 +xt_flowoffload_create_hook(struct net_device *dev)
148 + struct xt_flowoffload_hook *hook;
149 + struct nf_hook_ops *ops;
151 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
156 + ops->pf = NFPROTO_NETDEV;
157 + ops->hooknum = NF_NETDEV_INGRESS;
158 + ops->priority = 10;
159 + ops->priv = &nf_flowtable;
160 + ops->hook = xt_flowoffload_net_hook;
163 + hlist_add_head(&hook->list, &hooks);
164 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
169 +static struct xt_flowoffload_hook *
170 +flow_offload_lookup_hook(struct net_device *dev)
172 + struct xt_flowoffload_hook *hook;
174 + hlist_for_each_entry(hook, &hooks, list) {
175 + if (hook->ops.dev == dev)
183 +xt_flowoffload_check_device(struct net_device *dev)
185 + struct xt_flowoffload_hook *hook;
187 + spin_lock_bh(&hooks_lock);
188 + hook = flow_offload_lookup_hook(dev);
192 + xt_flowoffload_create_hook(dev);
193 + spin_unlock_bh(&hooks_lock);
197 +xt_flowoffload_register_hooks(void)
199 + struct xt_flowoffload_hook *hook;
202 + hlist_for_each_entry(hook, &hooks, list) {
203 + if (hook->registered)
206 + hook->registered = true;
207 + hook->net = dev_net(hook->ops.dev);
208 + spin_unlock_bh(&hooks_lock);
209 + nf_register_net_hook(hook->net, &hook->ops);
210 + spin_lock_bh(&hooks_lock);
217 +xt_flowoffload_cleanup_hooks(void)
219 + struct xt_flowoffload_hook *hook;
222 + hlist_for_each_entry(hook, &hooks, list) {
223 + if (hook->used || !hook->registered)
226 + hlist_del(&hook->list);
227 + spin_unlock_bh(&hooks_lock);
228 + nf_unregister_net_hook(hook->net, &hook->ops);
230 + spin_lock_bh(&hooks_lock);
237 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
239 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
240 + struct xt_flowoffload_hook *hook;
241 + bool *found = data;
243 + spin_lock_bh(&hooks_lock);
244 + hlist_for_each_entry(hook, &hooks, list) {
245 + if (hook->ops.dev->ifindex != tuple->iifidx &&
246 + hook->ops.dev->ifindex != tuple->oifidx)
252 + spin_unlock_bh(&hooks_lock);
256 +xt_flowoffload_hook_work(struct work_struct *work)
258 + struct xt_flowoffload_hook *hook;
259 + bool found = false;
262 + spin_lock_bh(&hooks_lock);
263 + xt_flowoffload_register_hooks();
264 + hlist_for_each_entry(hook, &hooks, list)
265 + hook->used = false;
266 + spin_unlock_bh(&hooks_lock);
268 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
270 + if (err && err != -EAGAIN)
273 + spin_lock_bh(&hooks_lock);
274 + xt_flowoffload_cleanup_hooks();
275 + spin_unlock_bh(&hooks_lock);
279 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
283 +xt_flowoffload_skip(struct sk_buff *skb)
285 + struct ip_options *opt = &(IPCB(skb)->opt);
287 + if (unlikely(opt->optlen))
289 + if (skb_sec_path(skb))
295 +static struct dst_entry *
296 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
297 + const struct xt_action_param *par)
299 + struct dst_entry *dst = NULL;
302 + memset(&fl, 0, sizeof(fl));
303 + switch (xt_family(par)) {
305 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
308 + fl.u.ip6.saddr = ct->tuplehash[dir].tuple.dst.u3.in6;
309 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
313 + nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
319 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
320 + const struct xt_action_param *par,
321 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
323 + struct dst_entry *this_dst, *other_dst;
325 + this_dst = xt_flowoffload_dst(ct, dir, par);
326 + other_dst = xt_flowoffload_dst(ct, !dir, par);
327 + if (!this_dst || !other_dst)
330 + if (dst_xfrm(this_dst) || dst_xfrm(other_dst))
333 + route->tuple[dir].dst = this_dst;
334 + route->tuple[dir].ifindex = xt_in(par)->ifindex;
335 + route->tuple[!dir].dst = other_dst;
336 + route->tuple[!dir].ifindex = xt_out(par)->ifindex;
342 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
344 + const struct xt_flowoffload_target_info *info = par->targinfo;
345 + enum ip_conntrack_info ctinfo;
346 + enum ip_conntrack_dir dir;
347 + struct nf_flow_route route;
348 + struct flow_offload *flow;
349 + struct nf_conn *ct;
350 + const struct nf_conn_help *help;
352 + if (xt_flowoffload_skip(skb))
353 + return XT_CONTINUE;
355 + ct = nf_ct_get(skb, &ctinfo);
357 + return XT_CONTINUE;
359 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
361 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
362 + return XT_CONTINUE;
367 + return XT_CONTINUE;
370 + help = nfct_help(ct);
372 + return XT_CONTINUE;
374 + if (ctinfo == IP_CT_NEW ||
375 + ctinfo == IP_CT_RELATED)
376 + return XT_CONTINUE;
378 + if (!xt_in(par) || !xt_out(par))
379 + return XT_CONTINUE;
381 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
382 + return XT_CONTINUE;
384 + dir = CTINFO2DIR(ctinfo);
386 + if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
387 + goto err_flow_route;
389 + flow = flow_offload_alloc(ct, &route);
391 + goto err_flow_alloc;
393 + if (flow_offload_add(&nf_flowtable, flow) < 0)
396 + xt_flowoffload_check_device(xt_in(par));
397 + xt_flowoffload_check_device(xt_out(par));
399 + if (info->flags & XT_FLOWOFFLOAD_HW)
400 + nf_flow_offload_hw_add(xt_net(par), flow, ct);
402 + return XT_CONTINUE;
405 + flow_offload_free(flow);
407 + dst_release(route.tuple[!dir].dst);
409 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
410 + return XT_CONTINUE;
414 +static int flowoffload_chk(const struct xt_tgchk_param *par)
416 + struct xt_flowoffload_target_info *info = par->targinfo;
418 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
424 +static struct xt_target offload_tg_reg __read_mostly = {
425 + .family = NFPROTO_UNSPEC,
426 + .name = "FLOWOFFLOAD",
428 + .targetsize = sizeof(struct xt_flowoffload_target_info),
429 + .usersize = sizeof(struct xt_flowoffload_target_info),
430 + .checkentry = flowoffload_chk,
431 + .target = flowoffload_tg,
435 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
437 + table->flags = NF_FLOWTABLE_F_HW;
438 + nf_flow_table_init(table);
442 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
444 + nf_flow_table_free(table);
447 +static int flow_offload_netdev_event(struct notifier_block *this,
448 + unsigned long event, void *ptr)
450 + struct xt_flowoffload_hook *hook = NULL;
451 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
453 + if (event != NETDEV_UNREGISTER)
454 + return NOTIFY_DONE;
456 + spin_lock_bh(&hooks_lock);
457 + hook = flow_offload_lookup_hook(dev);
459 + hlist_del(&hook->list);
461 + spin_unlock_bh(&hooks_lock);
463 + nf_unregister_net_hook(hook->net, &hook->ops);
467 + nf_flow_table_cleanup(dev_net(dev), dev);
469 + return NOTIFY_DONE;
472 +static struct notifier_block flow_offload_netdev_notifier = {
473 + .notifier_call = flow_offload_netdev_event,
476 +static int __init xt_flowoffload_tg_init(void)
480 + register_netdevice_notifier(&flow_offload_netdev_notifier);
482 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
484 + ret = xt_flowoffload_table_init(&nf_flowtable);
488 + ret = xt_register_target(&offload_tg_reg);
490 + xt_flowoffload_table_cleanup(&nf_flowtable);
495 +static void __exit xt_flowoffload_tg_exit(void)
497 + xt_unregister_target(&offload_tg_reg);
498 + xt_flowoffload_table_cleanup(&nf_flowtable);
499 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
502 +MODULE_LICENSE("GPL");
503 +module_init(xt_flowoffload_tg_init);
504 +module_exit(xt_flowoffload_tg_exit);
505 --- a/net/netfilter/nf_flow_table_core.c
506 +++ b/net/netfilter/nf_flow_table_core.c
508 #include <linux/netdevice.h>
510 #include <net/ip6_route.h>
511 -#include <net/netfilter/nf_tables.h>
512 #include <net/netfilter/nf_flow_table.h>
513 #include <net/netfilter/nf_conntrack.h>
514 #include <net/netfilter/nf_conntrack_core.h>
516 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
518 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
519 +#ifndef _XT_FLOWOFFLOAD_H
520 +#define _XT_FLOWOFFLOAD_H
522 +#include <linux/types.h>
525 + XT_FLOWOFFLOAD_HW = 1 << 0,
527 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
530 +struct xt_flowoffload_target_info {
534 +#endif /* _XT_FLOWOFFLOAD_H */