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