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 @@ -968,6 +967,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>
113 +#include <net/netfilter/nf_conntrack.h>
114 +#include <net/netfilter/nf_flow_table.h>
116 +static struct nf_flowtable nf_flowtable;
117 +static HLIST_HEAD(hooks);
118 +static DEFINE_SPINLOCK(hooks_lock);
119 +static struct delayed_work hook_work;
121 +struct xt_flowoffload_hook {
122 + struct hlist_node list;
123 + struct nf_hook_ops ops;
129 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
130 + const struct nf_hook_state *state)
132 + switch (skb->protocol) {
133 + case htons(ETH_P_IP):
134 + return nf_flow_offload_ip_hook(priv, skb, state);
135 + case htons(ETH_P_IPV6):
136 + return nf_flow_offload_ipv6_hook(priv, skb, state);
143 +xt_flowoffload_create_hook(struct net_device *dev)
145 + struct xt_flowoffload_hook *hook;
146 + struct nf_hook_ops *ops;
148 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
153 + ops->pf = NFPROTO_NETDEV;
154 + ops->hooknum = NF_NETDEV_INGRESS;
155 + ops->priority = 10;
156 + ops->priv = &nf_flowtable;
157 + ops->hook = xt_flowoffload_net_hook;
160 + hlist_add_head(&hook->list, &hooks);
161 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
166 +static struct xt_flowoffload_hook *
167 +flow_offload_lookup_hook(struct net_device *dev)
169 + struct xt_flowoffload_hook *hook;
171 + hlist_for_each_entry(hook, &hooks, list) {
172 + if (hook->ops.dev == dev)
180 +xt_flowoffload_check_device(struct net_device *dev)
182 + struct xt_flowoffload_hook *hook;
184 + spin_lock_bh(&hooks_lock);
185 + hook = flow_offload_lookup_hook(dev);
189 + xt_flowoffload_create_hook(dev);
190 + spin_unlock_bh(&hooks_lock);
194 +xt_flowoffload_register_hooks(void)
196 + struct xt_flowoffload_hook *hook;
199 + hlist_for_each_entry(hook, &hooks, list) {
200 + if (hook->registered)
203 + hook->registered = true;
204 + spin_unlock_bh(&hooks_lock);
205 + nf_register_net_hook(dev_net(hook->ops.dev), &hook->ops);
206 + spin_lock_bh(&hooks_lock);
213 +xt_flowoffload_cleanup_hooks(void)
215 + struct xt_flowoffload_hook *hook;
218 + hlist_for_each_entry(hook, &hooks, list) {
222 + hlist_del(&hook->list);
223 + spin_unlock_bh(&hooks_lock);
224 + nf_unregister_net_hook(dev_net(hook->ops.dev), &hook->ops);
226 + spin_lock_bh(&hooks_lock);
233 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
235 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
236 + struct xt_flowoffload_hook *hook;
237 + bool *found = data;
239 + spin_lock_bh(&hooks_lock);
240 + hlist_for_each_entry(hook, &hooks, list) {
241 + if (hook->ops.dev->ifindex != tuple->iifidx &&
242 + hook->ops.dev->ifindex != tuple->oifidx)
248 + spin_unlock_bh(&hooks_lock);
252 +xt_flowoffload_hook_work(struct work_struct *work)
254 + struct xt_flowoffload_hook *hook;
255 + bool found = false;
258 + spin_lock_bh(&hooks_lock);
259 + xt_flowoffload_register_hooks();
260 + hlist_for_each_entry(hook, &hooks, list)
261 + hook->used = false;
262 + spin_unlock_bh(&hooks_lock);
264 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
266 + if (err && err != -EAGAIN)
269 + spin_lock_bh(&hooks_lock);
270 + xt_flowoffload_cleanup_hooks();
271 + spin_unlock_bh(&hooks_lock);
275 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
279 +xt_flowoffload_skip(struct sk_buff *skb)
281 + struct ip_options *opt = &(IPCB(skb)->opt);
283 + if (unlikely(opt->optlen))
285 + if (skb_sec_path(skb))
292 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
293 + const struct xt_action_param *par,
294 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
296 + struct dst_entry *this_dst = skb_dst(skb);
297 + struct dst_entry *other_dst = NULL;
300 + memset(&fl, 0, sizeof(fl));
301 + switch (xt_family(par)) {
303 + fl.u.ip4.daddr = ct->tuplehash[!dir].tuple.dst.u3.ip;
306 + fl.u.ip6.daddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
310 + nf_route(xt_net(par), &other_dst, &fl, false, xt_family(par));
314 + route->tuple[dir].dst = this_dst;
315 + route->tuple[dir].ifindex = xt_in(par)->ifindex;
316 + route->tuple[!dir].dst = other_dst;
317 + route->tuple[!dir].ifindex = xt_out(par)->ifindex;
323 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
325 + enum ip_conntrack_info ctinfo;
326 + enum ip_conntrack_dir dir;
327 + struct nf_flow_route route;
328 + struct flow_offload *flow;
329 + struct nf_conn *ct;
331 + if (xt_flowoffload_skip(skb))
332 + return XT_CONTINUE;
334 + ct = nf_ct_get(skb, &ctinfo);
336 + return XT_CONTINUE;
338 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
343 + return XT_CONTINUE;
346 + if (test_bit(IPS_HELPER_BIT, &ct->status))
347 + return XT_CONTINUE;
349 + if (ctinfo == IP_CT_NEW ||
350 + ctinfo == IP_CT_RELATED)
351 + return XT_CONTINUE;
353 + if (!xt_in(par) || !xt_out(par))
354 + return XT_CONTINUE;
356 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
357 + return XT_CONTINUE;
359 + dir = CTINFO2DIR(ctinfo);
361 + if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
362 + goto err_flow_route;
364 + flow = flow_offload_alloc(ct, &route);
366 + goto err_flow_alloc;
368 + if (flow_offload_add(&nf_flowtable, flow) < 0)
371 + xt_flowoffload_check_device(xt_in(par));
372 + xt_flowoffload_check_device(xt_out(par));
374 + return XT_CONTINUE;
377 + flow_offload_free(flow);
379 + dst_release(route.tuple[!dir].dst);
381 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
382 + return XT_CONTINUE;
386 +static int flowoffload_chk(const struct xt_tgchk_param *par)
391 +static struct xt_target offload_tg_reg __read_mostly = {
392 + .family = NFPROTO_UNSPEC,
393 + .name = "FLOWOFFLOAD",
395 + .checkentry = flowoffload_chk,
396 + .target = flowoffload_tg,
400 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
402 + nf_flow_table_init(table);
406 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
408 + nf_flow_table_free(table);
411 +static int __init xt_flowoffload_tg_init(void)
415 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
417 + ret = xt_flowoffload_table_init(&nf_flowtable);
421 + ret = xt_register_target(&offload_tg_reg);
423 + xt_flowoffload_table_cleanup(&nf_flowtable);
428 +static void __exit xt_flowoffload_tg_exit(void)
430 + xt_unregister_target(&offload_tg_reg);
431 + xt_flowoffload_table_cleanup(&nf_flowtable);
434 +MODULE_LICENSE("GPL");
435 +module_init(xt_flowoffload_tg_init);
436 +module_exit(xt_flowoffload_tg_exit);
437 --- a/net/netfilter/nf_flow_table_core.c
438 +++ b/net/netfilter/nf_flow_table_core.c
440 #include <linux/netdevice.h>
442 #include <net/ip6_route.h>
443 -#include <net/netfilter/nf_tables.h>
444 #include <net/netfilter/nf_flow_table.h>
445 #include <net/netfilter/nf_conntrack.h>
446 #include <net/netfilter/nf_conntrack_core.h>