b83a185aebbaf4e97771df5adbf9b5507a67a7b6
[openwrt/openwrt.git] / target / linux / generic / hack-5.10 / 650-netfilter-add-xt_FLOWOFFLOAD-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_FLOWOFFLOAD 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 @@ -56,8 +56,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 @@ -66,6 +64,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 @@ -45,7 +45,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 @@ -55,6 +54,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 @@ -682,8 +682,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 @@ -692,11 +690,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 @@ -976,6 +975,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 @@ -145,6 +145,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,712 @@
102 +/*
103 + * Copyright (C) 2018-2021 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 <linux/if_vlan.h>
114 +#include <linux/if_pppox.h>
115 +#include <linux/ppp_defs.h>
116 +#include <net/ip.h>
117 +#include <net/netfilter/nf_conntrack.h>
118 +#include <net/netfilter/nf_conntrack_extend.h>
119 +#include <net/netfilter/nf_conntrack_helper.h>
120 +#include <net/netfilter/nf_flow_table.h>
121 +
122 +struct xt_flowoffload_hook {
123 + struct hlist_node list;
124 + struct nf_hook_ops ops;
125 + struct net *net;
126 + bool registered;
127 + bool used;
128 +};
129 +
130 +struct xt_flowoffload_table {
131 + struct nf_flowtable ft;
132 + struct hlist_head hooks;
133 + struct delayed_work work;
134 +};
135 +
136 +struct nf_forward_info {
137 + const struct net_device *indev;
138 + const struct net_device *outdev;
139 + const struct net_device *hw_outdev;
140 + struct id {
141 + __u16 id;
142 + __be16 proto;
143 + } encap[NF_FLOW_TABLE_ENCAP_MAX];
144 + u8 num_encaps;
145 + u8 ingress_vlans;
146 + u8 h_source[ETH_ALEN];
147 + u8 h_dest[ETH_ALEN];
148 + enum flow_offload_xmit_type xmit_type;
149 +};
150 +
151 +static DEFINE_SPINLOCK(hooks_lock);
152 +
153 +struct xt_flowoffload_table flowtable[2];
154 +
155 +static inline __be16 nf_flow_pppoe_proto(const struct sk_buff *skb)
156 +{
157 + __be16 proto;
158 +
159 + proto = *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
160 + sizeof(struct pppoe_hdr)));
161 + switch (proto) {
162 + case htons(PPP_IP):
163 + return htons(ETH_P_IP);
164 + case htons(PPP_IPV6):
165 + return htons(ETH_P_IPV6);
166 + }
167 +
168 + return 0;
169 +}
170 +
171 +static unsigned int
172 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
173 + const struct nf_hook_state *state)
174 +{
175 + struct vlan_ethhdr *veth;
176 + __be16 proto;
177 +
178 + switch (skb->protocol) {
179 + case htons(ETH_P_8021Q):
180 + veth = (struct vlan_ethhdr *)skb_mac_header(skb);
181 + proto = veth->h_vlan_encapsulated_proto;
182 + break;
183 + case htons(ETH_P_PPP_SES):
184 + proto = nf_flow_pppoe_proto(skb);
185 + break;
186 + default:
187 + proto = skb->protocol;
188 + break;
189 + }
190 +
191 + switch (proto) {
192 + case htons(ETH_P_IP):
193 + return nf_flow_offload_ip_hook(priv, skb, state);
194 + case htons(ETH_P_IPV6):
195 + return nf_flow_offload_ipv6_hook(priv, skb, state);
196 + }
197 +
198 + return NF_ACCEPT;
199 +}
200 +
201 +static int
202 +xt_flowoffload_create_hook(struct xt_flowoffload_table *table,
203 + struct net_device *dev)
204 +{
205 + struct xt_flowoffload_hook *hook;
206 + struct nf_hook_ops *ops;
207 +
208 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
209 + if (!hook)
210 + return -ENOMEM;
211 +
212 + ops = &hook->ops;
213 + ops->pf = NFPROTO_NETDEV;
214 + ops->hooknum = NF_NETDEV_INGRESS;
215 + ops->priority = 10;
216 + ops->priv = &table->ft;
217 + ops->hook = xt_flowoffload_net_hook;
218 + ops->dev = dev;
219 +
220 + hlist_add_head(&hook->list, &table->hooks);
221 + mod_delayed_work(system_power_efficient_wq, &table->work, 0);
222 +
223 + return 0;
224 +}
225 +
226 +static struct xt_flowoffload_hook *
227 +flow_offload_lookup_hook(struct xt_flowoffload_table *table,
228 + struct net_device *dev)
229 +{
230 + struct xt_flowoffload_hook *hook;
231 +
232 + hlist_for_each_entry(hook, &table->hooks, list) {
233 + if (hook->ops.dev == dev)
234 + return hook;
235 + }
236 +
237 + return NULL;
238 +}
239 +
240 +static void
241 +xt_flowoffload_check_device(struct xt_flowoffload_table *table,
242 + struct net_device *dev)
243 +{
244 + struct xt_flowoffload_hook *hook;
245 +
246 + if (!dev)
247 + return;
248 +
249 + spin_lock_bh(&hooks_lock);
250 + hook = flow_offload_lookup_hook(table, dev);
251 + if (hook)
252 + hook->used = true;
253 + else
254 + xt_flowoffload_create_hook(table, dev);
255 + spin_unlock_bh(&hooks_lock);
256 +}
257 +
258 +static void
259 +xt_flowoffload_register_hooks(struct xt_flowoffload_table *table)
260 +{
261 + struct xt_flowoffload_hook *hook;
262 +
263 +restart:
264 + hlist_for_each_entry(hook, &table->hooks, list) {
265 + if (hook->registered)
266 + continue;
267 +
268 + hook->registered = true;
269 + hook->net = dev_net(hook->ops.dev);
270 + spin_unlock_bh(&hooks_lock);
271 + nf_register_net_hook(hook->net, &hook->ops);
272 + if (table->ft.flags & NF_FLOWTABLE_HW_OFFLOAD)
273 + table->ft.type->setup(&table->ft, hook->ops.dev,
274 + FLOW_BLOCK_BIND);
275 + spin_lock_bh(&hooks_lock);
276 + goto restart;
277 + }
278 +
279 +}
280 +
281 +static bool
282 +xt_flowoffload_cleanup_hooks(struct xt_flowoffload_table *table)
283 +{
284 + struct xt_flowoffload_hook *hook;
285 + bool active = false;
286 +
287 +restart:
288 + spin_lock_bh(&hooks_lock);
289 + hlist_for_each_entry(hook, &table->hooks, list) {
290 + if (hook->used || !hook->registered) {
291 + active = true;
292 + continue;
293 + }
294 +
295 + hlist_del(&hook->list);
296 + spin_unlock_bh(&hooks_lock);
297 + if (table->ft.flags & NF_FLOWTABLE_HW_OFFLOAD)
298 + table->ft.type->setup(&table->ft, hook->ops.dev,
299 + FLOW_BLOCK_UNBIND);
300 + nf_unregister_net_hook(hook->net, &hook->ops);
301 + kfree(hook);
302 + goto restart;
303 + }
304 + spin_unlock_bh(&hooks_lock);
305 +
306 + return active;
307 +}
308 +
309 +static void
310 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
311 +{
312 + struct xt_flowoffload_table *table = data;
313 + struct flow_offload_tuple *tuple0 = &flow->tuplehash[0].tuple;
314 + struct flow_offload_tuple *tuple1 = &flow->tuplehash[1].tuple;
315 + struct xt_flowoffload_hook *hook;
316 +
317 + spin_lock_bh(&hooks_lock);
318 + hlist_for_each_entry(hook, &table->hooks, list) {
319 + if (hook->ops.dev->ifindex != tuple0->iifidx &&
320 + hook->ops.dev->ifindex != tuple1->iifidx)
321 + continue;
322 +
323 + hook->used = true;
324 + }
325 + spin_unlock_bh(&hooks_lock);
326 +}
327 +
328 +static void
329 +xt_flowoffload_hook_work(struct work_struct *work)
330 +{
331 + struct xt_flowoffload_table *table;
332 + struct xt_flowoffload_hook *hook;
333 + int err;
334 +
335 + table = container_of(work, struct xt_flowoffload_table, work.work);
336 +
337 + spin_lock_bh(&hooks_lock);
338 + xt_flowoffload_register_hooks(table);
339 + hlist_for_each_entry(hook, &table->hooks, list)
340 + hook->used = false;
341 + spin_unlock_bh(&hooks_lock);
342 +
343 + err = nf_flow_table_iterate(&table->ft, xt_flowoffload_check_hook,
344 + table);
345 + if (err && err != -EAGAIN)
346 + goto out;
347 +
348 + if (!xt_flowoffload_cleanup_hooks(table))
349 + return;
350 +
351 +out:
352 + queue_delayed_work(system_power_efficient_wq, &table->work, HZ);
353 +}
354 +
355 +static bool
356 +xt_flowoffload_skip(struct sk_buff *skb, int family)
357 +{
358 + if (skb_sec_path(skb))
359 + return true;
360 +
361 + if (family == NFPROTO_IPV4) {
362 + const struct ip_options *opt = &(IPCB(skb)->opt);
363 +
364 + if (unlikely(opt->optlen))
365 + return true;
366 + }
367 +
368 + return false;
369 +}
370 +
371 +static enum flow_offload_xmit_type nf_xmit_type(struct dst_entry *dst)
372 +{
373 + if (dst_xfrm(dst))
374 + return FLOW_OFFLOAD_XMIT_XFRM;
375 +
376 + return FLOW_OFFLOAD_XMIT_NEIGH;
377 +}
378 +
379 +static void nf_default_forward_path(struct nf_flow_route *route,
380 + struct dst_entry *dst_cache,
381 + enum ip_conntrack_dir dir,
382 + struct net_device **dev)
383 +{
384 + dev[!dir] = dst_cache->dev;
385 + route->tuple[!dir].in.ifindex = dst_cache->dev->ifindex;
386 + route->tuple[dir].dst = dst_cache;
387 + route->tuple[dir].xmit_type = nf_xmit_type(dst_cache);
388 +}
389 +
390 +static bool nf_is_valid_ether_device(const struct net_device *dev)
391 +{
392 + if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
393 + dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
394 + return false;
395 +
396 + return true;
397 +}
398 +
399 +static void nf_dev_path_info(const struct net_device_path_stack *stack,
400 + struct nf_forward_info *info,
401 + unsigned char *ha)
402 +{
403 + const struct net_device_path *path;
404 + int i;
405 +
406 + memcpy(info->h_dest, ha, ETH_ALEN);
407 +
408 + for (i = 0; i < stack->num_paths; i++) {
409 + path = &stack->path[i];
410 + switch (path->type) {
411 + case DEV_PATH_ETHERNET:
412 + case DEV_PATH_DSA:
413 + case DEV_PATH_VLAN:
414 + case DEV_PATH_PPPOE:
415 + info->indev = path->dev;
416 + if (is_zero_ether_addr(info->h_source))
417 + memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
418 +
419 + if (path->type == DEV_PATH_ETHERNET)
420 + break;
421 + if (path->type == DEV_PATH_DSA) {
422 + i = stack->num_paths;
423 + break;
424 + }
425 +
426 + /* DEV_PATH_VLAN and DEV_PATH_PPPOE */
427 + if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
428 + info->indev = NULL;
429 + break;
430 + }
431 + if (!info->outdev)
432 + info->outdev = path->dev;
433 + info->encap[info->num_encaps].id = path->encap.id;
434 + info->encap[info->num_encaps].proto = path->encap.proto;
435 + info->num_encaps++;
436 + if (path->type == DEV_PATH_PPPOE)
437 + memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
438 + break;
439 + case DEV_PATH_BRIDGE:
440 + if (is_zero_ether_addr(info->h_source))
441 + memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
442 +
443 + switch (path->bridge.vlan_mode) {
444 + case DEV_PATH_BR_VLAN_UNTAG_HW:
445 + info->ingress_vlans |= BIT(info->num_encaps - 1);
446 + break;
447 + case DEV_PATH_BR_VLAN_TAG:
448 + info->encap[info->num_encaps].id = path->bridge.vlan_id;
449 + info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
450 + info->num_encaps++;
451 + break;
452 + case DEV_PATH_BR_VLAN_UNTAG:
453 + info->num_encaps--;
454 + break;
455 + case DEV_PATH_BR_VLAN_KEEP:
456 + break;
457 + }
458 + break;
459 + default:
460 + info->indev = NULL;
461 + break;
462 + }
463 + }
464 + if (!info->outdev)
465 + info->outdev = info->indev;
466 +
467 + info->hw_outdev = info->indev;
468 +
469 + if (nf_is_valid_ether_device(info->indev))
470 + info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
471 +}
472 +
473 +static int nf_dev_fill_forward_path(const struct nf_flow_route *route,
474 + const struct dst_entry *dst_cache,
475 + const struct nf_conn *ct,
476 + enum ip_conntrack_dir dir, u8 *ha,
477 + struct net_device_path_stack *stack)
478 +{
479 + const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
480 + struct net_device *dev = dst_cache->dev;
481 + struct neighbour *n;
482 + u8 nud_state;
483 +
484 + if (!nf_is_valid_ether_device(dev))
485 + goto out;
486 +
487 + n = dst_neigh_lookup(dst_cache, daddr);
488 + if (!n)
489 + return -1;
490 +
491 + read_lock_bh(&n->lock);
492 + nud_state = n->nud_state;
493 + ether_addr_copy(ha, n->ha);
494 + read_unlock_bh(&n->lock);
495 + neigh_release(n);
496 +
497 + if (!(nud_state & NUD_VALID))
498 + return -1;
499 +
500 +out:
501 + return dev_fill_forward_path(dev, ha, stack);
502 +}
503 +
504 +static void nf_dev_forward_path(struct nf_flow_route *route,
505 + const struct nf_conn *ct,
506 + enum ip_conntrack_dir dir,
507 + struct net_device **devs)
508 +{
509 + const struct dst_entry *dst = route->tuple[dir].dst;
510 + struct net_device_path_stack stack;
511 + struct nf_forward_info info = {};
512 + unsigned char ha[ETH_ALEN];
513 + int i;
514 +
515 + if (nf_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
516 + nf_dev_path_info(&stack, &info, ha);
517 +
518 + devs[!dir] = (struct net_device *)info.indev;
519 + if (!info.indev)
520 + return;
521 +
522 + route->tuple[!dir].in.ifindex = info.indev->ifindex;
523 + for (i = 0; i < info.num_encaps; i++) {
524 + route->tuple[!dir].in.encap[i].id = info.encap[i].id;
525 + route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
526 + }
527 + route->tuple[!dir].in.num_encaps = info.num_encaps;
528 + route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
529 +
530 + if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
531 + memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
532 + memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
533 + route->tuple[dir].out.ifindex = info.outdev->ifindex;
534 + route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
535 + route->tuple[dir].xmit_type = info.xmit_type;
536 + }
537 +}
538 +
539 +static int
540 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
541 + const struct xt_action_param *par,
542 + struct nf_flow_route *route, enum ip_conntrack_dir dir,
543 + struct net_device **devs)
544 +{
545 + struct dst_entry *this_dst = skb_dst(skb);
546 + struct dst_entry *other_dst = NULL;
547 + struct flowi fl;
548 +
549 + memset(&fl, 0, sizeof(fl));
550 + switch (xt_family(par)) {
551 + case NFPROTO_IPV4:
552 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
553 + fl.u.ip4.flowi4_oif = xt_in(par)->ifindex;
554 + break;
555 + case NFPROTO_IPV6:
556 + fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
557 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
558 + fl.u.ip6.flowi6_oif = xt_in(par)->ifindex;
559 + break;
560 + }
561 +
562 + nf_route(xt_net(par), &other_dst, &fl, false, xt_family(par));
563 + if (!other_dst)
564 + return -ENOENT;
565 +
566 + nf_default_forward_path(route, this_dst, dir, devs);
567 + nf_default_forward_path(route, other_dst, !dir, devs);
568 +
569 + if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
570 + route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
571 + nf_dev_forward_path(route, ct, dir, devs);
572 + nf_dev_forward_path(route, ct, !dir, devs);
573 + }
574 +
575 + return 0;
576 +}
577 +
578 +static unsigned int
579 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
580 +{
581 + struct xt_flowoffload_table *table;
582 + const struct xt_flowoffload_target_info *info = par->targinfo;
583 + struct tcphdr _tcph, *tcph = NULL;
584 + enum ip_conntrack_info ctinfo;
585 + enum ip_conntrack_dir dir;
586 + struct nf_flow_route route = {};
587 + struct flow_offload *flow = NULL;
588 + struct net_device *devs[2] = {};
589 + struct nf_conn *ct;
590 + struct net *net;
591 +
592 + if (xt_flowoffload_skip(skb, xt_family(par)))
593 + return XT_CONTINUE;
594 +
595 + ct = nf_ct_get(skb, &ctinfo);
596 + if (ct == NULL)
597 + return XT_CONTINUE;
598 +
599 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
600 + case IPPROTO_TCP:
601 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
602 + return XT_CONTINUE;
603 +
604 + tcph = skb_header_pointer(skb, par->thoff,
605 + sizeof(_tcph), &_tcph);
606 + if (unlikely(!tcph || tcph->fin || tcph->rst))
607 + return XT_CONTINUE;
608 + break;
609 + case IPPROTO_UDP:
610 + break;
611 + default:
612 + return XT_CONTINUE;
613 + }
614 +
615 + if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
616 + ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
617 + return XT_CONTINUE;
618 +
619 + if (!nf_ct_is_confirmed(ct))
620 + return XT_CONTINUE;
621 +
622 + devs[dir] = xt_out(par);
623 + devs[!dir] = xt_in(par);
624 +
625 + if (!devs[dir] || !devs[!dir])
626 + return XT_CONTINUE;
627 +
628 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
629 + return XT_CONTINUE;
630 +
631 + dir = CTINFO2DIR(ctinfo);
632 +
633 + if (xt_flowoffload_route(skb, ct, par, &route, dir, devs) < 0)
634 + goto err_flow_route;
635 +
636 + flow = flow_offload_alloc(ct);
637 + if (!flow)
638 + goto err_flow_alloc;
639 +
640 + if (flow_offload_route_init(flow, &route) < 0)
641 + goto err_flow_add;
642 +
643 + if (tcph) {
644 + ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
645 + ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
646 + }
647 +
648 + table = &flowtable[!!(info->flags & XT_FLOWOFFLOAD_HW)];
649 +
650 + net = read_pnet(&table->ft.net);
651 + if (!net)
652 + write_pnet(&table->ft.net, xt_net(par));
653 +
654 + if (flow_offload_add(&table->ft, flow) < 0)
655 + goto err_flow_add;
656 +
657 + xt_flowoffload_check_device(table, devs[0]);
658 + xt_flowoffload_check_device(table, devs[1]);
659 +
660 + dst_release(route.tuple[!dir].dst);
661 +
662 + return XT_CONTINUE;
663 +
664 +err_flow_add:
665 + flow_offload_free(flow);
666 +err_flow_alloc:
667 + dst_release(route.tuple[!dir].dst);
668 +err_flow_route:
669 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
670 +
671 + return XT_CONTINUE;
672 +}
673 +
674 +static int flowoffload_chk(const struct xt_tgchk_param *par)
675 +{
676 + struct xt_flowoffload_target_info *info = par->targinfo;
677 +
678 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
679 + return -EINVAL;
680 +
681 + return 0;
682 +}
683 +
684 +static struct xt_target offload_tg_reg __read_mostly = {
685 + .family = NFPROTO_UNSPEC,
686 + .name = "FLOWOFFLOAD",
687 + .revision = 0,
688 + .targetsize = sizeof(struct xt_flowoffload_target_info),
689 + .usersize = sizeof(struct xt_flowoffload_target_info),
690 + .checkentry = flowoffload_chk,
691 + .target = flowoffload_tg,
692 + .me = THIS_MODULE,
693 +};
694 +
695 +static int flow_offload_netdev_event(struct notifier_block *this,
696 + unsigned long event, void *ptr)
697 +{
698 + struct xt_flowoffload_hook *hook0, *hook1;
699 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
700 +
701 + if (event != NETDEV_UNREGISTER)
702 + return NOTIFY_DONE;
703 +
704 + spin_lock_bh(&hooks_lock);
705 + hook0 = flow_offload_lookup_hook(&flowtable[0], dev);
706 + if (hook0)
707 + hlist_del(&hook0->list);
708 +
709 + hook1 = flow_offload_lookup_hook(&flowtable[1], dev);
710 + if (hook1)
711 + hlist_del(&hook1->list);
712 + spin_unlock_bh(&hooks_lock);
713 +
714 + if (hook0) {
715 + nf_unregister_net_hook(hook0->net, &hook0->ops);
716 + kfree(hook0);
717 + }
718 +
719 + if (hook1) {
720 + nf_unregister_net_hook(hook1->net, &hook1->ops);
721 + kfree(hook1);
722 + }
723 +
724 + nf_flow_table_cleanup(dev);
725 +
726 + return NOTIFY_DONE;
727 +}
728 +
729 +static struct notifier_block flow_offload_netdev_notifier = {
730 + .notifier_call = flow_offload_netdev_event,
731 +};
732 +
733 +static int nf_flow_rule_route_inet(struct net *net,
734 + const struct flow_offload *flow,
735 + enum flow_offload_tuple_dir dir,
736 + struct nf_flow_rule *flow_rule)
737 +{
738 + const struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
739 + int err;
740 +
741 + switch (flow_tuple->l3proto) {
742 + case NFPROTO_IPV4:
743 + err = nf_flow_rule_route_ipv4(net, flow, dir, flow_rule);
744 + break;
745 + case NFPROTO_IPV6:
746 + err = nf_flow_rule_route_ipv6(net, flow, dir, flow_rule);
747 + break;
748 + default:
749 + err = -1;
750 + break;
751 + }
752 +
753 + return err;
754 +}
755 +
756 +static struct nf_flowtable_type flowtable_inet = {
757 + .family = NFPROTO_INET,
758 + .init = nf_flow_table_init,
759 + .setup = nf_flow_table_offload_setup,
760 + .action = nf_flow_rule_route_inet,
761 + .free = nf_flow_table_free,
762 + .hook = xt_flowoffload_net_hook,
763 + .owner = THIS_MODULE,
764 +};
765 +
766 +static int init_flowtable(struct xt_flowoffload_table *tbl)
767 +{
768 + INIT_DELAYED_WORK(&tbl->work, xt_flowoffload_hook_work);
769 + tbl->ft.type = &flowtable_inet;
770 +
771 + return nf_flow_table_init(&tbl->ft);
772 +}
773 +
774 +static int __init xt_flowoffload_tg_init(void)
775 +{
776 + int ret;
777 +
778 + register_netdevice_notifier(&flow_offload_netdev_notifier);
779 +
780 + ret = init_flowtable(&flowtable[0]);
781 + if (ret)
782 + return ret;
783 +
784 + ret = init_flowtable(&flowtable[1]);
785 + if (ret)
786 + goto cleanup;
787 +
788 + flowtable[1].ft.flags = NF_FLOWTABLE_HW_OFFLOAD;
789 +
790 + ret = xt_register_target(&offload_tg_reg);
791 + if (ret)
792 + goto cleanup2;
793 +
794 + return 0;
795 +
796 +cleanup2:
797 + nf_flow_table_free(&flowtable[1].ft);
798 +cleanup:
799 + nf_flow_table_free(&flowtable[0].ft);
800 + return ret;
801 +}
802 +
803 +static void __exit xt_flowoffload_tg_exit(void)
804 +{
805 + xt_unregister_target(&offload_tg_reg);
806 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
807 + nf_flow_table_free(&flowtable[0].ft);
808 + nf_flow_table_free(&flowtable[1].ft);
809 +}
810 +
811 +MODULE_LICENSE("GPL");
812 +module_init(xt_flowoffload_tg_init);
813 +module_exit(xt_flowoffload_tg_exit);
814 --- a/net/netfilter/nf_flow_table_core.c
815 +++ b/net/netfilter/nf_flow_table_core.c
816 @@ -7,7 +7,6 @@
817 #include <linux/netdevice.h>
818 #include <net/ip.h>
819 #include <net/ip6_route.h>
820 -#include <net/netfilter/nf_tables.h>
821 #include <net/netfilter/nf_flow_table.h>
822 #include <net/netfilter/nf_conntrack.h>
823 #include <net/netfilter/nf_conntrack_core.h>
824 @@ -380,8 +379,7 @@ flow_offload_lookup(struct nf_flowtable
825 }
826 EXPORT_SYMBOL_GPL(flow_offload_lookup);
827
828 -static int
829 -nf_flow_table_iterate(struct nf_flowtable *flow_table,
830 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
831 void (*iter)(struct flow_offload *flow, void *data),
832 void *data)
833 {
834 @@ -413,6 +411,7 @@ nf_flow_table_iterate(struct nf_flowtabl
835
836 return err;
837 }
838 +EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
839
840 static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
841 {
842 --- /dev/null
843 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
844 @@ -0,0 +1,17 @@
845 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
846 +#ifndef _XT_FLOWOFFLOAD_H
847 +#define _XT_FLOWOFFLOAD_H
848 +
849 +#include <linux/types.h>
850 +
851 +enum {
852 + XT_FLOWOFFLOAD_HW = 1 << 0,
853 +
854 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
855 +};
856 +
857 +struct xt_flowoffload_target_info {
858 + __u32 flags;
859 +};
860 +
861 +#endif /* _XT_FLOWOFFLOAD_H */
862 --- a/include/net/netfilter/nf_flow_table.h
863 +++ b/include/net/netfilter/nf_flow_table.h
864 @@ -271,6 +271,10 @@ void nf_flow_table_free(struct nf_flowta
865
866 void flow_offload_teardown(struct flow_offload *flow);
867
868 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
869 + void (*iter)(struct flow_offload *flow, void *data),
870 + void *data);
871 +
872 void nf_flow_snat_port(const struct flow_offload *flow,
873 struct sk_buff *skb, unsigned int thoff,
874 u8 protocol, enum flow_offload_tuple_dir dir);