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