7296cfa6c460649ffa79bf26c9ece7ddd583219b
[openwrt/openwrt.git] / target / linux / generic / hack-4.14 / 650-netfilter-add-xt_OFFLOAD-target.patch
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
4
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
6 ---
7 create mode 100644 net/netfilter/xt_OFFLOAD.c
8
9 --- a/net/ipv4/netfilter/Kconfig
10 +++ b/net/ipv4/netfilter/Kconfig
11 @@ -75,8 +75,6 @@ config NF_TABLES_ARP
12 help
13 This option enables the ARP support for nf_tables.
14
15 -endif # NF_TABLES
16 -
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
21
22 To compile it as a module, choose M here.
23
24 +endif # NF_TABLES
25 +
26 config NF_DUP_IPV4
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.
33
34 endif # NF_TABLES_IPV6
35 -endif # NF_TABLES
36
37 config NF_FLOW_TABLE_IPV6
38 tristate "Netfilter flow table IPv6 module"
39 @@ -79,6 +78,8 @@ config NF_FLOW_TABLE_IPV6
40
41 To compile it as a module, choose M here.
42
43 +endif # NF_TABLES
44 +
45 config NF_DUP_IPV6
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
51
52 endif # NF_TABLES_NETDEV
53
54 -endif # NF_TABLES
55 -
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
60
61 To compile it as a module, choose M here.
62
63 +endif # NF_TABLES
64 +
65 config NF_FLOW_TABLE
66 tristate "Netfilter flow table module"
67 depends on NETFILTER_INGRESS
68 depends on NF_CONNTRACK
69 - depends on NF_TABLES
70 help
71 This option adds the flow table core infrastructure.
72
73 @@ -959,6 +958,15 @@ config NETFILTER_XT_TARGET_NOTRACK
74 depends on NETFILTER_ADVANCED
75 select NETFILTER_XT_TARGET_CT
76
77 +config NETFILTER_XT_TARGET_FLOWOFFLOAD
78 + tristate '"FLOWOFFLOAD" target support'
79 + depends on NF_FLOW_TABLE
80 + depends on NETFILTER_INGRESS
81 + help
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
84 + netfilter chains
85 +
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
99 --- /dev/null
100 +++ b/net/netfilter/xt_FLOWOFFLOAD.c
101 @@ -0,0 +1,351 @@
102 +/*
103 + * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
104 + *
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.
108 + */
109 +#include <linux/module.h>
110 +#include <linux/init.h>
111 +#include <linux/netfilter.h>
112 +#include <net/ip.h>
113 +#include <net/netfilter/nf_conntrack.h>
114 +#include <net/netfilter/nf_flow_table.h>
115 +
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;
120 +
121 +struct xt_flowoffload_hook {
122 + struct hlist_node list;
123 + struct nf_hook_ops ops;
124 + struct net *net;
125 + bool registered;
126 + bool used;
127 +};
128 +
129 +static unsigned int
130 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
131 + const struct nf_hook_state *state)
132 +{
133 + switch (skb->protocol) {
134 + case htons(ETH_P_IP):
135 + return nf_flow_offload_ip_hook(priv, skb, state);
136 + case htons(ETH_P_IPV6):
137 + return nf_flow_offload_ipv6_hook(priv, skb, state);
138 + }
139 +
140 + return NF_ACCEPT;
141 +}
142 +
143 +static int
144 +xt_flowoffload_create_hook(struct net_device *dev)
145 +{
146 + struct xt_flowoffload_hook *hook;
147 + struct nf_hook_ops *ops;
148 +
149 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
150 + if (!hook)
151 + return -ENOMEM;
152 +
153 + ops = &hook->ops;
154 + ops->pf = NFPROTO_NETDEV;
155 + ops->hooknum = NF_NETDEV_INGRESS;
156 + ops->priority = 10;
157 + ops->priv = &nf_flowtable;
158 + ops->hook = xt_flowoffload_net_hook;
159 + ops->dev = dev;
160 +
161 + hlist_add_head(&hook->list, &hooks);
162 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
163 +
164 + return 0;
165 +}
166 +
167 +static struct xt_flowoffload_hook *
168 +flow_offload_lookup_hook(struct net_device *dev)
169 +{
170 + struct xt_flowoffload_hook *hook;
171 +
172 + hlist_for_each_entry(hook, &hooks, list) {
173 + if (hook->ops.dev == dev)
174 + return hook;
175 + }
176 +
177 + return NULL;
178 +}
179 +
180 +static void
181 +xt_flowoffload_check_device(struct net_device *dev)
182 +{
183 + struct xt_flowoffload_hook *hook;
184 +
185 + spin_lock_bh(&hooks_lock);
186 + hook = flow_offload_lookup_hook(dev);
187 + if (hook)
188 + hook->used = true;
189 + else
190 + xt_flowoffload_create_hook(dev);
191 + spin_unlock_bh(&hooks_lock);
192 +}
193 +
194 +static void
195 +xt_flowoffload_register_hooks(void)
196 +{
197 + struct xt_flowoffload_hook *hook;
198 +
199 +restart:
200 + hlist_for_each_entry(hook, &hooks, list) {
201 + if (hook->registered)
202 + continue;
203 +
204 + hook->registered = true;
205 + hook->net = dev_net(hook->ops.dev);
206 + spin_unlock_bh(&hooks_lock);
207 + nf_register_net_hook(hook->net, &hook->ops);
208 + spin_lock_bh(&hooks_lock);
209 + goto restart;
210 + }
211 +
212 +}
213 +
214 +static void
215 +xt_flowoffload_cleanup_hooks(void)
216 +{
217 + struct xt_flowoffload_hook *hook;
218 +
219 +restart:
220 + hlist_for_each_entry(hook, &hooks, list) {
221 + if (hook->used || !hook->registered)
222 + continue;
223 +
224 + hlist_del(&hook->list);
225 + spin_unlock_bh(&hooks_lock);
226 + nf_unregister_net_hook(hook->net, &hook->ops);
227 + kfree(hook);
228 + spin_lock_bh(&hooks_lock);
229 + goto restart;
230 + }
231 +
232 +}
233 +
234 +static void
235 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
236 +{
237 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
238 + struct xt_flowoffload_hook *hook;
239 + bool *found = data;
240 +
241 + spin_lock_bh(&hooks_lock);
242 + hlist_for_each_entry(hook, &hooks, list) {
243 + if (hook->ops.dev->ifindex != tuple->iifidx &&
244 + hook->ops.dev->ifindex != tuple->oifidx)
245 + continue;
246 +
247 + hook->used = true;
248 + *found = true;
249 + }
250 + spin_unlock_bh(&hooks_lock);
251 +}
252 +
253 +static void
254 +xt_flowoffload_hook_work(struct work_struct *work)
255 +{
256 + struct xt_flowoffload_hook *hook;
257 + bool found = false;
258 + int err;
259 +
260 + spin_lock_bh(&hooks_lock);
261 + xt_flowoffload_register_hooks();
262 + hlist_for_each_entry(hook, &hooks, list)
263 + hook->used = false;
264 + spin_unlock_bh(&hooks_lock);
265 +
266 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
267 + &found);
268 + if (err && err != -EAGAIN)
269 + goto out;
270 +
271 + spin_lock_bh(&hooks_lock);
272 + xt_flowoffload_cleanup_hooks();
273 + spin_unlock_bh(&hooks_lock);
274 +
275 +out:
276 + if (found)
277 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
278 +}
279 +
280 +static bool
281 +xt_flowoffload_skip(struct sk_buff *skb)
282 +{
283 + struct ip_options *opt = &(IPCB(skb)->opt);
284 +
285 + if (unlikely(opt->optlen))
286 + return true;
287 + if (skb_sec_path(skb))
288 + return true;
289 +
290 + return false;
291 +}
292 +
293 +static struct dst_entry *
294 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
295 + const struct xt_action_param *par)
296 +{
297 + struct dst_entry *dst;
298 + struct flowi fl;
299 +
300 + memset(&fl, 0, sizeof(fl));
301 + switch (xt_family(par)) {
302 + case NFPROTO_IPV4:
303 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
304 + break;
305 + case NFPROTO_IPV6:
306 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
307 + break;
308 + }
309 +
310 + nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
311 +
312 + return dst;
313 +}
314 +
315 +static int
316 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
317 + const struct xt_action_param *par,
318 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
319 +{
320 + struct dst_entry *this_dst, *other_dst;
321 +
322 + this_dst = xt_flowoffload_dst(ct, dir, par);
323 + other_dst = xt_flowoffload_dst(ct, !dir, par);
324 + if (!this_dst || !other_dst)
325 + return -ENOENT;
326 +
327 + route->tuple[dir].dst = this_dst;
328 + route->tuple[dir].ifindex = xt_in(par)->ifindex;
329 + route->tuple[!dir].dst = other_dst;
330 + route->tuple[!dir].ifindex = xt_out(par)->ifindex;
331 +
332 + return 0;
333 +}
334 +
335 +static unsigned int
336 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
337 +{
338 + enum ip_conntrack_info ctinfo;
339 + enum ip_conntrack_dir dir;
340 + struct nf_flow_route route;
341 + struct flow_offload *flow;
342 + struct nf_conn *ct;
343 +
344 + if (xt_flowoffload_skip(skb))
345 + return XT_CONTINUE;
346 +
347 + ct = nf_ct_get(skb, &ctinfo);
348 + if (ct == NULL)
349 + return XT_CONTINUE;
350 +
351 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
352 + case IPPROTO_TCP:
353 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
354 + return XT_CONTINUE;
355 + break;
356 + case IPPROTO_UDP:
357 + break;
358 + default:
359 + return XT_CONTINUE;
360 + }
361 +
362 + if (test_bit(IPS_HELPER_BIT, &ct->status))
363 + return XT_CONTINUE;
364 +
365 + if (ctinfo == IP_CT_NEW ||
366 + ctinfo == IP_CT_RELATED)
367 + return XT_CONTINUE;
368 +
369 + if (!xt_in(par) || !xt_out(par))
370 + return XT_CONTINUE;
371 +
372 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
373 + return XT_CONTINUE;
374 +
375 + dir = CTINFO2DIR(ctinfo);
376 +
377 + if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
378 + goto err_flow_route;
379 +
380 + flow = flow_offload_alloc(ct, &route);
381 + if (!flow)
382 + goto err_flow_alloc;
383 +
384 + if (flow_offload_add(&nf_flowtable, flow) < 0)
385 + goto err_flow_add;
386 +
387 + xt_flowoffload_check_device(xt_in(par));
388 + xt_flowoffload_check_device(xt_out(par));
389 +
390 + return XT_CONTINUE;
391 +
392 +err_flow_add:
393 + flow_offload_free(flow);
394 +err_flow_alloc:
395 + dst_release(route.tuple[!dir].dst);
396 +err_flow_route:
397 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
398 + return XT_CONTINUE;
399 +}
400 +
401 +
402 +static int flowoffload_chk(const struct xt_tgchk_param *par)
403 +{
404 + return 0;
405 +}
406 +
407 +static struct xt_target offload_tg_reg __read_mostly = {
408 + .family = NFPROTO_UNSPEC,
409 + .name = "FLOWOFFLOAD",
410 + .revision = 0,
411 + .checkentry = flowoffload_chk,
412 + .target = flowoffload_tg,
413 + .me = THIS_MODULE,
414 +};
415 +
416 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
417 +{
418 + nf_flow_table_init(table);
419 + return 0;
420 +}
421 +
422 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
423 +{
424 + nf_flow_table_free(table);
425 +}
426 +
427 +static int __init xt_flowoffload_tg_init(void)
428 +{
429 + int ret;
430 +
431 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
432 +
433 + ret = xt_flowoffload_table_init(&nf_flowtable);
434 + if (ret)
435 + return ret;
436 +
437 + ret = xt_register_target(&offload_tg_reg);
438 + if (ret)
439 + xt_flowoffload_table_cleanup(&nf_flowtable);
440 +
441 + return ret;
442 +}
443 +
444 +static void __exit xt_flowoffload_tg_exit(void)
445 +{
446 + xt_unregister_target(&offload_tg_reg);
447 + xt_flowoffload_table_cleanup(&nf_flowtable);
448 +}
449 +
450 +MODULE_LICENSE("GPL");
451 +module_init(xt_flowoffload_tg_init);
452 +module_exit(xt_flowoffload_tg_exit);
453 --- a/net/netfilter/nf_flow_table_core.c
454 +++ b/net/netfilter/nf_flow_table_core.c
455 @@ -6,7 +6,6 @@
456 #include <linux/netdevice.h>
457 #include <net/ip.h>
458 #include <net/ip6_route.h>
459 -#include <net/netfilter/nf_tables.h>
460 #include <net/netfilter/nf_flow_table.h>
461 #include <net/netfilter/nf_conntrack.h>
462 #include <net/netfilter/nf_conntrack_core.h>