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 @@ -75,8 +75,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 @@ -85,6 +83,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 @@ -69,7 +69,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 @@ -79,6 +78,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 @@ -665,8 +665,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 @@ -675,11 +673,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 @@ -959,6 +958,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 @@ -133,6 +133,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>
117 +static struct nf_flowtable nf_flowtable;
118 +static HLIST_HEAD(hooks);
119 +static DEFINE_SPINLOCK(hooks_lock);
120 +static struct delayed_work hook_work;
122 +struct xt_flowoffload_hook {
123 + struct hlist_node list;
124 + struct nf_hook_ops ops;
131 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
132 + const struct nf_hook_state *state)
134 + switch (skb->protocol) {
135 + case htons(ETH_P_IP):
136 + return nf_flow_offload_ip_hook(priv, skb, state);
137 + case htons(ETH_P_IPV6):
138 + return nf_flow_offload_ipv6_hook(priv, skb, state);
145 +xt_flowoffload_create_hook(struct net_device *dev)
147 + struct xt_flowoffload_hook *hook;
148 + struct nf_hook_ops *ops;
150 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
155 + ops->pf = NFPROTO_NETDEV;
156 + ops->hooknum = NF_NETDEV_INGRESS;
157 + ops->priority = 10;
158 + ops->priv = &nf_flowtable;
159 + ops->hook = xt_flowoffload_net_hook;
162 + hlist_add_head(&hook->list, &hooks);
163 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
168 +static struct xt_flowoffload_hook *
169 +flow_offload_lookup_hook(struct net_device *dev)
171 + struct xt_flowoffload_hook *hook;
173 + hlist_for_each_entry(hook, &hooks, list) {
174 + if (hook->ops.dev == dev)
182 +xt_flowoffload_check_device(struct net_device *dev)
184 + struct xt_flowoffload_hook *hook;
186 + spin_lock_bh(&hooks_lock);
187 + hook = flow_offload_lookup_hook(dev);
191 + xt_flowoffload_create_hook(dev);
192 + spin_unlock_bh(&hooks_lock);
196 +xt_flowoffload_register_hooks(void)
198 + struct xt_flowoffload_hook *hook;
201 + hlist_for_each_entry(hook, &hooks, list) {
202 + if (hook->registered)
205 + hook->registered = true;
206 + hook->net = dev_net(hook->ops.dev);
207 + spin_unlock_bh(&hooks_lock);
208 + nf_register_net_hook(hook->net, &hook->ops);
209 + spin_lock_bh(&hooks_lock);
216 +xt_flowoffload_cleanup_hooks(void)
218 + struct xt_flowoffload_hook *hook;
221 + hlist_for_each_entry(hook, &hooks, list) {
222 + if (hook->used || !hook->registered)
225 + hlist_del(&hook->list);
226 + spin_unlock_bh(&hooks_lock);
227 + nf_unregister_net_hook(hook->net, &hook->ops);
229 + spin_lock_bh(&hooks_lock);
236 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
238 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
239 + struct xt_flowoffload_hook *hook;
240 + bool *found = data;
242 + spin_lock_bh(&hooks_lock);
243 + hlist_for_each_entry(hook, &hooks, list) {
244 + if (hook->ops.dev->ifindex != tuple->iifidx &&
245 + hook->ops.dev->ifindex != tuple->oifidx)
251 + spin_unlock_bh(&hooks_lock);
255 +xt_flowoffload_hook_work(struct work_struct *work)
257 + struct xt_flowoffload_hook *hook;
258 + bool found = false;
261 + spin_lock_bh(&hooks_lock);
262 + xt_flowoffload_register_hooks();
263 + hlist_for_each_entry(hook, &hooks, list)
264 + hook->used = false;
265 + spin_unlock_bh(&hooks_lock);
267 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
269 + if (err && err != -EAGAIN)
272 + spin_lock_bh(&hooks_lock);
273 + xt_flowoffload_cleanup_hooks();
274 + spin_unlock_bh(&hooks_lock);
278 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
282 +xt_flowoffload_skip(struct sk_buff *skb)
284 + struct ip_options *opt = &(IPCB(skb)->opt);
286 + if (unlikely(opt->optlen))
288 + if (skb_sec_path(skb))
294 +static struct dst_entry *
295 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
296 + const struct xt_action_param *par)
298 + struct dst_entry *dst;
301 + memset(&fl, 0, sizeof(fl));
302 + switch (xt_family(par)) {
304 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
307 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
311 + nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
317 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
318 + const struct xt_action_param *par,
319 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
321 + struct dst_entry *this_dst, *other_dst;
323 + this_dst = xt_flowoffload_dst(ct, dir, par);
324 + other_dst = xt_flowoffload_dst(ct, !dir, par);
325 + if (!this_dst || !other_dst)
328 + route->tuple[dir].dst = this_dst;
329 + route->tuple[dir].ifindex = xt_in(par)->ifindex;
330 + route->tuple[!dir].dst = other_dst;
331 + route->tuple[!dir].ifindex = xt_out(par)->ifindex;
337 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
339 + const struct xt_flowoffload_target_info *info = par->targinfo;
340 + enum ip_conntrack_info ctinfo;
341 + enum ip_conntrack_dir dir;
342 + struct nf_flow_route route;
343 + struct flow_offload *flow;
344 + struct nf_conn *ct;
346 + if (xt_flowoffload_skip(skb))
347 + return XT_CONTINUE;
349 + ct = nf_ct_get(skb, &ctinfo);
351 + return XT_CONTINUE;
353 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
355 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
356 + return XT_CONTINUE;
361 + return XT_CONTINUE;
364 + if (test_bit(IPS_HELPER_BIT, &ct->status))
365 + return XT_CONTINUE;
367 + if (ctinfo == IP_CT_NEW ||
368 + ctinfo == IP_CT_RELATED)
369 + return XT_CONTINUE;
371 + if (!xt_in(par) || !xt_out(par))
372 + return XT_CONTINUE;
374 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
375 + return XT_CONTINUE;
377 + dir = CTINFO2DIR(ctinfo);
379 + if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
380 + goto err_flow_route;
382 + flow = flow_offload_alloc(ct, &route);
384 + goto err_flow_alloc;
386 + if (flow_offload_add(&nf_flowtable, flow) < 0)
389 + xt_flowoffload_check_device(xt_in(par));
390 + xt_flowoffload_check_device(xt_out(par));
392 + if (info->flags & XT_FLOWOFFLOAD_HW)
393 + nf_flow_offload_hw_add(xt_net(par), flow, ct);
395 + return XT_CONTINUE;
398 + flow_offload_free(flow);
400 + dst_release(route.tuple[!dir].dst);
402 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
403 + return XT_CONTINUE;
407 +static int flowoffload_chk(const struct xt_tgchk_param *par)
409 + struct xt_flowoffload_target_info *info = par->targinfo;
411 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
417 +static struct xt_target offload_tg_reg __read_mostly = {
418 + .family = NFPROTO_UNSPEC,
419 + .name = "FLOWOFFLOAD",
421 + .targetsize = sizeof(struct xt_flowoffload_target_info),
422 + .usersize = sizeof(struct xt_flowoffload_target_info),
423 + .checkentry = flowoffload_chk,
424 + .target = flowoffload_tg,
428 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
430 + table->flags = NF_FLOWTABLE_F_HW;
431 + nf_flow_table_init(table);
435 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
437 + nf_flow_table_free(table);
440 +static int __init xt_flowoffload_tg_init(void)
444 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
446 + ret = xt_flowoffload_table_init(&nf_flowtable);
450 + ret = xt_register_target(&offload_tg_reg);
452 + xt_flowoffload_table_cleanup(&nf_flowtable);
457 +static void __exit xt_flowoffload_tg_exit(void)
459 + xt_unregister_target(&offload_tg_reg);
460 + xt_flowoffload_table_cleanup(&nf_flowtable);
463 +MODULE_LICENSE("GPL");
464 +module_init(xt_flowoffload_tg_init);
465 +module_exit(xt_flowoffload_tg_exit);
466 --- a/net/netfilter/nf_flow_table_core.c
467 +++ b/net/netfilter/nf_flow_table_core.c
469 #include <linux/netdevice.h>
471 #include <net/ip6_route.h>
472 -#include <net/netfilter/nf_tables.h>
473 #include <net/netfilter/nf_flow_table.h>
474 #include <net/netfilter/nf_conntrack.h>
475 #include <net/netfilter/nf_conntrack_core.h>
477 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
479 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
480 +#ifndef _XT_FLOWOFFLOAD_H
481 +#define _XT_FLOWOFFLOAD_H
483 +#include <linux/types.h>
486 + XT_FLOWOFFLOAD_HW = 1 << 0,
488 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
491 +struct xt_flowoffload_target_info {
495 +#endif /* _XT_FLOWOFFLOAD_H */