kernel: port upstream nft_flow_offload changes to xt_FLOWOFFLOAD and fix routing...
[librecmc/librecmc.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 @@ -76,8 +76,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 @@ -86,6 +84,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 @@ -97,7 +97,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 @@ -107,6 +106,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 @@ -671,8 +671,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 @@ -681,11 +679,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 @@ -974,6 +973,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 @@ -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
99 --- /dev/null
100 +++ b/net/netfilter/xt_FLOWOFFLOAD.c
101 @@ -0,0 +1,421 @@
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 <linux/netfilter/xt_FLOWOFFLOAD.h>
113 +#include <net/ip.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>
118 +
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;
123 +
124 +struct xt_flowoffload_hook {
125 +       struct hlist_node list;
126 +       struct nf_hook_ops ops;
127 +       struct net *net;
128 +       bool registered;
129 +       bool used;
130 +};
131 +
132 +static unsigned int
133 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
134 +                         const struct nf_hook_state *state)
135 +{
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);
141 +       }
142 +
143 +       return NF_ACCEPT;
144 +}
145 +
146 +static int
147 +xt_flowoffload_create_hook(struct net_device *dev)
148 +{
149 +       struct xt_flowoffload_hook *hook;
150 +       struct nf_hook_ops *ops;
151 +
152 +       hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
153 +       if (!hook)
154 +               return -ENOMEM;
155 +
156 +       ops = &hook->ops;
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;
162 +       ops->dev = dev;
163 +
164 +       hlist_add_head(&hook->list, &hooks);
165 +       mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
166 +
167 +       return 0;
168 +}
169 +
170 +static struct xt_flowoffload_hook *
171 +flow_offload_lookup_hook(struct net_device *dev)
172 +{
173 +       struct xt_flowoffload_hook *hook;
174 +
175 +       hlist_for_each_entry(hook, &hooks, list) {
176 +               if (hook->ops.dev == dev)
177 +                       return hook;
178 +       }
179 +
180 +       return NULL;
181 +}
182 +
183 +static void
184 +xt_flowoffload_check_device(struct net_device *dev)
185 +{
186 +       struct xt_flowoffload_hook *hook;
187 +
188 +       spin_lock_bh(&hooks_lock);
189 +       hook = flow_offload_lookup_hook(dev);
190 +       if (hook)
191 +               hook->used = true;
192 +       else
193 +               xt_flowoffload_create_hook(dev);
194 +       spin_unlock_bh(&hooks_lock);
195 +}
196 +
197 +static void
198 +xt_flowoffload_register_hooks(void)
199 +{
200 +       struct xt_flowoffload_hook *hook;
201 +
202 +restart:
203 +       hlist_for_each_entry(hook, &hooks, list) {
204 +               if (hook->registered)
205 +                       continue;
206 +
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);
212 +               goto restart;
213 +       }
214 +
215 +}
216 +
217 +static void
218 +xt_flowoffload_cleanup_hooks(void)
219 +{
220 +       struct xt_flowoffload_hook *hook;
221 +
222 +restart:
223 +       hlist_for_each_entry(hook, &hooks, list) {
224 +               if (hook->used || !hook->registered)
225 +                       continue;
226 +
227 +               hlist_del(&hook->list);
228 +               spin_unlock_bh(&hooks_lock);
229 +               nf_unregister_net_hook(hook->net, &hook->ops);
230 +               kfree(hook);
231 +               spin_lock_bh(&hooks_lock);
232 +               goto restart;
233 +       }
234 +
235 +}
236 +
237 +static void
238 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
239 +{
240 +       struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
241 +       struct xt_flowoffload_hook *hook;
242 +       bool *found = data;
243 +
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)
248 +                       continue;
249 +
250 +               hook->used = true;
251 +               *found = true;
252 +       }
253 +       spin_unlock_bh(&hooks_lock);
254 +}
255 +
256 +static void
257 +xt_flowoffload_hook_work(struct work_struct *work)
258 +{
259 +       struct xt_flowoffload_hook *hook;
260 +       bool found = false;
261 +       int err;
262 +
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);
268 +
269 +       err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
270 +                                   &found);
271 +       if (err && err != -EAGAIN)
272 +           goto out;
273 +
274 +       spin_lock_bh(&hooks_lock);
275 +       xt_flowoffload_cleanup_hooks();
276 +       spin_unlock_bh(&hooks_lock);
277 +
278 +out:
279 +       if (found)
280 +               queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
281 +}
282 +
283 +static bool
284 +xt_flowoffload_skip(struct sk_buff *skb, int family)
285 +{
286 +       if (skb_sec_path(skb))
287 +               return true;
288 +
289 +       if (family == NFPROTO_IPV4) {
290 +               const struct ip_options *opt = &(IPCB(skb)->opt);
291 +
292 +               if (unlikely(opt->optlen))
293 +                       return true;
294 +       }
295 +
296 +       return false;
297 +}
298 +
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)
302 +{
303 +       struct dst_entry *dst = NULL;
304 +       struct flowi fl;
305 +
306 +       memset(&fl, 0, sizeof(fl));
307 +       switch (xt_family(par)) {
308 +       case NFPROTO_IPV4:
309 +               fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
310 +               fl.u.ip4.flowi4_oif = ifindex;
311 +               break;
312 +       case NFPROTO_IPV6:
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;
316 +               break;
317 +       }
318 +
319 +       nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
320 +
321 +       return dst;
322 +}
323 +
324 +static int
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)
328 +{
329 +       struct dst_entry *this_dst, *other_dst;
330 +
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);
333 +       if (!this_dst || !other_dst)
334 +               return -ENOENT;
335 +
336 +       if (dst_xfrm(this_dst) || dst_xfrm(other_dst))
337 +               return -EINVAL;
338 +
339 +       route->tuple[dir].dst           = this_dst;
340 +       route->tuple[!dir].dst          = other_dst;
341 +
342 +       return 0;
343 +}
344 +
345 +static unsigned int
346 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
347 +{
348 +       const struct xt_flowoffload_target_info *info = par->targinfo;
349 +       struct tcphdr _tcph, *tcph = NULL;
350 +       enum ip_conntrack_info ctinfo;
351 +       enum ip_conntrack_dir dir;
352 +       struct nf_flow_route route;
353 +       struct flow_offload *flow;
354 +       struct nf_conn *ct;
355 +       struct net *net;
356 +
357 +       if (xt_flowoffload_skip(skb, xt_family(par)))
358 +               return XT_CONTINUE;
359 +
360 +       ct = nf_ct_get(skb, &ctinfo);
361 +       if (ct == NULL)
362 +               return XT_CONTINUE;
363 +
364 +       switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
365 +       case IPPROTO_TCP:
366 +               if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
367 +                       return XT_CONTINUE;
368 +
369 +               tcph = skb_header_pointer(skb, par->thoff,
370 +                                         sizeof(_tcph), &_tcph);
371 +               if (unlikely(!tcph || tcph->fin || tcph->rst))
372 +                       return XT_CONTINUE;
373 +               break;
374 +       case IPPROTO_UDP:
375 +               break;
376 +       default:
377 +               return XT_CONTINUE;
378 +       }
379 +
380 +       if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
381 +           ct->status & IPS_SEQ_ADJUST)
382 +               return XT_CONTINUE;
383 +
384 +       if (!nf_ct_is_confirmed(ct))
385 +               return XT_CONTINUE;
386 +
387 +       if (!xt_in(par) || !xt_out(par))
388 +               return XT_CONTINUE;
389 +
390 +       if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
391 +               return XT_CONTINUE;
392 +
393 +       dir = CTINFO2DIR(ctinfo);
394 +
395 +       if (xt_flowoffload_route(skb, ct, par, &route, dir) < 0)
396 +               goto err_flow_route;
397 +
398 +       flow = flow_offload_alloc(ct, &route);
399 +       if (!flow)
400 +               goto err_flow_alloc;
401 +
402 +       if (tcph) {
403 +               ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
404 +               ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
405 +       }
406 +
407 +       if (flow_offload_add(&nf_flowtable, flow) < 0)
408 +               goto err_flow_add;
409 +
410 +       xt_flowoffload_check_device(xt_in(par));
411 +       xt_flowoffload_check_device(xt_out(par));
412 +
413 +       net = read_pnet(&nf_flowtable.ft_net);
414 +       if (!net)
415 +               write_pnet(&nf_flowtable.ft_net, xt_net(par));
416 +
417 +       if (info->flags & XT_FLOWOFFLOAD_HW)
418 +               nf_flow_offload_hw_add(xt_net(par), flow, ct);
419 +
420 +       return XT_CONTINUE;
421 +
422 +err_flow_add:
423 +       flow_offload_free(flow);
424 +err_flow_alloc:
425 +       dst_release(route.tuple[!dir].dst);
426 +err_flow_route:
427 +       clear_bit(IPS_OFFLOAD_BIT, &ct->status);
428 +       return XT_CONTINUE;
429 +}
430 +
431 +
432 +static int flowoffload_chk(const struct xt_tgchk_param *par)
433 +{
434 +       struct xt_flowoffload_target_info *info = par->targinfo;
435 +
436 +       if (info->flags & ~XT_FLOWOFFLOAD_MASK)
437 +               return -EINVAL;
438 +
439 +       return 0;
440 +}
441 +
442 +static struct xt_target offload_tg_reg __read_mostly = {
443 +       .family         = NFPROTO_UNSPEC,
444 +       .name           = "FLOWOFFLOAD",
445 +       .revision       = 0,
446 +       .targetsize     = sizeof(struct xt_flowoffload_target_info),
447 +       .usersize       = sizeof(struct xt_flowoffload_target_info),
448 +       .checkentry     = flowoffload_chk,
449 +       .target         = flowoffload_tg,
450 +       .me             = THIS_MODULE,
451 +};
452 +
453 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
454 +{
455 +       table->flags = NF_FLOWTABLE_F_HW;
456 +       nf_flow_table_init(table);
457 +       return 0;
458 +}
459 +
460 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
461 +{
462 +       nf_flow_table_free(table);
463 +}
464 +
465 +static int flow_offload_netdev_event(struct notifier_block *this,
466 +                                    unsigned long event, void *ptr)
467 +{
468 +       struct xt_flowoffload_hook *hook = NULL;
469 +       struct net_device *dev = netdev_notifier_info_to_dev(ptr);
470 +
471 +       if (event != NETDEV_UNREGISTER)
472 +               return NOTIFY_DONE;
473 +
474 +       spin_lock_bh(&hooks_lock);
475 +       hook = flow_offload_lookup_hook(dev);
476 +       if (hook) {
477 +               hlist_del(&hook->list);
478 +       }
479 +       spin_unlock_bh(&hooks_lock);
480 +       if (hook) {
481 +               nf_unregister_net_hook(hook->net, &hook->ops);
482 +               kfree(hook);
483 +       }
484 +
485 +       nf_flow_table_cleanup(dev_net(dev), dev);
486 +
487 +       return NOTIFY_DONE;
488 +}
489 +
490 +static struct notifier_block flow_offload_netdev_notifier = {
491 +       .notifier_call  = flow_offload_netdev_event,
492 +};
493 +
494 +static int __init xt_flowoffload_tg_init(void)
495 +{
496 +       int ret;
497 +
498 +       register_netdevice_notifier(&flow_offload_netdev_notifier);
499 +
500 +       INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
501 +
502 +       ret = xt_flowoffload_table_init(&nf_flowtable);
503 +       if (ret)
504 +               return ret;
505 +
506 +       ret = xt_register_target(&offload_tg_reg);
507 +       if (ret)
508 +               xt_flowoffload_table_cleanup(&nf_flowtable);
509 +
510 +       return ret;
511 +}
512 +
513 +static void __exit xt_flowoffload_tg_exit(void)
514 +{
515 +       xt_unregister_target(&offload_tg_reg);
516 +       xt_flowoffload_table_cleanup(&nf_flowtable);
517 +       unregister_netdevice_notifier(&flow_offload_netdev_notifier);
518 +}
519 +
520 +MODULE_LICENSE("GPL");
521 +module_init(xt_flowoffload_tg_init);
522 +module_exit(xt_flowoffload_tg_exit);
523 --- a/net/netfilter/nf_flow_table_core.c
524 +++ b/net/netfilter/nf_flow_table_core.c
525 @@ -6,7 +6,6 @@
526  #include <linux/netdevice.h>
527  #include <net/ip.h>
528  #include <net/ip6_route.h>
529 -#include <net/netfilter/nf_tables.h>
530  #include <net/netfilter/nf_flow_table.h>
531  #include <net/netfilter/nf_conntrack.h>
532  #include <net/netfilter/nf_conntrack_core.h>
533 --- /dev/null
534 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
535 @@ -0,0 +1,17 @@
536 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
537 +#ifndef _XT_FLOWOFFLOAD_H
538 +#define _XT_FLOWOFFLOAD_H
539 +
540 +#include <linux/types.h>
541 +
542 +enum {
543 +       XT_FLOWOFFLOAD_HW       = 1 << 0,
544 +
545 +       XT_FLOWOFFLOAD_MASK     = XT_FLOWOFFLOAD_HW
546 +};
547 +
548 +struct xt_flowoffload_target_info {
549 +       __u32 flags;
550 +};
551 +
552 +#endif /* _XT_FLOWOFFLOAD_H */