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