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