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
5 Signed-off-by: Felix Fietkau <nbd@nbd.name>
7 create mode 100644 net/netfilter/xt_OFFLOAD.c
9 --- a/net/ipv4/netfilter/Kconfig
10 +++ b/net/ipv4/netfilter/Kconfig
11 @@ -56,8 +56,6 @@ config NF_TABLES_ARP
13 This option enables the ARP support for nf_tables.
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
22 To compile it as a module, choose M here.
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.
34 endif # NF_TABLES_IPV6
37 config NF_FLOW_TABLE_IPV6
38 tristate "Netfilter flow table IPv6 module"
39 @@ -55,6 +54,8 @@ config NF_FLOW_TABLE_IPV6
41 To compile it as a module, choose M here.
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 @@ -702,8 +702,6 @@ config NFT_FIB_NETDEV
52 endif # NF_TABLES_NETDEV
56 config NF_FLOW_TABLE_INET
57 tristate "Netfilter flow table mixed IPv4/IPv6 module"
58 depends on NF_FLOW_TABLE
59 @@ -712,11 +710,12 @@ config NF_FLOW_TABLE_INET
61 To compile it as a module, choose M here.
66 tristate "Netfilter flow table module"
67 depends on NETFILTER_INGRESS
68 depends on NF_CONNTRACK
69 - depends on NF_TABLES
71 This option adds the flow table core infrastructure.
73 @@ -1005,6 +1004,15 @@ config NETFILTER_XT_TARGET_NOTRACK
74 depends on NETFILTER_ADVANCED
75 select NETFILTER_XT_TARGET_CT
77 +config NETFILTER_XT_TARGET_FLOWOFFLOAD
78 + tristate '"FLOWOFFLOAD" target support'
79 + depends on NF_FLOW_TABLE
80 + depends on NETFILTER_INGRESS
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
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 @@ -144,6 +144,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
100 +++ b/net/netfilter/xt_FLOWOFFLOAD.c
103 + * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
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.
109 +#include <linux/module.h>
110 +#include <linux/init.h>
111 +#include <linux/netfilter.h>
112 +#include <linux/netfilter/xt_FLOWOFFLOAD.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>
119 +static struct nf_flowtable nf_flowtable;
120 +static HLIST_HEAD(hooks);
121 +static DEFINE_SPINLOCK(hooks_lock);
122 +static struct delayed_work hook_work;
124 +struct xt_flowoffload_hook {
125 + struct hlist_node list;
126 + struct nf_hook_ops ops;
133 +xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
134 + const struct nf_hook_state *state)
136 + switch (skb->protocol) {
137 + case htons(ETH_P_IP):
138 + return nf_flow_offload_ip_hook(priv, skb, state);
139 + case htons(ETH_P_IPV6):
140 + return nf_flow_offload_ipv6_hook(priv, skb, state);
146 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
147 + void (*iter)(struct flow_offload *flow, void *data),
151 +xt_flowoffload_create_hook(struct net_device *dev)
153 + struct xt_flowoffload_hook *hook;
154 + struct nf_hook_ops *ops;
156 + hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
161 + ops->pf = NFPROTO_NETDEV;
162 + ops->hooknum = NF_NETDEV_INGRESS;
163 + ops->priority = 10;
164 + ops->priv = &nf_flowtable;
165 + ops->hook = xt_flowoffload_net_hook;
168 + hlist_add_head(&hook->list, &hooks);
169 + mod_delayed_work(system_power_efficient_wq, &hook_work, 0);
174 +static struct xt_flowoffload_hook *
175 +flow_offload_lookup_hook(struct net_device *dev)
177 + struct xt_flowoffload_hook *hook;
179 + hlist_for_each_entry(hook, &hooks, list) {
180 + if (hook->ops.dev == dev)
188 +xt_flowoffload_check_device(struct net_device *dev)
190 + struct xt_flowoffload_hook *hook;
192 + spin_lock_bh(&hooks_lock);
193 + hook = flow_offload_lookup_hook(dev);
197 + xt_flowoffload_create_hook(dev);
198 + spin_unlock_bh(&hooks_lock);
202 +xt_flowoffload_register_hooks(void)
204 + struct xt_flowoffload_hook *hook;
207 + hlist_for_each_entry(hook, &hooks, list) {
208 + if (hook->registered)
211 + hook->registered = true;
212 + hook->net = dev_net(hook->ops.dev);
213 + spin_unlock_bh(&hooks_lock);
214 + nf_register_net_hook(hook->net, &hook->ops);
215 + spin_lock_bh(&hooks_lock);
222 +xt_flowoffload_cleanup_hooks(void)
224 + struct xt_flowoffload_hook *hook;
227 + hlist_for_each_entry(hook, &hooks, list) {
228 + if (hook->used || !hook->registered)
231 + hlist_del(&hook->list);
232 + spin_unlock_bh(&hooks_lock);
233 + nf_unregister_net_hook(hook->net, &hook->ops);
235 + spin_lock_bh(&hooks_lock);
242 +xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
244 + struct flow_offload_tuple *tuple = &flow->tuplehash[0].tuple;
245 + struct xt_flowoffload_hook *hook;
246 + bool *found = data;
247 + struct rtable *rt = (struct rtable *)tuple->dst_cache;
249 + spin_lock_bh(&hooks_lock);
250 + hlist_for_each_entry(hook, &hooks, list) {
251 + if (hook->ops.dev->ifindex != tuple->iifidx &&
252 + hook->ops.dev->ifindex != rt->dst.dev->ifindex)
258 + spin_unlock_bh(&hooks_lock);
262 +xt_flowoffload_hook_work(struct work_struct *work)
264 + struct xt_flowoffload_hook *hook;
265 + bool found = false;
268 + spin_lock_bh(&hooks_lock);
269 + xt_flowoffload_register_hooks();
270 + hlist_for_each_entry(hook, &hooks, list)
271 + hook->used = false;
272 + spin_unlock_bh(&hooks_lock);
274 + err = nf_flow_table_iterate(&nf_flowtable, xt_flowoffload_check_hook,
276 + if (err && err != -EAGAIN)
279 + spin_lock_bh(&hooks_lock);
280 + xt_flowoffload_cleanup_hooks();
281 + spin_unlock_bh(&hooks_lock);
285 + queue_delayed_work(system_power_efficient_wq, &hook_work, HZ);
289 +xt_flowoffload_skip(struct sk_buff *skb, int family)
291 + if (skb_sec_path(skb))
294 + if (family == NFPROTO_IPV4) {
295 + const struct ip_options *opt = &(IPCB(skb)->opt);
297 + if (unlikely(opt->optlen))
304 +static struct dst_entry *
305 +xt_flowoffload_dst(const struct nf_conn *ct, enum ip_conntrack_dir dir,
306 + const struct xt_action_param *par, int ifindex)
308 + struct dst_entry *dst = NULL;
311 + memset(&fl, 0, sizeof(fl));
312 + switch (xt_family(par)) {
314 + fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
315 + fl.u.ip4.flowi4_oif = ifindex;
318 + fl.u.ip6.saddr = ct->tuplehash[dir].tuple.dst.u3.in6;
319 + fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
320 + fl.u.ip6.flowi6_oif = ifindex;
324 + nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
330 +xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
331 + const struct xt_action_param *par,
332 + struct nf_flow_route *route, enum ip_conntrack_dir dir)
334 + struct dst_entry *this_dst, *other_dst;
336 + this_dst = xt_flowoffload_dst(ct, !dir, par, xt_out(par)->ifindex);
337 + other_dst = xt_flowoffload_dst(ct, dir, par, xt_in(par)->ifindex);
339 + route->tuple[dir].dst = this_dst;
340 + route->tuple[!dir].dst = other_dst;
342 + if (!this_dst || !other_dst)
345 + if (dst_xfrm(this_dst) || dst_xfrm(other_dst))
352 +flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
354 + const struct xt_flowoffload_target_info *info = par->targinfo;
355 + struct tcphdr _tcph, *tcph = NULL;
356 + enum ip_conntrack_info ctinfo;
357 + enum ip_conntrack_dir dir;
358 + struct nf_flow_route route;
359 + struct flow_offload *flow = NULL;
360 + struct nf_conn *ct;
363 + if (xt_flowoffload_skip(skb, xt_family(par)))
364 + return XT_CONTINUE;
366 + ct = nf_ct_get(skb, &ctinfo);
368 + return XT_CONTINUE;
370 + switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
372 + if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
373 + return XT_CONTINUE;
375 + tcph = skb_header_pointer(skb, par->thoff,
376 + sizeof(_tcph), &_tcph);
377 + if (unlikely(!tcph || tcph->fin || tcph->rst))
378 + return XT_CONTINUE;
383 + return XT_CONTINUE;
386 + if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
387 + ct->status & IPS_SEQ_ADJUST)
388 + return XT_CONTINUE;
390 + if (!nf_ct_is_confirmed(ct))
391 + return XT_CONTINUE;
393 + if (!xt_in(par) || !xt_out(par))
394 + return XT_CONTINUE;
396 + if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
397 + return XT_CONTINUE;
399 + dir = CTINFO2DIR(ctinfo);
401 + if (xt_flowoffload_route(skb, ct, par, &route, dir) == 0)
402 + flow = flow_offload_alloc(ct, &route);
404 + dst_release(route.tuple[dir].dst);
405 + dst_release(route.tuple[!dir].dst);
408 + goto err_flow_route;
411 + ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
412 + ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
415 + if (flow_offload_add(&nf_flowtable, flow) < 0)
418 + xt_flowoffload_check_device(xt_in(par));
419 + xt_flowoffload_check_device(xt_out(par));
421 + net = read_pnet(&nf_flowtable.ft_net);
423 + write_pnet(&nf_flowtable.ft_net, xt_net(par));
425 + if (info->flags & XT_FLOWOFFLOAD_HW)
426 + nf_flow_offload_hw_add(xt_net(par), flow, ct);
428 + return XT_CONTINUE;
431 + flow_offload_free(flow);
433 + clear_bit(IPS_OFFLOAD_BIT, &ct->status);
434 + return XT_CONTINUE;
438 +static int flowoffload_chk(const struct xt_tgchk_param *par)
440 + struct xt_flowoffload_target_info *info = par->targinfo;
442 + if (info->flags & ~XT_FLOWOFFLOAD_MASK)
448 +static struct xt_target offload_tg_reg __read_mostly = {
449 + .family = NFPROTO_UNSPEC,
450 + .name = "FLOWOFFLOAD",
452 + .targetsize = sizeof(struct xt_flowoffload_target_info),
453 + .usersize = sizeof(struct xt_flowoffload_target_info),
454 + .checkentry = flowoffload_chk,
455 + .target = flowoffload_tg,
459 +static int xt_flowoffload_table_init(struct nf_flowtable *table)
461 + table->flags = NF_FLOWTABLE_F_HW;
462 + nf_flow_table_init(table);
466 +static void xt_flowoffload_table_cleanup(struct nf_flowtable *table)
468 + nf_flow_table_free(table);
471 +static int flow_offload_netdev_event(struct notifier_block *this,
472 + unsigned long event, void *ptr)
474 + struct xt_flowoffload_hook *hook = NULL;
475 + struct net_device *dev = netdev_notifier_info_to_dev(ptr);
477 + if (event != NETDEV_UNREGISTER)
478 + return NOTIFY_DONE;
480 + spin_lock_bh(&hooks_lock);
481 + hook = flow_offload_lookup_hook(dev);
483 + hlist_del(&hook->list);
485 + spin_unlock_bh(&hooks_lock);
487 + nf_unregister_net_hook(hook->net, &hook->ops);
491 + nf_flow_table_cleanup(dev);
493 + return NOTIFY_DONE;
496 +static struct notifier_block flow_offload_netdev_notifier = {
497 + .notifier_call = flow_offload_netdev_event,
500 +static int __init xt_flowoffload_tg_init(void)
504 + register_netdevice_notifier(&flow_offload_netdev_notifier);
506 + INIT_DELAYED_WORK(&hook_work, xt_flowoffload_hook_work);
508 + ret = xt_flowoffload_table_init(&nf_flowtable);
512 + ret = xt_register_target(&offload_tg_reg);
514 + xt_flowoffload_table_cleanup(&nf_flowtable);
519 +static void __exit xt_flowoffload_tg_exit(void)
521 + xt_unregister_target(&offload_tg_reg);
522 + xt_flowoffload_table_cleanup(&nf_flowtable);
523 + unregister_netdevice_notifier(&flow_offload_netdev_notifier);
526 +MODULE_LICENSE("GPL");
527 +module_init(xt_flowoffload_tg_init);
528 +module_exit(xt_flowoffload_tg_exit);
529 --- a/net/netfilter/nf_flow_table_core.c
530 +++ b/net/netfilter/nf_flow_table_core.c
532 #include <linux/netdevice.h>
534 #include <net/ip6_route.h>
535 -#include <net/netfilter/nf_tables.h>
536 #include <net/netfilter/nf_flow_table.h>
537 #include <net/netfilter/nf_conntrack.h>
538 #include <net/netfilter/nf_conntrack_core.h>
539 @@ -338,8 +337,7 @@ flow_offload_lookup(struct nf_flowtable
541 EXPORT_SYMBOL_GPL(flow_offload_lookup);
544 -nf_flow_table_iterate(struct nf_flowtable *flow_table,
545 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
546 void (*iter)(struct flow_offload *flow, void *data),
549 @@ -372,6 +370,7 @@ nf_flow_table_iterate(struct nf_flowtabl
553 +EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
555 static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
558 +++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
560 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
561 +#ifndef _XT_FLOWOFFLOAD_H
562 +#define _XT_FLOWOFFLOAD_H
564 +#include <linux/types.h>
567 + XT_FLOWOFFLOAD_HW = 1 << 0,
569 + XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
572 +struct xt_flowoffload_target_info {
576 +#endif /* _XT_FLOWOFFLOAD_H */
577 --- a/include/net/netfilter/nf_flow_table.h
578 +++ b/include/net/netfilter/nf_flow_table.h
579 @@ -130,6 +130,10 @@ static inline void flow_offload_dead(str
580 flow->flags |= FLOW_OFFLOAD_DYING;
583 +int nf_flow_table_iterate(struct nf_flowtable *flow_table,
584 + void (*iter)(struct flow_offload *flow, void *data),
587 int nf_flow_snat_port(const struct flow_offload *flow,
588 struct sk_buff *skb, unsigned int thoff,
589 u8 protocol, enum flow_offload_tuple_dir dir);