mtd: fix build with GCC 14
[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,701 @@
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 + if (!dst_hold_safe(this_dst))
548 + return -ENOENT;
549 +
550 + nf_route(xt_net(par), &other_dst, &fl, false, xt_family(par));
551 + if (!other_dst) {
552 + dst_release(this_dst);
553 + return -ENOENT;
554 + }
555 +
556 + nf_default_forward_path(route, this_dst, dir, devs);
557 + nf_default_forward_path(route, other_dst, !dir, devs);
558 +
559 + if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
560 + route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
561 + nf_dev_forward_path(route, ct, dir, devs);
562 + nf_dev_forward_path(route, ct, !dir, devs);
563 + }
564 +
565 + return 0;
566 +}
567 +
568 +static unsigned int
569 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
570 +{
571 + struct xt_flowoffload_table *table;
572 + const struct xt_flowoffload_target_info *info = par->targinfo;
573 + struct tcphdr _tcph, *tcph = NULL;
574 + enum ip_conntrack_info ctinfo;
575 + enum ip_conntrack_dir dir;
576 + struct nf_flow_route route = {};
577 + struct flow_offload *flow = NULL;
578 + struct net_device *devs[2] = {};
579 + struct nf_conn *ct;
580 + struct net *net;
581 +
582 + if (xt_flowoffload_skip(skb, xt_family(par)))
583 + return XT_CONTINUE;
584 +
585 + ct = nf_ct_get(skb, &ctinfo);
586 + if (ct == NULL)
587 + return XT_CONTINUE;
588 +
589 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
590 + case IPPROTO_TCP:
591 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
592 + return XT_CONTINUE;
593 +
594 + tcph = skb_header_pointer(skb, par->thoff,
595 + sizeof(_tcph), &_tcph);
596 + if (unlikely(!tcph || tcph->fin || tcph->rst))
597 + return XT_CONTINUE;
598 + break;
599 + case IPPROTO_UDP:
600 + break;
601 + default:
602 + return XT_CONTINUE;
603 + }
604 +
605 + if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
606 + ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
607 + return XT_CONTINUE;
608 +
609 + if (!nf_ct_is_confirmed(ct))
610 + return XT_CONTINUE;
611 +
612 + dir = CTINFO2DIR(ctinfo);
613 +
614 + devs[dir] = xt_out(par);
615 + devs[!dir] = xt_in(par);
616 +
617 + if (!devs[dir] || !devs[!dir])
618 + return XT_CONTINUE;
619 +
620 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
621 + return XT_CONTINUE;
622 +
623 + if (xt_flowoffload_route(skb, ct, par, &route, dir, devs) < 0)
624 + goto err_flow_route;
625 +
626 + flow = flow_offload_alloc(ct);
627 + if (!flow)
628 + goto err_flow_alloc;
629 +
630 + flow_offload_route_init(flow, &route);
631 +
632 + if (tcph) {
633 + ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
634 + ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
635 + }
636 +
637 + table = &flowtable[!!(info->flags & XT_FLOWOFFLOAD_HW)];
638 +
639 + net = read_pnet(&table->ft.net);
640 + if (!net)
641 + write_pnet(&table->ft.net, xt_net(par));
642 +
643 + if (flow_offload_add(&table->ft, flow) < 0)
644 + goto err_flow_add;
645 +
646 + xt_flowoffload_check_device(table, devs[0]);
647 + xt_flowoffload_check_device(table, devs[1]);
648 +
649 + return XT_CONTINUE;
650 +
651 +err_flow_add:
652 + flow_offload_free(flow);
653 +err_flow_alloc:
654 + dst_release(route.tuple[dir].dst);
655 + dst_release(route.tuple[!dir].dst);
656 +err_flow_route:
657 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
658 +
659 + return XT_CONTINUE;
660 +}
661 +
662 +static int flowoffload_chk(const struct xt_tgchk_param *par)
663 +{
664 + struct xt_flowoffload_target_info *info = par->targinfo;
665 +
666 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
667 + return -EINVAL;
668 +
669 + return 0;
670 +}
671 +
672 +static struct xt_target offload_tg_reg __read_mostly = {
673 + .family = NFPROTO_UNSPEC,
674 + .name = "FLOWOFFLOAD",
675 + .revision = 0,
676 + .targetsize = sizeof(struct xt_flowoffload_target_info),
677 + .usersize = sizeof(struct xt_flowoffload_target_info),
678 + .checkentry = flowoffload_chk,
679 + .target = flowoffload_tg,
680 + .me = THIS_MODULE,
681 +};
682 +
683 +static int flow_offload_netdev_event(struct notifier_block *this,
684 + unsigned long event, void *ptr)
685 +{
686 + struct xt_flowoffload_hook *hook0, *hook1;
687 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
688 +
689 + if (event != NETDEV_UNREGISTER)
690 + return NOTIFY_DONE;
691 +
692 + spin_lock_bh(&hooks_lock);
693 + hook0 = flow_offload_lookup_hook(&flowtable[0], dev);
694 + if (hook0)
695 + hlist_del(&hook0->list);
696 +
697 + hook1 = flow_offload_lookup_hook(&flowtable[1], dev);
698 + if (hook1)
699 + hlist_del(&hook1->list);
700 + spin_unlock_bh(&hooks_lock);
701 +
702 + if (hook0) {
703 + nf_unregister_net_hook(hook0->net, &hook0->ops);
704 + kfree(hook0);
705 + }
706 +
707 + if (hook1) {
708 + nf_unregister_net_hook(hook1->net, &hook1->ops);
709 + kfree(hook1);
710 + }
711 +
712 + nf_flow_table_cleanup(dev);
713 +
714 + return NOTIFY_DONE;
715 +}
716 +
717 +static struct notifier_block flow_offload_netdev_notifier = {
718 + .notifier_call = flow_offload_netdev_event,
719 +};
720 +
721 +static int nf_flow_rule_route_inet(struct net *net,
722 + const struct flow_offload *flow,
723 + enum flow_offload_tuple_dir dir,
724 + struct nf_flow_rule *flow_rule)
725 +{
726 + const struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
727 + int err;
728 +
729 + switch (flow_tuple->l3proto) {
730 + case NFPROTO_IPV4:
731 + err = nf_flow_rule_route_ipv4(net, flow, dir, flow_rule);
732 + break;
733 + case NFPROTO_IPV6:
734 + err = nf_flow_rule_route_ipv6(net, flow, dir, flow_rule);
735 + break;
736 + default:
737 + err = -1;
738 + break;
739 + }
740 +
741 + return err;
742 +}
743 +
744 +static struct nf_flowtable_type flowtable_inet = {
745 + .family = NFPROTO_INET,
746 + .init = nf_flow_table_init,
747 + .setup = nf_flow_table_offload_setup,
748 + .action = nf_flow_rule_route_inet,
749 + .free = nf_flow_table_free,
750 + .hook = xt_flowoffload_net_hook,
751 + .owner = THIS_MODULE,
752 +};
753 +
754 +static int init_flowtable(struct xt_flowoffload_table *tbl)
755 +{
756 + INIT_DELAYED_WORK(&tbl->work, xt_flowoffload_hook_work);
757 + tbl->ft.type = &flowtable_inet;
758 + tbl->ft.flags = NF_FLOWTABLE_COUNTER;
759 +
760 + return nf_flow_table_init(&tbl->ft);
761 +}
762 +
763 +static int __init xt_flowoffload_tg_init(void)
764 +{
765 + int ret;
766 +
767 + register_netdevice_notifier(&flow_offload_netdev_notifier);
768 +
769 + ret = init_flowtable(&flowtable[0]);
770 + if (ret)
771 + return ret;
772 +
773 + ret = init_flowtable(&flowtable[1]);
774 + if (ret)
775 + goto cleanup;
776 +
777 + flowtable[1].ft.flags |= NF_FLOWTABLE_HW_OFFLOAD;
778 +
779 + ret = xt_register_target(&offload_tg_reg);
780 + if (ret)
781 + goto cleanup2;
782 +
783 + return 0;
784 +
785 +cleanup2:
786 + nf_flow_table_free(&flowtable[1].ft);
787 +cleanup:
788 + nf_flow_table_free(&flowtable[0].ft);
789 + return ret;
790 +}
791 +
792 +static void __exit xt_flowoffload_tg_exit(void)
793 +{
794 + xt_unregister_target(&offload_tg_reg);
795 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
796 + nf_flow_table_free(&flowtable[0].ft);
797 + nf_flow_table_free(&flowtable[1].ft);
798 +}
799 +
800 +MODULE_LICENSE("GPL");
801 +module_init(xt_flowoffload_tg_init);
802 +module_exit(xt_flowoffload_tg_exit);
803 --- a/net/netfilter/nf_flow_table_core.c
804 +++ b/net/netfilter/nf_flow_table_core.c
805 @@ -7,7 +7,6 @@
806 #include <linux/netdevice.h>
807 #include <net/ip.h>
808 #include <net/ip6_route.h>
809 -#include <net/netfilter/nf_tables.h>
810 #include <net/netfilter/nf_flow_table.h>
811 #include <net/netfilter/nf_conntrack.h>
812 #include <net/netfilter/nf_conntrack_core.h>
813 @@ -373,8 +372,7 @@ flow_offload_lookup(struct nf_flowtable
814 }
815 EXPORT_SYMBOL_GPL(flow_offload_lookup);
816
817 -static int
818 -nf_flow_table_iterate(struct nf_flowtable *flow_table,
819 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
820 void (*iter)(struct nf_flowtable *flowtable,
821 struct flow_offload *flow, void *data),
822 void *data)
823 @@ -428,6 +426,7 @@ static void nf_flow_offload_gc_step(stru
824 nf_flow_offload_stats(flow_table, flow);
825 }
826 }
827 +EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
828
829 void nf_flow_table_gc_run(struct nf_flowtable *flow_table)
830 {
831 --- /dev/null
832 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
833 @@ -0,0 +1,17 @@
834 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
835 +#ifndef _XT_FLOWOFFLOAD_H
836 +#define _XT_FLOWOFFLOAD_H
837 +
838 +#include <linux/types.h>
839 +
840 +enum {
841 + XT_FLOWOFFLOAD_HW = 1 << 0,
842 +
843 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
844 +};
845 +
846 +struct xt_flowoffload_target_info {
847 + __u32 flags;
848 +};
849 +
850 +#endif /* _XT_FLOWOFFLOAD_H */
851 --- a/include/net/netfilter/nf_flow_table.h
852 +++ b/include/net/netfilter/nf_flow_table.h
853 @@ -276,6 +276,11 @@ void nf_flow_table_free(struct nf_flowta
854
855 void flow_offload_teardown(struct flow_offload *flow);
856
857 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
858 + void (*iter)(struct nf_flowtable *flowtable,
859 + struct flow_offload *flow, void *data),
860 + void *data);
861 +
862 void nf_flow_snat_port(const struct flow_offload *flow,
863 struct sk_buff *skb, unsigned int thoff,
864 u8 protocol, enum flow_offload_tuple_dir dir);