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