blob: 6b40b56d2a2d556d1d49929a4894fad1e5118ca7 [file] [log] [blame]
developer8cb3ac72022-07-04 10:55:14 +08001From 6ad9bd65769003ab526e504577e0f747eba14287 Mon Sep 17 00:00:00 2001
2From: Bo Jiao <Bo.Jiao@mediatek.com>
3Date: Wed, 22 Jun 2022 09:42:19 +0800
4Subject: [PATCH 1/8]
5 9990-mt7622-backport-nf-hw-offload-framework-and-upstream-hnat-plus-xt-FLOWOFFLOAD-update-v2
6
7---
8 drivers/net/ethernet/mediatek/Makefile | 3 +-
9 drivers/net/ethernet/mediatek/mtk_eth_soc.c | 28 +-
10 drivers/net/ethernet/mediatek/mtk_eth_soc.h | 20 +-
11 drivers/net/ethernet/mediatek/mtk_ppe.c | 509 +++++++
12 drivers/net/ethernet/mediatek/mtk_ppe.h | 288 ++++
13 .../net/ethernet/mediatek/mtk_ppe_debugfs.c | 214 +++
14 .../net/ethernet/mediatek/mtk_ppe_offload.c | 526 ++++++++
15 drivers/net/ethernet/mediatek/mtk_ppe_regs.h | 144 ++
16 drivers/net/ppp/ppp_generic.c | 22 +
17 drivers/net/ppp/pppoe.c | 24 +
18 include/linux/netdevice.h | 60 +
19 include/linux/ppp_channel.h | 3 +
20 include/net/dsa.h | 10 +
21 include/net/flow_offload.h | 4 +
22 include/net/ip6_route.h | 5 +-
23 .../net/netfilter/ipv6/nf_conntrack_ipv6.h | 3 -
24 include/net/netfilter/nf_conntrack.h | 12 +
25 include/net/netfilter/nf_conntrack_acct.h | 11 +
26 include/net/netfilter/nf_flow_table.h | 264 +++-
27 include/net/netns/conntrack.h | 6 +
28 .../linux/netfilter/nf_conntrack_common.h | 9 +-
29 include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h | 17 +
30 net/8021q/vlan_dev.c | 21 +
31 net/bridge/br_device.c | 49 +
32 net/bridge/br_private.h | 20 +
33 net/bridge/br_vlan.c | 55 +
34 net/core/dev.c | 46 +
35 net/dsa/dsa.c | 9 +
36 net/dsa/slave.c | 41 +-
37 net/ipv4/netfilter/Kconfig | 4 +-
38 net/ipv6/ip6_output.c | 2 +-
39 net/ipv6/netfilter/Kconfig | 3 +-
40 net/ipv6/route.c | 22 +-
41 net/netfilter/Kconfig | 14 +-
42 net/netfilter/Makefile | 4 +-
43 net/netfilter/nf_conntrack_core.c | 20 +-
44 net/netfilter/nf_conntrack_proto_tcp.c | 4 +
45 net/netfilter/nf_conntrack_proto_udp.c | 4 +
46 net/netfilter/nf_conntrack_standalone.c | 34 +-
47 net/netfilter/nf_flow_table_core.c | 446 +++---
48 net/netfilter/nf_flow_table_ip.c | 455 ++++---
49 net/netfilter/nf_flow_table_offload.c | 1191 +++++++++++++++++
50 net/netfilter/xt_FLOWOFFLOAD.c | 719 ++++++++++
51 43 files changed, 4913 insertions(+), 432 deletions(-)
52 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe.c
53 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe.h
54 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
55 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe_offload.c
56 create mode 100644 drivers/net/ethernet/mediatek/mtk_ppe_regs.h
57 create mode 100644 include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
58 create mode 100644 net/netfilter/nf_flow_table_offload.c
59 create mode 100644 net/netfilter/xt_FLOWOFFLOAD.c
60
61diff --git a/drivers/net/ethernet/mediatek/Makefile b/drivers/net/ethernet/mediatek/Makefile
62index 13c5b4e8f..0a6af99f1 100755
63--- a/drivers/net/ethernet/mediatek/Makefile
64+++ b/drivers/net/ethernet/mediatek/Makefile
65@@ -4,5 +4,6 @@
66 #
67
68 obj-$(CONFIG_NET_MEDIATEK_SOC) += mtk_eth.o
69-mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_eth_dbg.o mtk_eth_reset.o
70+mtk_eth-y := mtk_eth_soc.o mtk_sgmii.o mtk_eth_path.o mtk_eth_dbg.o mtk_eth_reset.o \
71+ mtk_ppe.o mtk_ppe_debugfs.o mtk_ppe_offload.o
72 obj-$(CONFIG_NET_MEDIATEK_HNAT) += mtk_hnat/
73diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
74index 2b21f7ed0..819d8a0be 100755
75--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
76+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
77@@ -2654,12 +2654,17 @@ static int mtk_open(struct net_device *dev)
78
79 /* we run 2 netdevs on the same dma ring so we only bring it up once */
80 if (!refcount_read(&eth->dma_refcnt)) {
81- int err = mtk_start_dma(eth);
82+ u32 gdm_config = MTK_GDMA_TO_PDMA;
83+ int err;
84
85+ err = mtk_start_dma(eth);
86 if (err)
87 return err;
88
89- mtk_gdm_config(eth, MTK_GDMA_TO_PDMA);
90+ if (eth->soc->offload_version && mtk_ppe_start(&eth->ppe) == 0)
91+ gdm_config = MTK_GDMA_TO_PPE;
92+
93+ mtk_gdm_config(eth, gdm_config);
94
95 /* Indicates CDM to parse the MTK special tag from CPU */
96 if (netdev_uses_dsa(dev)) {
97@@ -2772,6 +2777,9 @@ static int mtk_stop(struct net_device *dev)
98
99 mtk_dma_free(eth);
100
101+ if (eth->soc->offload_version)
102+ mtk_ppe_stop(&eth->ppe);
103+
104 return 0;
105 }
106
107@@ -3391,6 +3399,7 @@ static const struct net_device_ops mtk_netdev_ops = {
108 #ifdef CONFIG_NET_POLL_CONTROLLER
109 .ndo_poll_controller = mtk_poll_controller,
110 #endif
111+ .ndo_setup_tc = mtk_eth_setup_tc,
112 };
113
114 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
115@@ -3682,6 +3691,17 @@ static int mtk_probe(struct platform_device *pdev)
116 goto err_free_dev;
117 }
118
119+ if (eth->soc->offload_version) {
120+ err = mtk_ppe_init(&eth->ppe, eth->dev,
121+ eth->base + MTK_ETH_PPE_BASE, 2);
122+ if (err)
123+ goto err_free_dev;
124+
125+ err = mtk_eth_offload_init(eth);
126+ if (err)
127+ goto err_free_dev;
128+ }
129+
130 for (i = 0; i < MTK_MAX_DEVS; i++) {
131 if (!eth->netdev[i])
132 continue;
133@@ -3781,6 +3801,7 @@ static const struct mtk_soc_data mt2701_data = {
134 .required_clks = MT7623_CLKS_BITMAP,
135 .required_pctl = true,
136 .has_sram = false,
137+ .offload_version = 2,
138 };
139
140 static const struct mtk_soc_data mt7621_data = {
141@@ -3789,6 +3810,7 @@ static const struct mtk_soc_data mt7621_data = {
142 .required_clks = MT7621_CLKS_BITMAP,
143 .required_pctl = false,
144 .has_sram = false,
145+ .offload_version = 2,
146 };
147
148 static const struct mtk_soc_data mt7622_data = {
149@@ -3798,6 +3820,7 @@ static const struct mtk_soc_data mt7622_data = {
150 .required_clks = MT7622_CLKS_BITMAP,
151 .required_pctl = false,
152 .has_sram = false,
153+ .offload_version = 2,
154 };
155
156 static const struct mtk_soc_data mt7623_data = {
157@@ -3806,6 +3829,7 @@ static const struct mtk_soc_data mt7623_data = {
158 .required_clks = MT7623_CLKS_BITMAP,
159 .required_pctl = true,
160 .has_sram = false,
161+ .offload_version = 2,
162 };
163
164 static const struct mtk_soc_data mt7629_data = {
165diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.h b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
166index b6380ffeb..349f98503 100755
167--- a/drivers/net/ethernet/mediatek/mtk_eth_soc.h
168+++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.h
169@@ -15,6 +15,8 @@
170 #include <linux/u64_stats_sync.h>
171 #include <linux/refcount.h>
172 #include <linux/phylink.h>
173+#include <linux/rhashtable.h>
174+#include "mtk_ppe.h"
175
176 #define MTK_QDMA_PAGE_SIZE 2048
177 #define MTK_MAX_RX_LENGTH 1536
178@@ -37,7 +39,8 @@
179 NETIF_F_HW_VLAN_CTAG_TX | \
180 NETIF_F_SG | NETIF_F_TSO | \
181 NETIF_F_TSO6 | \
182- NETIF_F_IPV6_CSUM)
183+ NETIF_F_IPV6_CSUM |\
184+ NETIF_F_HW_TC)
185 #define MTK_SET_FEATURES (NETIF_F_LRO | \
186 NETIF_F_HW_VLAN_CTAG_RX)
187 #define MTK_HW_FEATURES_MT7628 (NETIF_F_SG | NETIF_F_RXCSUM)
188@@ -107,6 +110,7 @@
189 #define MTK_GDMA_TCS_EN BIT(21)
190 #define MTK_GDMA_UCS_EN BIT(20)
191 #define MTK_GDMA_TO_PDMA 0x0
192+#define MTK_GDMA_TO_PPE 0x4444
193 #define MTK_GDMA_DROP_ALL 0x7777
194
195 /* Unicast Filter MAC Address Register - Low */
196@@ -547,6 +551,12 @@
197 #define RX_DMA_TCI(_x) ((_x) & (VLAN_PRIO_MASK | VLAN_VID_MASK))
198 #define RX_DMA_VPID(_x) (((_x) >> 16) & 0xffff)
199
200+/* QDMA descriptor rxd4 */
201+#define MTK_RXD4_FOE_ENTRY GENMASK(13, 0)
202+#define MTK_RXD4_PPE_CPU_REASON GENMASK(18, 14)
203+#define MTK_RXD4_SRC_PORT GENMASK(21, 19)
204+#define MTK_RXD4_ALG GENMASK(31, 22)
205+
206 /* QDMA descriptor rxd4 */
207 #define RX_DMA_L4_VALID BIT(24)
208 #define RX_DMA_L4_VALID_PDMA BIT(30) /* when PDMA is used */
209@@ -1158,6 +1168,7 @@ struct mtk_soc_data {
210 u32 caps;
211 u32 required_clks;
212 bool required_pctl;
213+ u8 offload_version;
214 netdev_features_t hw_features;
215 bool has_sram;
216 };
217@@ -1271,6 +1282,9 @@ struct mtk_eth {
218 int ip_align;
219 spinlock_t syscfg0_lock;
220 struct timer_list mtk_dma_monitor_timer;
221+
222+ struct mtk_ppe ppe;
223+ struct rhashtable flow_table;
224 };
225
226 /* struct mtk_mac - the structure that holds the info about the MACs of the
227@@ -1319,4 +1333,8 @@ int mtk_gmac_rgmii_path_setup(struct mtk_eth *eth, int mac_id);
228 void mtk_gdm_config(struct mtk_eth *eth, u32 config);
229 void ethsys_reset(struct mtk_eth *eth, u32 reset_bits);
230
231+int mtk_eth_offload_init(struct mtk_eth *eth);
232+int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
233+ void *type_data);
234+
235 #endif /* MTK_ETH_H */
236diff --git a/drivers/net/ethernet/mediatek/mtk_ppe.c b/drivers/net/ethernet/mediatek/mtk_ppe.c
237new file mode 100644
238index 000000000..66298e223
239--- /dev/null
240+++ b/drivers/net/ethernet/mediatek/mtk_ppe.c
241@@ -0,0 +1,509 @@
242+// SPDX-License-Identifier: GPL-2.0-only
243+/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
244+
245+#include <linux/kernel.h>
246+#include <linux/io.h>
247+#include <linux/iopoll.h>
248+#include <linux/etherdevice.h>
249+#include <linux/platform_device.h>
250+#include "mtk_ppe.h"
251+#include "mtk_ppe_regs.h"
252+
253+static void ppe_w32(struct mtk_ppe *ppe, u32 reg, u32 val)
254+{
255+ writel(val, ppe->base + reg);
256+}
257+
258+static u32 ppe_r32(struct mtk_ppe *ppe, u32 reg)
259+{
260+ return readl(ppe->base + reg);
261+}
262+
263+static u32 ppe_m32(struct mtk_ppe *ppe, u32 reg, u32 mask, u32 set)
264+{
265+ u32 val;
266+
267+ val = ppe_r32(ppe, reg);
268+ val &= ~mask;
269+ val |= set;
270+ ppe_w32(ppe, reg, val);
271+
272+ return val;
273+}
274+
275+static u32 ppe_set(struct mtk_ppe *ppe, u32 reg, u32 val)
276+{
277+ return ppe_m32(ppe, reg, 0, val);
278+}
279+
280+static u32 ppe_clear(struct mtk_ppe *ppe, u32 reg, u32 val)
281+{
282+ return ppe_m32(ppe, reg, val, 0);
283+}
284+
285+static int mtk_ppe_wait_busy(struct mtk_ppe *ppe)
286+{
287+ int ret;
288+ u32 val;
289+
290+ ret = readl_poll_timeout(ppe->base + MTK_PPE_GLO_CFG, val,
291+ !(val & MTK_PPE_GLO_CFG_BUSY),
292+ 20, MTK_PPE_WAIT_TIMEOUT_US);
293+
294+ if (ret)
295+ dev_err(ppe->dev, "PPE table busy");
296+
297+ return ret;
298+}
299+
300+static void mtk_ppe_cache_clear(struct mtk_ppe *ppe)
301+{
302+ ppe_set(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
303+ ppe_clear(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_CLEAR);
304+}
305+
306+static void mtk_ppe_cache_enable(struct mtk_ppe *ppe, bool enable)
307+{
308+ mtk_ppe_cache_clear(ppe);
309+
310+ ppe_m32(ppe, MTK_PPE_CACHE_CTL, MTK_PPE_CACHE_CTL_EN,
311+ enable * MTK_PPE_CACHE_CTL_EN);
312+}
313+
314+static u32 mtk_ppe_hash_entry(struct mtk_foe_entry *e)
315+{
316+ u32 hv1, hv2, hv3;
317+ u32 hash;
318+
319+ switch (FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, e->ib1)) {
320+ case MTK_PPE_PKT_TYPE_BRIDGE:
321+ hv1 = e->bridge.src_mac_lo;
322+ hv1 ^= ((e->bridge.src_mac_hi & 0xffff) << 16);
323+ hv2 = e->bridge.src_mac_hi >> 16;
324+ hv2 ^= e->bridge.dest_mac_lo;
325+ hv3 = e->bridge.dest_mac_hi;
326+ break;
327+ case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
328+ case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
329+ hv1 = e->ipv4.orig.ports;
330+ hv2 = e->ipv4.orig.dest_ip;
331+ hv3 = e->ipv4.orig.src_ip;
332+ break;
333+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
334+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
335+ hv1 = e->ipv6.src_ip[3] ^ e->ipv6.dest_ip[3];
336+ hv1 ^= e->ipv6.ports;
337+
338+ hv2 = e->ipv6.src_ip[2] ^ e->ipv6.dest_ip[2];
339+ hv2 ^= e->ipv6.dest_ip[0];
340+
341+ hv3 = e->ipv6.src_ip[1] ^ e->ipv6.dest_ip[1];
342+ hv3 ^= e->ipv6.src_ip[0];
343+ break;
344+ case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
345+ case MTK_PPE_PKT_TYPE_IPV6_6RD:
346+ default:
347+ WARN_ON_ONCE(1);
348+ return MTK_PPE_HASH_MASK;
349+ }
350+
351+ hash = (hv1 & hv2) | ((~hv1) & hv3);
352+ hash = (hash >> 24) | ((hash & 0xffffff) << 8);
353+ hash ^= hv1 ^ hv2 ^ hv3;
354+ hash ^= hash >> 16;
355+ hash <<= 1;
356+ hash &= MTK_PPE_ENTRIES - 1;
357+
358+ return hash;
359+}
360+
361+static inline struct mtk_foe_mac_info *
362+mtk_foe_entry_l2(struct mtk_foe_entry *entry)
363+{
364+ int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
365+
366+ if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
367+ return &entry->ipv6.l2;
368+
369+ return &entry->ipv4.l2;
370+}
371+
372+static inline u32 *
373+mtk_foe_entry_ib2(struct mtk_foe_entry *entry)
374+{
375+ int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
376+
377+ if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE)
378+ return &entry->ipv6.ib2;
379+
380+ return &entry->ipv4.ib2;
381+}
382+
383+int mtk_foe_entry_prepare(struct mtk_foe_entry *entry, int type, int l4proto,
384+ u8 pse_port, u8 *src_mac, u8 *dest_mac)
385+{
386+ struct mtk_foe_mac_info *l2;
387+ u32 ports_pad, val;
388+
389+ memset(entry, 0, sizeof(*entry));
390+
391+ val = FIELD_PREP(MTK_FOE_IB1_STATE, MTK_FOE_STATE_BIND) |
392+ FIELD_PREP(MTK_FOE_IB1_PACKET_TYPE, type) |
393+ FIELD_PREP(MTK_FOE_IB1_UDP, l4proto == IPPROTO_UDP) |
394+ MTK_FOE_IB1_BIND_TTL |
395+ MTK_FOE_IB1_BIND_CACHE;
396+ entry->ib1 = val;
397+
398+ val = FIELD_PREP(MTK_FOE_IB2_PORT_MG, 0x3f) |
399+ FIELD_PREP(MTK_FOE_IB2_PORT_AG, 0x1f) |
400+ FIELD_PREP(MTK_FOE_IB2_DEST_PORT, pse_port);
401+
402+ if (is_multicast_ether_addr(dest_mac))
403+ val |= MTK_FOE_IB2_MULTICAST;
404+
405+ ports_pad = 0xa5a5a500 | (l4proto & 0xff);
406+ if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
407+ entry->ipv4.orig.ports = ports_pad;
408+ if (type == MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
409+ entry->ipv6.ports = ports_pad;
410+
411+ if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
412+ entry->ipv6.ib2 = val;
413+ l2 = &entry->ipv6.l2;
414+ } else {
415+ entry->ipv4.ib2 = val;
416+ l2 = &entry->ipv4.l2;
417+ }
418+
419+ l2->dest_mac_hi = get_unaligned_be32(dest_mac);
420+ l2->dest_mac_lo = get_unaligned_be16(dest_mac + 4);
421+ l2->src_mac_hi = get_unaligned_be32(src_mac);
422+ l2->src_mac_lo = get_unaligned_be16(src_mac + 4);
423+
424+ if (type >= MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T)
425+ l2->etype = ETH_P_IPV6;
426+ else
427+ l2->etype = ETH_P_IP;
428+
429+ return 0;
430+}
431+
432+int mtk_foe_entry_set_pse_port(struct mtk_foe_entry *entry, u8 port)
433+{
434+ u32 *ib2 = mtk_foe_entry_ib2(entry);
435+ u32 val;
436+
437+ val = *ib2;
438+ val &= ~MTK_FOE_IB2_DEST_PORT;
439+ val |= FIELD_PREP(MTK_FOE_IB2_DEST_PORT, port);
440+ *ib2 = val;
441+
442+ return 0;
443+}
444+
445+int mtk_foe_entry_set_ipv4_tuple(struct mtk_foe_entry *entry, bool egress,
446+ __be32 src_addr, __be16 src_port,
447+ __be32 dest_addr, __be16 dest_port)
448+{
449+ int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
450+ struct mtk_ipv4_tuple *t;
451+
452+ switch (type) {
453+ case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
454+ if (egress) {
455+ t = &entry->ipv4.new;
456+ break;
457+ }
458+ fallthrough;
459+ case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
460+ case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
461+ t = &entry->ipv4.orig;
462+ break;
463+ case MTK_PPE_PKT_TYPE_IPV6_6RD:
464+ entry->ipv6_6rd.tunnel_src_ip = be32_to_cpu(src_addr);
465+ entry->ipv6_6rd.tunnel_dest_ip = be32_to_cpu(dest_addr);
466+ return 0;
467+ default:
468+ WARN_ON_ONCE(1);
469+ return -EINVAL;
470+ }
471+
472+ t->src_ip = be32_to_cpu(src_addr);
473+ t->dest_ip = be32_to_cpu(dest_addr);
474+
475+ if (type == MTK_PPE_PKT_TYPE_IPV4_ROUTE)
476+ return 0;
477+
478+ t->src_port = be16_to_cpu(src_port);
479+ t->dest_port = be16_to_cpu(dest_port);
480+
481+ return 0;
482+}
483+
484+int mtk_foe_entry_set_ipv6_tuple(struct mtk_foe_entry *entry,
485+ __be32 *src_addr, __be16 src_port,
486+ __be32 *dest_addr, __be16 dest_port)
487+{
488+ int type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
489+ u32 *src, *dest;
490+ int i;
491+
492+ switch (type) {
493+ case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
494+ src = entry->dslite.tunnel_src_ip;
495+ dest = entry->dslite.tunnel_dest_ip;
496+ break;
497+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
498+ case MTK_PPE_PKT_TYPE_IPV6_6RD:
499+ entry->ipv6.src_port = be16_to_cpu(src_port);
500+ entry->ipv6.dest_port = be16_to_cpu(dest_port);
501+ fallthrough;
502+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
503+ src = entry->ipv6.src_ip;
504+ dest = entry->ipv6.dest_ip;
505+ break;
506+ default:
507+ WARN_ON_ONCE(1);
508+ return -EINVAL;
509+ }
510+
511+ for (i = 0; i < 4; i++)
512+ src[i] = be32_to_cpu(src_addr[i]);
513+ for (i = 0; i < 4; i++)
514+ dest[i] = be32_to_cpu(dest_addr[i]);
515+
516+ return 0;
517+}
518+
519+int mtk_foe_entry_set_dsa(struct mtk_foe_entry *entry, int port)
520+{
521+ struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(entry);
522+
523+ l2->etype = BIT(port);
524+
525+ if (!(entry->ib1 & MTK_FOE_IB1_BIND_VLAN_LAYER))
526+ entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_VLAN_LAYER, 1);
527+ else
528+ l2->etype |= BIT(8);
529+
530+ entry->ib1 &= ~MTK_FOE_IB1_BIND_VLAN_TAG;
531+
532+ return 0;
533+}
534+
535+int mtk_foe_entry_set_vlan(struct mtk_foe_entry *entry, int vid)
536+{
537+ struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(entry);
538+
539+ switch (FIELD_GET(MTK_FOE_IB1_BIND_VLAN_LAYER, entry->ib1)) {
540+ case 0:
541+ entry->ib1 |= MTK_FOE_IB1_BIND_VLAN_TAG |
542+ FIELD_PREP(MTK_FOE_IB1_BIND_VLAN_LAYER, 1);
543+ l2->vlan1 = vid;
544+ return 0;
545+ case 1:
546+ if (!(entry->ib1 & MTK_FOE_IB1_BIND_VLAN_TAG)) {
547+ l2->vlan1 = vid;
548+ l2->etype |= BIT(8);
549+ } else {
550+ l2->vlan2 = vid;
551+ entry->ib1 += FIELD_PREP(MTK_FOE_IB1_BIND_VLAN_LAYER, 1);
552+ }
553+ return 0;
554+ default:
555+ return -ENOSPC;
556+ }
557+}
558+
559+int mtk_foe_entry_set_pppoe(struct mtk_foe_entry *entry, int sid)
560+{
561+ struct mtk_foe_mac_info *l2 = mtk_foe_entry_l2(entry);
562+
563+ if (!(entry->ib1 & MTK_FOE_IB1_BIND_VLAN_LAYER) ||
564+ (entry->ib1 & MTK_FOE_IB1_BIND_VLAN_TAG))
565+ l2->etype = ETH_P_PPP_SES;
566+
567+ entry->ib1 |= MTK_FOE_IB1_BIND_PPPOE;
568+ l2->pppoe_id = sid;
569+
570+ return 0;
571+}
572+
573+static inline bool mtk_foe_entry_usable(struct mtk_foe_entry *entry)
574+{
575+ return !(entry->ib1 & MTK_FOE_IB1_STATIC) &&
576+ FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1) != MTK_FOE_STATE_BIND;
577+}
578+
579+int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
580+ u16 timestamp)
581+{
582+ struct mtk_foe_entry *hwe;
583+ u32 hash;
584+
585+ timestamp &= MTK_FOE_IB1_BIND_TIMESTAMP;
586+ entry->ib1 &= ~MTK_FOE_IB1_BIND_TIMESTAMP;
587+ entry->ib1 |= FIELD_PREP(MTK_FOE_IB1_BIND_TIMESTAMP, timestamp);
588+
589+ hash = mtk_ppe_hash_entry(entry);
590+ hwe = &ppe->foe_table[hash];
591+ if (!mtk_foe_entry_usable(hwe)) {
592+ hwe++;
593+ hash++;
594+
595+ if (!mtk_foe_entry_usable(hwe))
596+ return -ENOSPC;
597+ }
598+
599+ memcpy(&hwe->data, &entry->data, sizeof(hwe->data));
600+ wmb();
601+ hwe->ib1 = entry->ib1;
602+
603+ dma_wmb();
604+
605+ mtk_ppe_cache_clear(ppe);
606+
607+ return hash;
608+}
609+
610+int mtk_ppe_init(struct mtk_ppe *ppe, struct device *dev, void __iomem *base,
611+ int version)
612+{
613+ struct mtk_foe_entry *foe;
614+
615+ /* need to allocate a separate device, since it PPE DMA access is
616+ * not coherent.
617+ */
618+ ppe->base = base;
619+ ppe->dev = dev;
620+ ppe->version = version;
621+
622+ foe = dmam_alloc_coherent(ppe->dev, MTK_PPE_ENTRIES * sizeof(*foe),
623+ &ppe->foe_phys, GFP_KERNEL);
624+ if (!foe)
625+ return -ENOMEM;
626+
627+ ppe->foe_table = foe;
628+
629+ mtk_ppe_debugfs_init(ppe);
630+
631+ return 0;
632+}
633+
634+static void mtk_ppe_init_foe_table(struct mtk_ppe *ppe)
635+{
636+ static const u8 skip[] = { 12, 25, 38, 51, 76, 89, 102 };
637+ int i, k;
638+
639+ memset(ppe->foe_table, 0, MTK_PPE_ENTRIES * sizeof(*ppe->foe_table));
640+
641+ if (!IS_ENABLED(CONFIG_SOC_MT7621))
642+ return;
643+
644+ /* skip all entries that cross the 1024 byte boundary */
645+ for (i = 0; i < MTK_PPE_ENTRIES; i += 128)
646+ for (k = 0; k < ARRAY_SIZE(skip); k++)
647+ ppe->foe_table[i + skip[k]].ib1 |= MTK_FOE_IB1_STATIC;
648+}
649+
650+int mtk_ppe_start(struct mtk_ppe *ppe)
651+{
652+ u32 val;
653+
654+ mtk_ppe_init_foe_table(ppe);
655+ ppe_w32(ppe, MTK_PPE_TB_BASE, ppe->foe_phys);
656+
657+ val = MTK_PPE_TB_CFG_ENTRY_80B |
658+ MTK_PPE_TB_CFG_AGE_NON_L4 |
659+ MTK_PPE_TB_CFG_AGE_UNBIND |
660+ MTK_PPE_TB_CFG_AGE_TCP |
661+ MTK_PPE_TB_CFG_AGE_UDP |
662+ MTK_PPE_TB_CFG_AGE_TCP_FIN |
663+ FIELD_PREP(MTK_PPE_TB_CFG_SEARCH_MISS,
664+ MTK_PPE_SEARCH_MISS_ACTION_FORWARD_BUILD) |
665+ FIELD_PREP(MTK_PPE_TB_CFG_KEEPALIVE,
666+ MTK_PPE_KEEPALIVE_DISABLE) |
667+ FIELD_PREP(MTK_PPE_TB_CFG_HASH_MODE, 1) |
668+ FIELD_PREP(MTK_PPE_TB_CFG_SCAN_MODE,
669+ MTK_PPE_SCAN_MODE_KEEPALIVE_AGE) |
670+ FIELD_PREP(MTK_PPE_TB_CFG_ENTRY_NUM,
671+ MTK_PPE_ENTRIES_SHIFT);
672+ ppe_w32(ppe, MTK_PPE_TB_CFG, val);
673+
674+ ppe_w32(ppe, MTK_PPE_IP_PROTO_CHK,
675+ MTK_PPE_IP_PROTO_CHK_IPV4 | MTK_PPE_IP_PROTO_CHK_IPV6);
676+
677+ mtk_ppe_cache_enable(ppe, true);
678+
679+ val = MTK_PPE_FLOW_CFG_IP4_TCP_FRAG |
680+ MTK_PPE_FLOW_CFG_IP4_UDP_FRAG |
681+ MTK_PPE_FLOW_CFG_IP6_3T_ROUTE |
682+ MTK_PPE_FLOW_CFG_IP6_5T_ROUTE |
683+ MTK_PPE_FLOW_CFG_IP6_6RD |
684+ MTK_PPE_FLOW_CFG_IP4_NAT |
685+ MTK_PPE_FLOW_CFG_IP4_NAPT |
686+ MTK_PPE_FLOW_CFG_IP4_DSLITE |
687+ MTK_PPE_FLOW_CFG_L2_BRIDGE |
688+ MTK_PPE_FLOW_CFG_IP4_NAT_FRAG;
689+ ppe_w32(ppe, MTK_PPE_FLOW_CFG, val);
690+
691+ val = FIELD_PREP(MTK_PPE_UNBIND_AGE_MIN_PACKETS, 1000) |
692+ FIELD_PREP(MTK_PPE_UNBIND_AGE_DELTA, 3);
693+ ppe_w32(ppe, MTK_PPE_UNBIND_AGE, val);
694+
695+ val = FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_UDP, 12) |
696+ FIELD_PREP(MTK_PPE_BIND_AGE0_DELTA_NON_L4, 1);
697+ ppe_w32(ppe, MTK_PPE_BIND_AGE0, val);
698+
699+ val = FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP_FIN, 1) |
700+ FIELD_PREP(MTK_PPE_BIND_AGE1_DELTA_TCP, 7);
701+ ppe_w32(ppe, MTK_PPE_BIND_AGE1, val);
702+
703+ val = MTK_PPE_BIND_LIMIT0_QUARTER | MTK_PPE_BIND_LIMIT0_HALF;
704+ ppe_w32(ppe, MTK_PPE_BIND_LIMIT0, val);
705+
706+ val = MTK_PPE_BIND_LIMIT1_FULL |
707+ FIELD_PREP(MTK_PPE_BIND_LIMIT1_NON_L4, 1);
708+ ppe_w32(ppe, MTK_PPE_BIND_LIMIT1, val);
709+
710+ val = FIELD_PREP(MTK_PPE_BIND_RATE_BIND, 30) |
711+ FIELD_PREP(MTK_PPE_BIND_RATE_PREBIND, 1);
712+ ppe_w32(ppe, MTK_PPE_BIND_RATE, val);
713+
714+ /* enable PPE */
715+ val = MTK_PPE_GLO_CFG_EN |
716+ MTK_PPE_GLO_CFG_IP4_L4_CS_DROP |
717+ MTK_PPE_GLO_CFG_IP4_CS_DROP |
718+ MTK_PPE_GLO_CFG_FLOW_DROP_UPDATE;
719+ ppe_w32(ppe, MTK_PPE_GLO_CFG, val);
720+
721+ ppe_w32(ppe, MTK_PPE_DEFAULT_CPU_PORT, 0);
722+
723+ return 0;
724+}
725+
726+int mtk_ppe_stop(struct mtk_ppe *ppe)
727+{
728+ u32 val;
729+ int i;
730+
731+ for (i = 0; i < MTK_PPE_ENTRIES; i++)
732+ ppe->foe_table[i].ib1 = FIELD_PREP(MTK_FOE_IB1_STATE,
733+ MTK_FOE_STATE_INVALID);
734+
735+ mtk_ppe_cache_enable(ppe, false);
736+
737+ /* disable offload engine */
738+ ppe_clear(ppe, MTK_PPE_GLO_CFG, MTK_PPE_GLO_CFG_EN);
739+ ppe_w32(ppe, MTK_PPE_FLOW_CFG, 0);
740+
741+ /* disable aging */
742+ val = MTK_PPE_TB_CFG_AGE_NON_L4 |
743+ MTK_PPE_TB_CFG_AGE_UNBIND |
744+ MTK_PPE_TB_CFG_AGE_TCP |
745+ MTK_PPE_TB_CFG_AGE_UDP |
746+ MTK_PPE_TB_CFG_AGE_TCP_FIN;
747+ ppe_clear(ppe, MTK_PPE_TB_CFG, val);
748+
749+ return mtk_ppe_wait_busy(ppe);
750+}
751diff --git a/drivers/net/ethernet/mediatek/mtk_ppe.h b/drivers/net/ethernet/mediatek/mtk_ppe.h
752new file mode 100644
753index 000000000..242fb8f2a
754--- /dev/null
755+++ b/drivers/net/ethernet/mediatek/mtk_ppe.h
756@@ -0,0 +1,288 @@
757+// SPDX-License-Identifier: GPL-2.0-only
758+/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
759+
760+#ifndef __MTK_PPE_H
761+#define __MTK_PPE_H
762+
763+#include <linux/kernel.h>
764+#include <linux/bitfield.h>
765+
766+#define MTK_ETH_PPE_BASE 0xc00
767+
768+#define MTK_PPE_ENTRIES_SHIFT 3
769+#define MTK_PPE_ENTRIES (1024 << MTK_PPE_ENTRIES_SHIFT)
770+#define MTK_PPE_HASH_MASK (MTK_PPE_ENTRIES - 1)
771+#define MTK_PPE_WAIT_TIMEOUT_US 1000000
772+
773+#define MTK_FOE_IB1_UNBIND_TIMESTAMP GENMASK(7, 0)
774+#define MTK_FOE_IB1_UNBIND_PACKETS GENMASK(23, 8)
775+#define MTK_FOE_IB1_UNBIND_PREBIND BIT(24)
776+
777+#define MTK_FOE_IB1_BIND_TIMESTAMP GENMASK(14, 0)
778+#define MTK_FOE_IB1_BIND_KEEPALIVE BIT(15)
779+#define MTK_FOE_IB1_BIND_VLAN_LAYER GENMASK(18, 16)
780+#define MTK_FOE_IB1_BIND_PPPOE BIT(19)
781+#define MTK_FOE_IB1_BIND_VLAN_TAG BIT(20)
782+#define MTK_FOE_IB1_BIND_PKT_SAMPLE BIT(21)
783+#define MTK_FOE_IB1_BIND_CACHE BIT(22)
784+#define MTK_FOE_IB1_BIND_TUNNEL_DECAP BIT(23)
785+#define MTK_FOE_IB1_BIND_TTL BIT(24)
786+
787+#define MTK_FOE_IB1_PACKET_TYPE GENMASK(27, 25)
788+#define MTK_FOE_IB1_STATE GENMASK(29, 28)
789+#define MTK_FOE_IB1_UDP BIT(30)
790+#define MTK_FOE_IB1_STATIC BIT(31)
791+
792+enum {
793+ MTK_PPE_PKT_TYPE_IPV4_HNAPT = 0,
794+ MTK_PPE_PKT_TYPE_IPV4_ROUTE = 1,
795+ MTK_PPE_PKT_TYPE_BRIDGE = 2,
796+ MTK_PPE_PKT_TYPE_IPV4_DSLITE = 3,
797+ MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T = 4,
798+ MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T = 5,
799+ MTK_PPE_PKT_TYPE_IPV6_6RD = 7,
800+};
801+
802+#define MTK_FOE_IB2_QID GENMASK(3, 0)
803+#define MTK_FOE_IB2_PSE_QOS BIT(4)
804+#define MTK_FOE_IB2_DEST_PORT GENMASK(7, 5)
805+#define MTK_FOE_IB2_MULTICAST BIT(8)
806+
807+#define MTK_FOE_IB2_WHNAT_QID2 GENMASK(13, 12)
808+#define MTK_FOE_IB2_WHNAT_DEVIDX BIT(16)
809+#define MTK_FOE_IB2_WHNAT_NAT BIT(17)
810+
811+#define MTK_FOE_IB2_PORT_MG GENMASK(17, 12)
812+
813+#define MTK_FOE_IB2_PORT_AG GENMASK(23, 18)
814+
815+#define MTK_FOE_IB2_DSCP GENMASK(31, 24)
816+
817+#define MTK_FOE_VLAN2_WHNAT_BSS GEMMASK(5, 0)
818+#define MTK_FOE_VLAN2_WHNAT_WCID GENMASK(13, 6)
819+#define MTK_FOE_VLAN2_WHNAT_RING GENMASK(15, 14)
820+
821+enum {
822+ MTK_FOE_STATE_INVALID,
823+ MTK_FOE_STATE_UNBIND,
824+ MTK_FOE_STATE_BIND,
825+ MTK_FOE_STATE_FIN
826+};
827+
828+struct mtk_foe_mac_info {
829+ u16 vlan1;
830+ u16 etype;
831+
832+ u32 dest_mac_hi;
833+
834+ u16 vlan2;
835+ u16 dest_mac_lo;
836+
837+ u32 src_mac_hi;
838+
839+ u16 pppoe_id;
840+ u16 src_mac_lo;
841+};
842+
843+struct mtk_foe_bridge {
844+ u32 dest_mac_hi;
845+
846+ u16 src_mac_lo;
847+ u16 dest_mac_lo;
848+
849+ u32 src_mac_hi;
850+
851+ u32 ib2;
852+
853+ u32 _rsv[5];
854+
855+ u32 udf_tsid;
856+ struct mtk_foe_mac_info l2;
857+};
858+
859+struct mtk_ipv4_tuple {
860+ u32 src_ip;
861+ u32 dest_ip;
862+ union {
863+ struct {
864+ u16 dest_port;
865+ u16 src_port;
866+ };
867+ struct {
868+ u8 protocol;
869+ u8 _pad[3]; /* fill with 0xa5a5a5 */
870+ };
871+ u32 ports;
872+ };
873+};
874+
875+struct mtk_foe_ipv4 {
876+ struct mtk_ipv4_tuple orig;
877+
878+ u32 ib2;
879+
880+ struct mtk_ipv4_tuple new;
881+
882+ u16 timestamp;
883+ u16 _rsv0[3];
884+
885+ u32 udf_tsid;
886+
887+ struct mtk_foe_mac_info l2;
888+};
889+
890+struct mtk_foe_ipv4_dslite {
891+ struct mtk_ipv4_tuple ip4;
892+
893+ u32 tunnel_src_ip[4];
894+ u32 tunnel_dest_ip[4];
895+
896+ u8 flow_label[3];
897+ u8 priority;
898+
899+ u32 udf_tsid;
900+
901+ u32 ib2;
902+
903+ struct mtk_foe_mac_info l2;
904+};
905+
906+struct mtk_foe_ipv6 {
907+ u32 src_ip[4];
908+ u32 dest_ip[4];
909+
910+ union {
911+ struct {
912+ u8 protocol;
913+ u8 _pad[3]; /* fill with 0xa5a5a5 */
914+ }; /* 3-tuple */
915+ struct {
916+ u16 dest_port;
917+ u16 src_port;
918+ }; /* 5-tuple */
919+ u32 ports;
920+ };
921+
922+ u32 _rsv[3];
923+
924+ u32 udf;
925+
926+ u32 ib2;
927+ struct mtk_foe_mac_info l2;
928+};
929+
930+struct mtk_foe_ipv6_6rd {
931+ u32 src_ip[4];
932+ u32 dest_ip[4];
933+ u16 dest_port;
934+ u16 src_port;
935+
936+ u32 tunnel_src_ip;
937+ u32 tunnel_dest_ip;
938+
939+ u16 hdr_csum;
940+ u8 dscp;
941+ u8 ttl;
942+
943+ u8 flag;
944+ u8 pad;
945+ u8 per_flow_6rd_id;
946+ u8 pad2;
947+
948+ u32 ib2;
949+ struct mtk_foe_mac_info l2;
950+};
951+
952+struct mtk_foe_entry {
953+ u32 ib1;
954+
955+ union {
956+ struct mtk_foe_bridge bridge;
957+ struct mtk_foe_ipv4 ipv4;
958+ struct mtk_foe_ipv4_dslite dslite;
959+ struct mtk_foe_ipv6 ipv6;
960+ struct mtk_foe_ipv6_6rd ipv6_6rd;
961+ u32 data[19];
962+ };
963+};
964+
965+enum {
966+ MTK_PPE_CPU_REASON_TTL_EXCEEDED = 0x02,
967+ MTK_PPE_CPU_REASON_OPTION_HEADER = 0x03,
968+ MTK_PPE_CPU_REASON_NO_FLOW = 0x07,
969+ MTK_PPE_CPU_REASON_IPV4_FRAG = 0x08,
970+ MTK_PPE_CPU_REASON_IPV4_DSLITE_FRAG = 0x09,
971+ MTK_PPE_CPU_REASON_IPV4_DSLITE_NO_TCP_UDP = 0x0a,
972+ MTK_PPE_CPU_REASON_IPV6_6RD_NO_TCP_UDP = 0x0b,
973+ MTK_PPE_CPU_REASON_TCP_FIN_SYN_RST = 0x0c,
974+ MTK_PPE_CPU_REASON_UN_HIT = 0x0d,
975+ MTK_PPE_CPU_REASON_HIT_UNBIND = 0x0e,
976+ MTK_PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED = 0x0f,
977+ MTK_PPE_CPU_REASON_HIT_BIND_TCP_FIN = 0x10,
978+ MTK_PPE_CPU_REASON_HIT_TTL_1 = 0x11,
979+ MTK_PPE_CPU_REASON_HIT_BIND_VLAN_VIOLATION = 0x12,
980+ MTK_PPE_CPU_REASON_KEEPALIVE_UC_OLD_HDR = 0x13,
981+ MTK_PPE_CPU_REASON_KEEPALIVE_MC_NEW_HDR = 0x14,
982+ MTK_PPE_CPU_REASON_KEEPALIVE_DUP_OLD_HDR = 0x15,
983+ MTK_PPE_CPU_REASON_HIT_BIND_FORCE_CPU = 0x16,
984+ MTK_PPE_CPU_REASON_TUNNEL_OPTION_HEADER = 0x17,
985+ MTK_PPE_CPU_REASON_MULTICAST_TO_CPU = 0x18,
986+ MTK_PPE_CPU_REASON_MULTICAST_TO_GMAC1_CPU = 0x19,
987+ MTK_PPE_CPU_REASON_HIT_PRE_BIND = 0x1a,
988+ MTK_PPE_CPU_REASON_PACKET_SAMPLING = 0x1b,
989+ MTK_PPE_CPU_REASON_EXCEED_MTU = 0x1c,
990+ MTK_PPE_CPU_REASON_PPE_BYPASS = 0x1e,
991+ MTK_PPE_CPU_REASON_INVALID = 0x1f,
992+};
993+
994+struct mtk_ppe {
995+ struct device *dev;
996+ void __iomem *base;
997+ int version;
998+
999+ struct mtk_foe_entry *foe_table;
1000+ dma_addr_t foe_phys;
1001+
1002+ void *acct_table;
1003+};
1004+
1005+int mtk_ppe_init(struct mtk_ppe *ppe, struct device *dev, void __iomem *base,
1006+ int version);
1007+int mtk_ppe_start(struct mtk_ppe *ppe);
1008+int mtk_ppe_stop(struct mtk_ppe *ppe);
1009+
1010+static inline void
1011+mtk_foe_entry_clear(struct mtk_ppe *ppe, u16 hash)
1012+{
1013+ ppe->foe_table[hash].ib1 = 0;
1014+ dma_wmb();
1015+}
1016+
1017+static inline int
1018+mtk_foe_entry_timestamp(struct mtk_ppe *ppe, u16 hash)
1019+{
1020+ u32 ib1 = READ_ONCE(ppe->foe_table[hash].ib1);
1021+
1022+ if (FIELD_GET(MTK_FOE_IB1_STATE, ib1) != MTK_FOE_STATE_BIND)
1023+ return -1;
1024+
1025+ return FIELD_GET(MTK_FOE_IB1_BIND_TIMESTAMP, ib1);
1026+}
1027+
1028+int mtk_foe_entry_prepare(struct mtk_foe_entry *entry, int type, int l4proto,
1029+ u8 pse_port, u8 *src_mac, u8 *dest_mac);
1030+int mtk_foe_entry_set_pse_port(struct mtk_foe_entry *entry, u8 port);
1031+int mtk_foe_entry_set_ipv4_tuple(struct mtk_foe_entry *entry, bool orig,
1032+ __be32 src_addr, __be16 src_port,
1033+ __be32 dest_addr, __be16 dest_port);
1034+int mtk_foe_entry_set_ipv6_tuple(struct mtk_foe_entry *entry,
1035+ __be32 *src_addr, __be16 src_port,
1036+ __be32 *dest_addr, __be16 dest_port);
1037+int mtk_foe_entry_set_dsa(struct mtk_foe_entry *entry, int port);
1038+int mtk_foe_entry_set_vlan(struct mtk_foe_entry *entry, int vid);
1039+int mtk_foe_entry_set_pppoe(struct mtk_foe_entry *entry, int sid);
1040+int mtk_foe_entry_commit(struct mtk_ppe *ppe, struct mtk_foe_entry *entry,
1041+ u16 timestamp);
1042+int mtk_ppe_debugfs_init(struct mtk_ppe *ppe);
1043+
1044+#endif
1045diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
1046new file mode 100644
1047index 000000000..d4b482340
1048--- /dev/null
1049+++ b/drivers/net/ethernet/mediatek/mtk_ppe_debugfs.c
1050@@ -0,0 +1,214 @@
1051+// SPDX-License-Identifier: GPL-2.0-only
1052+/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
1053+
1054+#include <linux/kernel.h>
1055+#include <linux/debugfs.h>
1056+#include "mtk_eth_soc.h"
1057+
1058+struct mtk_flow_addr_info
1059+{
1060+ void *src, *dest;
1061+ u16 *src_port, *dest_port;
1062+ bool ipv6;
1063+};
1064+
1065+static const char *mtk_foe_entry_state_str(int state)
1066+{
1067+ static const char * const state_str[] = {
1068+ [MTK_FOE_STATE_INVALID] = "INV",
1069+ [MTK_FOE_STATE_UNBIND] = "UNB",
1070+ [MTK_FOE_STATE_BIND] = "BND",
1071+ [MTK_FOE_STATE_FIN] = "FIN",
1072+ };
1073+
1074+ if (state >= ARRAY_SIZE(state_str) || !state_str[state])
1075+ return "UNK";
1076+
1077+ return state_str[state];
1078+}
1079+
1080+static const char *mtk_foe_pkt_type_str(int type)
1081+{
1082+ static const char * const type_str[] = {
1083+ [MTK_PPE_PKT_TYPE_IPV4_HNAPT] = "IPv4 5T",
1084+ [MTK_PPE_PKT_TYPE_IPV4_ROUTE] = "IPv4 3T",
1085+ [MTK_PPE_PKT_TYPE_BRIDGE] = "L2",
1086+ [MTK_PPE_PKT_TYPE_IPV4_DSLITE] = "DS-LITE",
1087+ [MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T] = "IPv6 3T",
1088+ [MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T] = "IPv6 5T",
1089+ [MTK_PPE_PKT_TYPE_IPV6_6RD] = "6RD",
1090+ };
1091+
1092+ if (type >= ARRAY_SIZE(type_str) || !type_str[type])
1093+ return "UNKNOWN";
1094+
1095+ return type_str[type];
1096+}
1097+
1098+static void
1099+mtk_print_addr(struct seq_file *m, u32 *addr, bool ipv6)
1100+{
1101+ u32 n_addr[4];
1102+ int i;
1103+
1104+ if (!ipv6) {
1105+ seq_printf(m, "%pI4h", addr);
1106+ return;
1107+ }
1108+
1109+ for (i = 0; i < ARRAY_SIZE(n_addr); i++)
1110+ n_addr[i] = htonl(addr[i]);
1111+ seq_printf(m, "%pI6", n_addr);
1112+}
1113+
1114+static void
1115+mtk_print_addr_info(struct seq_file *m, struct mtk_flow_addr_info *ai)
1116+{
1117+ mtk_print_addr(m, ai->src, ai->ipv6);
1118+ if (ai->src_port)
1119+ seq_printf(m, ":%d", *ai->src_port);
1120+ seq_printf(m, "->");
1121+ mtk_print_addr(m, ai->dest, ai->ipv6);
1122+ if (ai->dest_port)
1123+ seq_printf(m, ":%d", *ai->dest_port);
1124+}
1125+
1126+static int
1127+mtk_ppe_debugfs_foe_show(struct seq_file *m, void *private, bool bind)
1128+{
1129+ struct mtk_ppe *ppe = m->private;
1130+ int i;
1131+
1132+ for (i = 0; i < MTK_PPE_ENTRIES; i++) {
1133+ struct mtk_foe_entry *entry = &ppe->foe_table[i];
1134+ struct mtk_foe_mac_info *l2;
1135+ struct mtk_flow_addr_info ai = {};
1136+ unsigned char h_source[ETH_ALEN];
1137+ unsigned char h_dest[ETH_ALEN];
1138+ int type, state;
1139+ u32 ib2;
1140+
1141+
1142+ state = FIELD_GET(MTK_FOE_IB1_STATE, entry->ib1);
1143+ if (!state)
1144+ continue;
1145+
1146+ if (bind && state != MTK_FOE_STATE_BIND)
1147+ continue;
1148+
1149+ type = FIELD_GET(MTK_FOE_IB1_PACKET_TYPE, entry->ib1);
1150+ seq_printf(m, "%05x %s %7s", i,
1151+ mtk_foe_entry_state_str(state),
1152+ mtk_foe_pkt_type_str(type));
1153+
1154+ switch (type) {
1155+ case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
1156+ case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
1157+ ai.src_port = &entry->ipv4.orig.src_port;
1158+ ai.dest_port = &entry->ipv4.orig.dest_port;
1159+ fallthrough;
1160+ case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
1161+ ai.src = &entry->ipv4.orig.src_ip;
1162+ ai.dest = &entry->ipv4.orig.dest_ip;
1163+ break;
1164+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T:
1165+ ai.src_port = &entry->ipv6.src_port;
1166+ ai.dest_port = &entry->ipv6.dest_port;
1167+ fallthrough;
1168+ case MTK_PPE_PKT_TYPE_IPV6_ROUTE_3T:
1169+ case MTK_PPE_PKT_TYPE_IPV6_6RD:
1170+ ai.src = &entry->ipv6.src_ip;
1171+ ai.dest = &entry->ipv6.dest_ip;
1172+ ai.ipv6 = true;
1173+ break;
1174+ }
1175+
1176+ seq_printf(m, " orig=");
1177+ mtk_print_addr_info(m, &ai);
1178+
1179+ switch (type) {
1180+ case MTK_PPE_PKT_TYPE_IPV4_HNAPT:
1181+ case MTK_PPE_PKT_TYPE_IPV4_DSLITE:
1182+ ai.src_port = &entry->ipv4.new.src_port;
1183+ ai.dest_port = &entry->ipv4.new.dest_port;
1184+ fallthrough;
1185+ case MTK_PPE_PKT_TYPE_IPV4_ROUTE:
1186+ ai.src = &entry->ipv4.new.src_ip;
1187+ ai.dest = &entry->ipv4.new.dest_ip;
1188+ seq_printf(m, " new=");
1189+ mtk_print_addr_info(m, &ai);
1190+ break;
1191+ }
1192+
1193+ if (type >= MTK_PPE_PKT_TYPE_IPV4_DSLITE) {
1194+ l2 = &entry->ipv6.l2;
1195+ ib2 = entry->ipv6.ib2;
1196+ } else {
1197+ l2 = &entry->ipv4.l2;
1198+ ib2 = entry->ipv4.ib2;
1199+ }
1200+
1201+ *((__be32 *)h_source) = htonl(l2->src_mac_hi);
1202+ *((__be16 *)&h_source[4]) = htons(l2->src_mac_lo);
1203+ *((__be32 *)h_dest) = htonl(l2->dest_mac_hi);
1204+ *((__be16 *)&h_dest[4]) = htons(l2->dest_mac_lo);
1205+
1206+ seq_printf(m, " eth=%pM->%pM etype=%04x"
1207+ " vlan=%d,%d ib1=%08x ib2=%08x\n",
1208+ h_source, h_dest, ntohs(l2->etype),
1209+ l2->vlan1, l2->vlan2, entry->ib1, ib2);
1210+ }
1211+
1212+ return 0;
1213+}
1214+
1215+static int
1216+mtk_ppe_debugfs_foe_show_all(struct seq_file *m, void *private)
1217+{
1218+ return mtk_ppe_debugfs_foe_show(m, private, false);
1219+}
1220+
1221+static int
1222+mtk_ppe_debugfs_foe_show_bind(struct seq_file *m, void *private)
1223+{
1224+ return mtk_ppe_debugfs_foe_show(m, private, true);
1225+}
1226+
1227+static int
1228+mtk_ppe_debugfs_foe_open_all(struct inode *inode, struct file *file)
1229+{
1230+ return single_open(file, mtk_ppe_debugfs_foe_show_all,
1231+ inode->i_private);
1232+}
1233+
1234+static int
1235+mtk_ppe_debugfs_foe_open_bind(struct inode *inode, struct file *file)
1236+{
1237+ return single_open(file, mtk_ppe_debugfs_foe_show_bind,
1238+ inode->i_private);
1239+}
1240+
1241+int mtk_ppe_debugfs_init(struct mtk_ppe *ppe)
1242+{
1243+ static const struct file_operations fops_all = {
1244+ .open = mtk_ppe_debugfs_foe_open_all,
1245+ .read = seq_read,
1246+ .llseek = seq_lseek,
1247+ .release = single_release,
1248+ };
1249+
1250+ static const struct file_operations fops_bind = {
1251+ .open = mtk_ppe_debugfs_foe_open_bind,
1252+ .read = seq_read,
1253+ .llseek = seq_lseek,
1254+ .release = single_release,
1255+ };
1256+
1257+ struct dentry *root;
1258+
1259+ root = debugfs_create_dir("mtk_ppe", NULL);
1260+ debugfs_create_file("entries", S_IRUGO, root, ppe, &fops_all);
1261+ debugfs_create_file("bind", S_IRUGO, root, ppe, &fops_bind);
1262+
1263+ return 0;
1264+}
1265diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_offload.c b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
1266new file mode 100644
1267index 000000000..4294f0c74
1268--- /dev/null
1269+++ b/drivers/net/ethernet/mediatek/mtk_ppe_offload.c
1270@@ -0,0 +1,526 @@
1271+// SPDX-License-Identifier: GPL-2.0-only
1272+/*
1273+ * Copyright (C) 2020 Felix Fietkau <nbd@nbd.name>
1274+ */
1275+
1276+#include <linux/if_ether.h>
1277+#include <linux/rhashtable.h>
1278+#include <linux/ip.h>
1279+#include <linux/ipv6.h>
1280+#include <net/flow_offload.h>
1281+#include <net/pkt_cls.h>
1282+#include <net/dsa.h>
1283+#include "mtk_eth_soc.h"
1284+
1285+struct mtk_flow_data {
1286+ struct ethhdr eth;
1287+
1288+ union {
1289+ struct {
1290+ __be32 src_addr;
1291+ __be32 dst_addr;
1292+ } v4;
1293+
1294+ struct {
1295+ struct in6_addr src_addr;
1296+ struct in6_addr dst_addr;
1297+ } v6;
1298+ };
1299+
1300+ __be16 src_port;
1301+ __be16 dst_port;
1302+
1303+ struct {
1304+ u16 id;
1305+ __be16 proto;
1306+ u8 num;
1307+ } vlan;
1308+ struct {
1309+ u16 sid;
1310+ u8 num;
1311+ } pppoe;
1312+};
1313+
1314+struct mtk_flow_entry {
1315+ struct rhash_head node;
1316+ unsigned long cookie;
1317+ u16 hash;
1318+};
1319+
1320+static const struct rhashtable_params mtk_flow_ht_params = {
1321+ .head_offset = offsetof(struct mtk_flow_entry, node),
1322+ .key_offset = offsetof(struct mtk_flow_entry, cookie),
1323+ .key_len = sizeof(unsigned long),
1324+ .automatic_shrinking = true,
1325+};
1326+
1327+static u32
1328+mtk_eth_timestamp(struct mtk_eth *eth)
1329+{
1330+ return mtk_r32(eth, 0x0010) & MTK_FOE_IB1_BIND_TIMESTAMP;
1331+}
1332+
1333+static int
1334+mtk_flow_set_ipv4_addr(struct mtk_foe_entry *foe, struct mtk_flow_data *data,
1335+ bool egress)
1336+{
1337+ return mtk_foe_entry_set_ipv4_tuple(foe, egress,
1338+ data->v4.src_addr, data->src_port,
1339+ data->v4.dst_addr, data->dst_port);
1340+}
1341+
1342+static int
1343+mtk_flow_set_ipv6_addr(struct mtk_foe_entry *foe, struct mtk_flow_data *data)
1344+{
1345+ return mtk_foe_entry_set_ipv6_tuple(foe,
1346+ data->v6.src_addr.s6_addr32, data->src_port,
1347+ data->v6.dst_addr.s6_addr32, data->dst_port);
1348+}
1349+
1350+static void
1351+mtk_flow_offload_mangle_eth(const struct flow_action_entry *act, void *eth)
1352+{
1353+ void *dest = eth + act->mangle.offset;
1354+ const void *src = &act->mangle.val;
1355+
1356+ if (act->mangle.offset > 8)
1357+ return;
1358+
1359+ if (act->mangle.mask == 0xffff) {
1360+ src += 2;
1361+ dest += 2;
1362+ }
1363+
1364+ memcpy(dest, src, act->mangle.mask ? 2 : 4);
1365+}
1366+
1367+
1368+static int
1369+mtk_flow_mangle_ports(const struct flow_action_entry *act,
1370+ struct mtk_flow_data *data)
1371+{
1372+ u32 val = ntohl(act->mangle.val);
1373+
1374+ switch (act->mangle.offset) {
1375+ case 0:
1376+ if (act->mangle.mask == ~htonl(0xffff))
1377+ data->dst_port = cpu_to_be16(val);
1378+ else
1379+ data->src_port = cpu_to_be16(val >> 16);
1380+ break;
1381+ case 2:
1382+ data->dst_port = cpu_to_be16(val);
1383+ break;
1384+ default:
1385+ return -EINVAL;
1386+ }
1387+
1388+ return 0;
1389+}
1390+
1391+static int
1392+mtk_flow_mangle_ipv4(const struct flow_action_entry *act,
1393+ struct mtk_flow_data *data)
1394+{
1395+ __be32 *dest;
1396+
1397+ switch (act->mangle.offset) {
1398+ case offsetof(struct iphdr, saddr):
1399+ dest = &data->v4.src_addr;
1400+ break;
1401+ case offsetof(struct iphdr, daddr):
1402+ dest = &data->v4.dst_addr;
1403+ break;
1404+ default:
1405+ return -EINVAL;
1406+ }
1407+
1408+ memcpy(dest, &act->mangle.val, sizeof(u32));
1409+
1410+ return 0;
1411+}
1412+
1413+static int
1414+mtk_flow_get_dsa_port(struct net_device **dev)
1415+{
1416+#if IS_ENABLED(CONFIG_NET_DSA)
1417+ struct dsa_port *dp;
1418+
1419+ dp = dsa_port_from_netdev(*dev);
1420+ if (IS_ERR(dp))
1421+ return -ENODEV;
1422+
1423+ if (dp->cpu_dp->tag_ops->proto != DSA_TAG_PROTO_MTK)
1424+ return -ENODEV;
1425+
1426+ *dev = dp->cpu_dp->master;
1427+
1428+ return dp->index;
1429+#else
1430+ return -ENODEV;
1431+#endif
1432+}
1433+
1434+static int
1435+mtk_flow_set_output_device(struct mtk_eth *eth, struct mtk_foe_entry *foe,
1436+ struct net_device *dev)
1437+{
1438+ int pse_port, dsa_port;
1439+
1440+ dsa_port = mtk_flow_get_dsa_port(&dev);
1441+ if (dsa_port >= 0)
1442+ mtk_foe_entry_set_dsa(foe, dsa_port);
1443+
1444+ if (dev == eth->netdev[0])
1445+ pse_port = 1;
1446+ else if (dev == eth->netdev[1])
1447+ pse_port = 2;
1448+ else
1449+ return -EOPNOTSUPP;
1450+
1451+ mtk_foe_entry_set_pse_port(foe, pse_port);
1452+
1453+ return 0;
1454+}
1455+
1456+static int
1457+mtk_flow_offload_replace(struct mtk_eth *eth, struct flow_cls_offload *f)
1458+{
1459+ struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1460+ struct flow_action_entry *act;
1461+ struct mtk_flow_data data = {};
1462+ struct mtk_foe_entry foe;
1463+ struct net_device *odev = NULL;
1464+ struct mtk_flow_entry *entry;
1465+ int offload_type = 0;
1466+ u16 addr_type = 0;
1467+ u32 timestamp;
1468+ u8 l4proto = 0;
1469+ int err = 0;
1470+ int hash;
1471+ int i;
1472+
1473+ if (rhashtable_lookup(&eth->flow_table, &f->cookie, mtk_flow_ht_params))
1474+ return -EEXIST;
1475+
1476+ if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_META)) {
1477+ struct flow_match_meta match;
1478+
1479+ flow_rule_match_meta(rule, &match);
1480+ } else {
1481+ return -EOPNOTSUPP;
1482+ }
1483+
1484+ if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
1485+ struct flow_match_control match;
1486+
1487+ flow_rule_match_control(rule, &match);
1488+ addr_type = match.key->addr_type;
1489+ } else {
1490+ return -EOPNOTSUPP;
1491+ }
1492+
1493+ if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
1494+ struct flow_match_basic match;
1495+
1496+ flow_rule_match_basic(rule, &match);
1497+ l4proto = match.key->ip_proto;
1498+ } else {
1499+ return -EOPNOTSUPP;
1500+ }
1501+
1502+ flow_action_for_each(i, act, &rule->action) {
1503+ switch (act->id) {
1504+ case FLOW_ACTION_MANGLE:
1505+ if (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_ETH)
1506+ mtk_flow_offload_mangle_eth(act, &data.eth);
1507+ break;
1508+ case FLOW_ACTION_REDIRECT:
1509+ odev = act->dev;
1510+ break;
1511+ case FLOW_ACTION_CSUM:
1512+ break;
1513+ case FLOW_ACTION_VLAN_PUSH:
1514+ if (data.vlan.num == 1 ||
1515+ act->vlan.proto != htons(ETH_P_8021Q))
1516+ return -EOPNOTSUPP;
1517+
1518+ data.vlan.id = act->vlan.vid;
1519+ data.vlan.proto = act->vlan.proto;
1520+ data.vlan.num++;
1521+ break;
1522+ case FLOW_ACTION_VLAN_POP:
1523+ break;
1524+ case FLOW_ACTION_PPPOE_PUSH:
1525+ if (data.pppoe.num == 1)
1526+ return -EOPNOTSUPP;
1527+
1528+ data.pppoe.sid = act->pppoe.sid;
1529+ data.pppoe.num++;
1530+ break;
1531+ default:
1532+ return -EOPNOTSUPP;
1533+ }
1534+ }
1535+
1536+ switch (addr_type) {
1537+ case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
1538+ offload_type = MTK_PPE_PKT_TYPE_IPV4_HNAPT;
1539+ break;
1540+ case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
1541+ offload_type = MTK_PPE_PKT_TYPE_IPV6_ROUTE_5T;
1542+ break;
1543+ default:
1544+ return -EOPNOTSUPP;
1545+ }
1546+
1547+ if (!is_valid_ether_addr(data.eth.h_source) ||
1548+ !is_valid_ether_addr(data.eth.h_dest))
1549+ return -EINVAL;
1550+
1551+ err = mtk_foe_entry_prepare(&foe, offload_type, l4proto, 0,
1552+ data.eth.h_source,
1553+ data.eth.h_dest);
1554+ if (err)
1555+ return err;
1556+
1557+ if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
1558+ struct flow_match_ports ports;
1559+
1560+ flow_rule_match_ports(rule, &ports);
1561+ data.src_port = ports.key->src;
1562+ data.dst_port = ports.key->dst;
1563+ } else {
1564+ return -EOPNOTSUPP;
1565+ }
1566+
1567+ if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1568+ struct flow_match_ipv4_addrs addrs;
1569+
1570+ flow_rule_match_ipv4_addrs(rule, &addrs);
1571+
1572+ data.v4.src_addr = addrs.key->src;
1573+ data.v4.dst_addr = addrs.key->dst;
1574+
1575+ mtk_flow_set_ipv4_addr(&foe, &data, false);
1576+ }
1577+
1578+ if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
1579+ struct flow_match_ipv6_addrs addrs;
1580+
1581+ flow_rule_match_ipv6_addrs(rule, &addrs);
1582+
1583+ data.v6.src_addr = addrs.key->src;
1584+ data.v6.dst_addr = addrs.key->dst;
1585+
1586+ mtk_flow_set_ipv6_addr(&foe, &data);
1587+ }
1588+
1589+ flow_action_for_each(i, act, &rule->action) {
1590+ if (act->id != FLOW_ACTION_MANGLE)
1591+ continue;
1592+
1593+ switch (act->mangle.htype) {
1594+ case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
1595+ case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
1596+ err = mtk_flow_mangle_ports(act, &data);
1597+ break;
1598+ case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
1599+ err = mtk_flow_mangle_ipv4(act, &data);
1600+ break;
1601+ case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
1602+ /* handled earlier */
1603+ break;
1604+ default:
1605+ return -EOPNOTSUPP;
1606+ }
1607+
1608+ if (err)
1609+ return err;
1610+ }
1611+
1612+ if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
1613+ err = mtk_flow_set_ipv4_addr(&foe, &data, true);
1614+ if (err)
1615+ return err;
1616+ }
1617+
1618+ if (data.vlan.num == 1) {
1619+ if (data.vlan.proto != htons(ETH_P_8021Q))
1620+ return -EOPNOTSUPP;
1621+
1622+ mtk_foe_entry_set_vlan(&foe, data.vlan.id);
1623+ }
1624+ if (data.pppoe.num == 1)
1625+ mtk_foe_entry_set_pppoe(&foe, data.pppoe.sid);
1626+
1627+ err = mtk_flow_set_output_device(eth, &foe, odev);
1628+ if (err)
1629+ return err;
1630+
1631+ entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1632+ if (!entry)
1633+ return -ENOMEM;
1634+
1635+ entry->cookie = f->cookie;
1636+ timestamp = mtk_eth_timestamp(eth);
1637+ hash = mtk_foe_entry_commit(&eth->ppe, &foe, timestamp);
1638+ if (hash < 0) {
1639+ err = hash;
1640+ goto free;
1641+ }
1642+
1643+ entry->hash = hash;
1644+ err = rhashtable_insert_fast(&eth->flow_table, &entry->node,
1645+ mtk_flow_ht_params);
1646+ if (err < 0)
1647+ goto clear_flow;
1648+
1649+ return 0;
1650+clear_flow:
1651+ mtk_foe_entry_clear(&eth->ppe, hash);
1652+free:
1653+ kfree(entry);
1654+ return err;
1655+}
1656+
1657+static int
1658+mtk_flow_offload_destroy(struct mtk_eth *eth, struct flow_cls_offload *f)
1659+{
1660+ struct mtk_flow_entry *entry;
1661+
1662+ entry = rhashtable_lookup(&eth->flow_table, &f->cookie,
1663+ mtk_flow_ht_params);
1664+ if (!entry)
1665+ return -ENOENT;
1666+
1667+ mtk_foe_entry_clear(&eth->ppe, entry->hash);
1668+ rhashtable_remove_fast(&eth->flow_table, &entry->node,
1669+ mtk_flow_ht_params);
1670+ kfree(entry);
1671+
1672+ return 0;
1673+}
1674+
1675+static int
1676+mtk_flow_offload_stats(struct mtk_eth *eth, struct flow_cls_offload *f)
1677+{
1678+ struct mtk_flow_entry *entry;
1679+ int timestamp;
1680+ u32 idle;
1681+
1682+ entry = rhashtable_lookup(&eth->flow_table, &f->cookie,
1683+ mtk_flow_ht_params);
1684+ if (!entry)
1685+ return -ENOENT;
1686+
1687+ timestamp = mtk_foe_entry_timestamp(&eth->ppe, entry->hash);
1688+ if (timestamp < 0)
1689+ return -ETIMEDOUT;
1690+
1691+ idle = mtk_eth_timestamp(eth) - timestamp;
1692+ f->stats.lastused = jiffies - idle * HZ;
1693+
1694+ return 0;
1695+}
1696+
1697+static DEFINE_MUTEX(mtk_flow_offload_mutex);
1698+
1699+static int
1700+mtk_eth_setup_tc_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
1701+{
1702+ struct flow_cls_offload *cls = type_data;
1703+ struct net_device *dev = cb_priv;
1704+ struct mtk_mac *mac = netdev_priv(dev);
1705+ struct mtk_eth *eth = mac->hw;
1706+ int err;
1707+
1708+ if (!tc_can_offload(dev))
1709+ return -EOPNOTSUPP;
1710+
1711+ if (type != TC_SETUP_CLSFLOWER)
1712+ return -EOPNOTSUPP;
1713+
1714+ mutex_lock(&mtk_flow_offload_mutex);
1715+ switch (cls->command) {
1716+ case FLOW_CLS_REPLACE:
1717+ err = mtk_flow_offload_replace(eth, cls);
1718+ break;
1719+ case FLOW_CLS_DESTROY:
1720+ err = mtk_flow_offload_destroy(eth, cls);
1721+ break;
1722+ case FLOW_CLS_STATS:
1723+ err = mtk_flow_offload_stats(eth, cls);
1724+ break;
1725+ default:
1726+ err = -EOPNOTSUPP;
1727+ break;
1728+ }
1729+ mutex_unlock(&mtk_flow_offload_mutex);
1730+
1731+ return err;
1732+}
1733+
1734+static int
1735+mtk_eth_setup_tc_block(struct net_device *dev, struct flow_block_offload *f)
1736+{
1737+ struct mtk_mac *mac = netdev_priv(dev);
1738+ struct mtk_eth *eth = mac->hw;
1739+ static LIST_HEAD(block_cb_list);
1740+ struct flow_block_cb *block_cb;
1741+ flow_setup_cb_t *cb;
1742+
1743+ if (!eth->ppe.foe_table)
1744+ return -EOPNOTSUPP;
1745+
1746+ if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
1747+ return -EOPNOTSUPP;
1748+
1749+ cb = mtk_eth_setup_tc_block_cb;
1750+ f->driver_block_list = &block_cb_list;
1751+
1752+ switch (f->command) {
1753+ case FLOW_BLOCK_BIND:
1754+ block_cb = flow_block_cb_lookup(f->block, cb, dev);
1755+ if (block_cb) {
1756+ flow_block_cb_incref(block_cb);
1757+ return 0;
1758+ }
1759+ block_cb = flow_block_cb_alloc(cb, dev, dev, NULL);
1760+ if (IS_ERR(block_cb))
1761+ return PTR_ERR(block_cb);
1762+
1763+ flow_block_cb_add(block_cb, f);
1764+ list_add_tail(&block_cb->driver_list, &block_cb_list);
1765+ return 0;
1766+ case FLOW_BLOCK_UNBIND:
1767+ block_cb = flow_block_cb_lookup(f->block, cb, dev);
1768+ if (!block_cb)
1769+ return -ENOENT;
1770+
1771+ if (flow_block_cb_decref(block_cb)) {
1772+ flow_block_cb_remove(block_cb, f);
1773+ list_del(&block_cb->driver_list);
1774+ }
1775+ return 0;
1776+ default:
1777+ return -EOPNOTSUPP;
1778+ }
1779+}
1780+
1781+int mtk_eth_setup_tc(struct net_device *dev, enum tc_setup_type type,
1782+ void *type_data)
1783+{
1784+ if (type == TC_SETUP_FT)
1785+ return mtk_eth_setup_tc_block(dev, type_data);
1786+
1787+ return -EOPNOTSUPP;
1788+}
1789+
1790+int mtk_eth_offload_init(struct mtk_eth *eth)
1791+{
1792+ if (!eth->ppe.foe_table)
1793+ return 0;
1794+
1795+ return rhashtable_init(&eth->flow_table, &mtk_flow_ht_params);
1796+}
1797diff --git a/drivers/net/ethernet/mediatek/mtk_ppe_regs.h b/drivers/net/ethernet/mediatek/mtk_ppe_regs.h
1798new file mode 100644
1799index 000000000..0c45ea090
1800--- /dev/null
1801+++ b/drivers/net/ethernet/mediatek/mtk_ppe_regs.h
1802@@ -0,0 +1,144 @@
1803+// SPDX-License-Identifier: GPL-2.0-only
1804+/* Copyright (C) 2020 Felix Fietkau <nbd@nbd.name> */
1805+
1806+#ifndef __MTK_PPE_REGS_H
1807+#define __MTK_PPE_REGS_H
1808+
1809+#define MTK_PPE_GLO_CFG 0x200
1810+#define MTK_PPE_GLO_CFG_EN BIT(0)
1811+#define MTK_PPE_GLO_CFG_TSID_EN BIT(1)
1812+#define MTK_PPE_GLO_CFG_IP4_L4_CS_DROP BIT(2)
1813+#define MTK_PPE_GLO_CFG_IP4_CS_DROP BIT(3)
1814+#define MTK_PPE_GLO_CFG_TTL0_DROP BIT(4)
1815+#define MTK_PPE_GLO_CFG_PPE_BSWAP BIT(5)
1816+#define MTK_PPE_GLO_CFG_PSE_HASH_OFS BIT(6)
1817+#define MTK_PPE_GLO_CFG_MCAST_TB_EN BIT(7)
1818+#define MTK_PPE_GLO_CFG_FLOW_DROP_KA BIT(8)
1819+#define MTK_PPE_GLO_CFG_FLOW_DROP_UPDATE BIT(9)
1820+#define MTK_PPE_GLO_CFG_UDP_LITE_EN BIT(10)
1821+#define MTK_PPE_GLO_CFG_UDP_LEN_DROP BIT(11)
1822+#define MTK_PPE_GLO_CFG_MCAST_ENTRIES GNEMASK(13, 12)
1823+#define MTK_PPE_GLO_CFG_BUSY BIT(31)
1824+
1825+#define MTK_PPE_FLOW_CFG 0x204
1826+#define MTK_PPE_FLOW_CFG_IP4_TCP_FRAG BIT(6)
1827+#define MTK_PPE_FLOW_CFG_IP4_UDP_FRAG BIT(7)
1828+#define MTK_PPE_FLOW_CFG_IP6_3T_ROUTE BIT(8)
1829+#define MTK_PPE_FLOW_CFG_IP6_5T_ROUTE BIT(9)
1830+#define MTK_PPE_FLOW_CFG_IP6_6RD BIT(10)
1831+#define MTK_PPE_FLOW_CFG_IP4_NAT BIT(12)
1832+#define MTK_PPE_FLOW_CFG_IP4_NAPT BIT(13)
1833+#define MTK_PPE_FLOW_CFG_IP4_DSLITE BIT(14)
1834+#define MTK_PPE_FLOW_CFG_L2_BRIDGE BIT(15)
1835+#define MTK_PPE_FLOW_CFG_IP_PROTO_BLACKLIST BIT(16)
1836+#define MTK_PPE_FLOW_CFG_IP4_NAT_FRAG BIT(17)
1837+#define MTK_PPE_FLOW_CFG_IP4_HASH_FLOW_LABEL BIT(18)
1838+#define MTK_PPE_FLOW_CFG_IP4_HASH_GRE_KEY BIT(19)
1839+#define MTK_PPE_FLOW_CFG_IP6_HASH_GRE_KEY BIT(20)
1840+
1841+#define MTK_PPE_IP_PROTO_CHK 0x208
1842+#define MTK_PPE_IP_PROTO_CHK_IPV4 GENMASK(15, 0)
1843+#define MTK_PPE_IP_PROTO_CHK_IPV6 GENMASK(31, 16)
1844+
1845+#define MTK_PPE_TB_CFG 0x21c
1846+#define MTK_PPE_TB_CFG_ENTRY_NUM GENMASK(2, 0)
1847+#define MTK_PPE_TB_CFG_ENTRY_80B BIT(3)
1848+#define MTK_PPE_TB_CFG_SEARCH_MISS GENMASK(5, 4)
1849+#define MTK_PPE_TB_CFG_AGE_PREBIND BIT(6)
1850+#define MTK_PPE_TB_CFG_AGE_NON_L4 BIT(7)
1851+#define MTK_PPE_TB_CFG_AGE_UNBIND BIT(8)
1852+#define MTK_PPE_TB_CFG_AGE_TCP BIT(9)
1853+#define MTK_PPE_TB_CFG_AGE_UDP BIT(10)
1854+#define MTK_PPE_TB_CFG_AGE_TCP_FIN BIT(11)
1855+#define MTK_PPE_TB_CFG_KEEPALIVE GENMASK(13, 12)
1856+#define MTK_PPE_TB_CFG_HASH_MODE GENMASK(15, 14)
1857+#define MTK_PPE_TB_CFG_SCAN_MODE GENMASK(17, 16)
1858+#define MTK_PPE_TB_CFG_HASH_DEBUG GENMASK(19, 18)
1859+
1860+enum {
1861+ MTK_PPE_SCAN_MODE_DISABLED,
1862+ MTK_PPE_SCAN_MODE_CHECK_AGE,
1863+ MTK_PPE_SCAN_MODE_KEEPALIVE_AGE,
1864+};
1865+
1866+enum {
1867+ MTK_PPE_KEEPALIVE_DISABLE,
1868+ MTK_PPE_KEEPALIVE_UNICAST_CPU,
1869+ MTK_PPE_KEEPALIVE_DUP_CPU = 3,
1870+};
1871+
1872+enum {
1873+ MTK_PPE_SEARCH_MISS_ACTION_DROP,
1874+ MTK_PPE_SEARCH_MISS_ACTION_FORWARD = 2,
1875+ MTK_PPE_SEARCH_MISS_ACTION_FORWARD_BUILD = 3,
1876+};
1877+
1878+#define MTK_PPE_TB_BASE 0x220
1879+
1880+#define MTK_PPE_TB_USED 0x224
1881+#define MTK_PPE_TB_USED_NUM GENMASK(13, 0)
1882+
1883+#define MTK_PPE_BIND_RATE 0x228
1884+#define MTK_PPE_BIND_RATE_BIND GENMASK(15, 0)
1885+#define MTK_PPE_BIND_RATE_PREBIND GENMASK(31, 16)
1886+
1887+#define MTK_PPE_BIND_LIMIT0 0x22c
1888+#define MTK_PPE_BIND_LIMIT0_QUARTER GENMASK(13, 0)
1889+#define MTK_PPE_BIND_LIMIT0_HALF GENMASK(29, 16)
1890+
1891+#define MTK_PPE_BIND_LIMIT1 0x230
1892+#define MTK_PPE_BIND_LIMIT1_FULL GENMASK(13, 0)
1893+#define MTK_PPE_BIND_LIMIT1_NON_L4 GENMASK(23, 16)
1894+
1895+#define MTK_PPE_KEEPALIVE 0x234
1896+#define MTK_PPE_KEEPALIVE_TIME GENMASK(15, 0)
1897+#define MTK_PPE_KEEPALIVE_TIME_TCP GENMASK(23, 16)
1898+#define MTK_PPE_KEEPALIVE_TIME_UDP GENMASK(31, 24)
1899+
1900+#define MTK_PPE_UNBIND_AGE 0x238
1901+#define MTK_PPE_UNBIND_AGE_MIN_PACKETS GENMASK(31, 16)
1902+#define MTK_PPE_UNBIND_AGE_DELTA GENMASK(7, 0)
1903+
1904+#define MTK_PPE_BIND_AGE0 0x23c
1905+#define MTK_PPE_BIND_AGE0_DELTA_NON_L4 GENMASK(30, 16)
1906+#define MTK_PPE_BIND_AGE0_DELTA_UDP GENMASK(14, 0)
1907+
1908+#define MTK_PPE_BIND_AGE1 0x240
1909+#define MTK_PPE_BIND_AGE1_DELTA_TCP_FIN GENMASK(30, 16)
1910+#define MTK_PPE_BIND_AGE1_DELTA_TCP GENMASK(14, 0)
1911+
1912+#define MTK_PPE_HASH_SEED 0x244
1913+
1914+#define MTK_PPE_DEFAULT_CPU_PORT 0x248
1915+#define MTK_PPE_DEFAULT_CPU_PORT_MASK(_n) (GENMASK(2, 0) << ((_n) * 4))
1916+
1917+#define MTK_PPE_MTU_DROP 0x308
1918+
1919+#define MTK_PPE_VLAN_MTU0 0x30c
1920+#define MTK_PPE_VLAN_MTU0_NONE GENMASK(13, 0)
1921+#define MTK_PPE_VLAN_MTU0_1TAG GENMASK(29, 16)
1922+
1923+#define MTK_PPE_VLAN_MTU1 0x310
1924+#define MTK_PPE_VLAN_MTU1_2TAG GENMASK(13, 0)
1925+#define MTK_PPE_VLAN_MTU1_3TAG GENMASK(29, 16)
1926+
1927+#define MTK_PPE_VPM_TPID 0x318
1928+
1929+#define MTK_PPE_CACHE_CTL 0x320
1930+#define MTK_PPE_CACHE_CTL_EN BIT(0)
1931+#define MTK_PPE_CACHE_CTL_LOCK_CLR BIT(4)
1932+#define MTK_PPE_CACHE_CTL_REQ BIT(8)
1933+#define MTK_PPE_CACHE_CTL_CLEAR BIT(9)
1934+#define MTK_PPE_CACHE_CTL_CMD GENMASK(13, 12)
1935+
1936+#define MTK_PPE_MIB_CFG 0x334
1937+#define MTK_PPE_MIB_CFG_EN BIT(0)
1938+#define MTK_PPE_MIB_CFG_RD_CLR BIT(1)
1939+
1940+#define MTK_PPE_MIB_TB_BASE 0x338
1941+
1942+#define MTK_PPE_MIB_CACHE_CTL 0x350
1943+#define MTK_PPE_MIB_CACHE_CTL_EN BIT(0)
1944+#define MTK_PPE_MIB_CACHE_CTL_FLUSH BIT(2)
1945+
1946+#endif
1947diff --git a/drivers/net/ppp/ppp_generic.c b/drivers/net/ppp/ppp_generic.c
1948index a085213dc..813e30495 100644
1949--- a/drivers/net/ppp/ppp_generic.c
1950+++ b/drivers/net/ppp/ppp_generic.c
1951@@ -1378,12 +1378,34 @@ static void ppp_dev_priv_destructor(struct net_device *dev)
1952 ppp_destroy_interface(ppp);
1953 }
1954
1955+static int ppp_fill_forward_path(struct net_device_path_ctx *ctx,
1956+ struct net_device_path *path)
1957+{
1958+ struct ppp *ppp = netdev_priv(ctx->dev);
1959+ struct ppp_channel *chan;
1960+ struct channel *pch;
1961+
1962+ if (ppp->flags & SC_MULTILINK)
1963+ return -EOPNOTSUPP;
1964+
1965+ if (list_empty(&ppp->channels))
1966+ return -ENODEV;
1967+
1968+ pch = list_first_entry(&ppp->channels, struct channel, clist);
1969+ chan = pch->chan;
1970+ if (!chan->ops->fill_forward_path)
1971+ return -EOPNOTSUPP;
1972+
1973+ return chan->ops->fill_forward_path(ctx, path, chan);
1974+}
1975+
1976 static const struct net_device_ops ppp_netdev_ops = {
1977 .ndo_init = ppp_dev_init,
1978 .ndo_uninit = ppp_dev_uninit,
1979 .ndo_start_xmit = ppp_start_xmit,
1980 .ndo_do_ioctl = ppp_net_ioctl,
1981 .ndo_get_stats64 = ppp_get_stats64,
1982+ .ndo_fill_forward_path = ppp_fill_forward_path,
1983 };
1984
1985 static struct device_type ppp_type = {
1986diff --git a/drivers/net/ppp/pppoe.c b/drivers/net/ppp/pppoe.c
1987index 087b01684..7a8c246ab 100644
1988--- a/drivers/net/ppp/pppoe.c
1989+++ b/drivers/net/ppp/pppoe.c
1990@@ -974,8 +974,32 @@ static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
1991 return __pppoe_xmit(sk, skb);
1992 }
1993
1994+static int pppoe_fill_forward_path(struct net_device_path_ctx *ctx,
1995+ struct net_device_path *path,
1996+ const struct ppp_channel *chan)
1997+{
1998+ struct sock *sk = (struct sock *)chan->private;
1999+ struct pppox_sock *po = pppox_sk(sk);
2000+ struct net_device *dev = po->pppoe_dev;
2001+
2002+ if (sock_flag(sk, SOCK_DEAD) ||
2003+ !(sk->sk_state & PPPOX_CONNECTED) || !dev)
2004+ return -1;
2005+
2006+ path->type = DEV_PATH_PPPOE;
2007+ path->encap.proto = htons(ETH_P_PPP_SES);
2008+ path->encap.id = be16_to_cpu(po->num);
2009+ memcpy(path->encap.h_dest, po->pppoe_pa.remote, ETH_ALEN);
2010+ memcpy(ctx->daddr, po->pppoe_pa.remote, ETH_ALEN);
2011+ path->dev = ctx->dev;
2012+ ctx->dev = dev;
2013+
2014+ return 0;
2015+}
2016+
2017 static const struct ppp_channel_ops pppoe_chan_ops = {
2018 .start_xmit = pppoe_xmit,
2019+ .fill_forward_path = pppoe_fill_forward_path,
2020 };
2021
2022 static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
2023diff --git a/include/linux/netdevice.h b/include/linux/netdevice.h
2024index 38af42bf8..9f64504ac 100644
2025--- a/include/linux/netdevice.h
2026+++ b/include/linux/netdevice.h
2027@@ -829,6 +829,59 @@ typedef u16 (*select_queue_fallback_t)(struct net_device *dev,
2028 struct sk_buff *skb,
2029 struct net_device *sb_dev);
2030
2031+enum net_device_path_type {
2032+ DEV_PATH_ETHERNET = 0,
2033+ DEV_PATH_VLAN,
2034+ DEV_PATH_BRIDGE,
2035+ DEV_PATH_PPPOE,
2036+ DEV_PATH_DSA,
2037+};
2038+
2039+struct net_device_path {
2040+ enum net_device_path_type type;
2041+ const struct net_device *dev;
2042+ union {
2043+ struct {
2044+ u16 id;
2045+ __be16 proto;
2046+ u8 h_dest[ETH_ALEN];
2047+ } encap;
2048+ struct {
2049+ enum {
2050+ DEV_PATH_BR_VLAN_KEEP,
2051+ DEV_PATH_BR_VLAN_TAG,
2052+ DEV_PATH_BR_VLAN_UNTAG,
2053+ DEV_PATH_BR_VLAN_UNTAG_HW,
2054+ } vlan_mode;
2055+ u16 vlan_id;
2056+ __be16 vlan_proto;
2057+ } bridge;
2058+ struct {
2059+ int port;
2060+ u16 proto;
2061+ } dsa;
2062+ };
2063+};
2064+
2065+#define NET_DEVICE_PATH_STACK_MAX 5
2066+#define NET_DEVICE_PATH_VLAN_MAX 2
2067+
2068+struct net_device_path_stack {
2069+ int num_paths;
2070+ struct net_device_path path[NET_DEVICE_PATH_STACK_MAX];
2071+};
2072+
2073+struct net_device_path_ctx {
2074+ const struct net_device *dev;
2075+ u8 daddr[ETH_ALEN];
2076+
2077+ int num_vlans;
2078+ struct {
2079+ u16 id;
2080+ __be16 proto;
2081+ } vlan[NET_DEVICE_PATH_VLAN_MAX];
2082+};
2083+
2084 enum tc_setup_type {
2085 TC_SETUP_QDISC_MQPRIO,
2086 TC_SETUP_CLSU32,
2087@@ -844,6 +897,7 @@ enum tc_setup_type {
2088 TC_SETUP_ROOT_QDISC,
2089 TC_SETUP_QDISC_GRED,
2090 TC_SETUP_QDISC_TAPRIO,
2091+ TC_SETUP_FT,
2092 };
2093
2094 /* These structures hold the attributes of bpf state that are being passed
2095@@ -1239,6 +1293,8 @@ struct tlsdev_ops;
2096 * Get devlink port instance associated with a given netdev.
2097 * Called with a reference on the netdevice and devlink locks only,
2098 * rtnl_lock is not held.
2099+ * int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx, struct net_device_path *path);
2100+ * Get the forwarding path to reach the real device from the HW destination address
2101 */
2102 struct net_device_ops {
2103 int (*ndo_init)(struct net_device *dev);
2104@@ -1436,6 +1492,8 @@ struct net_device_ops {
2105 int (*ndo_xsk_wakeup)(struct net_device *dev,
2106 u32 queue_id, u32 flags);
2107 struct devlink_port * (*ndo_get_devlink_port)(struct net_device *dev);
2108+ int (*ndo_fill_forward_path)(struct net_device_path_ctx *ctx,
2109+ struct net_device_path *path);
2110 };
2111
2112 /**
2113@@ -2661,6 +2719,8 @@ void dev_remove_offload(struct packet_offload *po);
2114
2115 int dev_get_iflink(const struct net_device *dev);
2116 int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb);
2117+int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
2118+ struct net_device_path_stack *stack);
2119 struct net_device *__dev_get_by_flags(struct net *net, unsigned short flags,
2120 unsigned short mask);
2121 struct net_device *dev_get_by_name(struct net *net, const char *name);
2122diff --git a/include/linux/ppp_channel.h b/include/linux/ppp_channel.h
2123index 98966064e..91f9a9283 100644
2124--- a/include/linux/ppp_channel.h
2125+++ b/include/linux/ppp_channel.h
2126@@ -28,6 +28,9 @@ struct ppp_channel_ops {
2127 int (*start_xmit)(struct ppp_channel *, struct sk_buff *);
2128 /* Handle an ioctl call that has come in via /dev/ppp. */
2129 int (*ioctl)(struct ppp_channel *, unsigned int, unsigned long);
2130+ int (*fill_forward_path)(struct net_device_path_ctx *,
2131+ struct net_device_path *,
2132+ const struct ppp_channel *);
2133 };
2134
2135 struct ppp_channel {
2136diff --git a/include/net/dsa.h b/include/net/dsa.h
2137index 05f66d487..cafc74218 100644
2138--- a/include/net/dsa.h
2139+++ b/include/net/dsa.h
2140@@ -561,6 +561,8 @@ struct dsa_switch_ops {
2141 struct sk_buff *skb);
2142 };
2143
2144+struct dsa_port *dsa_port_from_netdev(struct net_device *netdev);
2145+
2146 struct dsa_switch_driver {
2147 struct list_head list;
2148 const struct dsa_switch_ops *ops;
2149@@ -653,6 +655,14 @@ static inline int call_dsa_notifiers(unsigned long val, struct net_device *dev,
2150 #define BRCM_TAG_GET_PORT(v) ((v) >> 8)
2151 #define BRCM_TAG_GET_QUEUE(v) ((v) & 0xff)
2152
2153+#if IS_ENABLED(CONFIG_NET_DSA)
2154+bool dsa_slave_dev_check(const struct net_device *dev);
2155+#else
2156+static inline bool dsa_slave_dev_check(const struct net_device *dev)
2157+{
2158+ return false;
2159+}
2160+#endif
2161
2162 netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
2163 int dsa_port_get_phy_strings(struct dsa_port *dp, uint8_t *data);
2164diff --git a/include/net/flow_offload.h b/include/net/flow_offload.h
2165index c6f7bd22d..59b873653 100644
2166--- a/include/net/flow_offload.h
2167+++ b/include/net/flow_offload.h
2168@@ -138,6 +138,7 @@ enum flow_action_id {
2169 FLOW_ACTION_MPLS_PUSH,
2170 FLOW_ACTION_MPLS_POP,
2171 FLOW_ACTION_MPLS_MANGLE,
2172+ FLOW_ACTION_PPPOE_PUSH,
2173 NUM_FLOW_ACTIONS,
2174 };
2175
2176@@ -213,6 +214,9 @@ struct flow_action_entry {
2177 u8 bos;
2178 u8 ttl;
2179 } mpls_mangle;
2180+ struct { /* FLOW_ACTION_PPPOE_PUSH */
2181+ u16 sid;
2182+ } pppoe;
2183 };
2184 };
2185
2186diff --git a/include/net/ip6_route.h b/include/net/ip6_route.h
2187index 2c739fc75..89ab8f180 100644
2188--- a/include/net/ip6_route.h
2189+++ b/include/net/ip6_route.h
2190@@ -314,12 +314,13 @@ static inline bool rt6_duplicate_nexthop(struct fib6_info *a, struct fib6_info *
2191 !lwtunnel_cmp_encap(nha->fib_nh_lws, nhb->fib_nh_lws);
2192 }
2193
2194-static inline unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
2195+static inline unsigned int ip6_dst_mtu_maybe_forward(const struct dst_entry *dst,
2196+ bool forwarding)
2197 {
2198 struct inet6_dev *idev;
2199 unsigned int mtu;
2200
2201- if (dst_metric_locked(dst, RTAX_MTU)) {
2202+ if (!forwarding || dst_metric_locked(dst, RTAX_MTU)) {
2203 mtu = dst_metric_raw(dst, RTAX_MTU);
2204 if (mtu)
2205 goto out;
2206diff --git a/include/net/netfilter/ipv6/nf_conntrack_ipv6.h b/include/net/netfilter/ipv6/nf_conntrack_ipv6.h
2207index 7b3c873f8..e95483192 100644
2208--- a/include/net/netfilter/ipv6/nf_conntrack_ipv6.h
2209+++ b/include/net/netfilter/ipv6/nf_conntrack_ipv6.h
2210@@ -4,7 +4,4 @@
2211
2212 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6;
2213
2214-#include <linux/sysctl.h>
2215-extern struct ctl_table nf_ct_ipv6_sysctl_table[];
2216-
2217 #endif /* _NF_CONNTRACK_IPV6_H*/
2218diff --git a/include/net/netfilter/nf_conntrack.h b/include/net/netfilter/nf_conntrack.h
2219index 90690e37a..ce0bc3e62 100644
2220--- a/include/net/netfilter/nf_conntrack.h
2221+++ b/include/net/netfilter/nf_conntrack.h
2222@@ -279,6 +279,18 @@ static inline bool nf_ct_should_gc(const struct nf_conn *ct)
2223 !nf_ct_is_dying(ct);
2224 }
2225
2226+#define NF_CT_DAY (86400 * HZ)
2227+
2228+/* Set an arbitrary timeout large enough not to ever expire, this save
2229+ * us a check for the IPS_OFFLOAD_BIT from the packet path via
2230+ * nf_ct_is_expired().
2231+ */
2232+static inline void nf_ct_offload_timeout(struct nf_conn *ct)
2233+{
2234+ if (nf_ct_expires(ct) < NF_CT_DAY / 2)
2235+ WRITE_ONCE(ct->timeout, nfct_time_stamp + NF_CT_DAY);
2236+}
2237+
2238 struct kernel_param;
2239
2240 int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp);
2241diff --git a/include/net/netfilter/nf_conntrack_acct.h b/include/net/netfilter/nf_conntrack_acct.h
2242index f7a060c6e..7f44a7715 100644
2243--- a/include/net/netfilter/nf_conntrack_acct.h
2244+++ b/include/net/netfilter/nf_conntrack_acct.h
2245@@ -65,6 +65,17 @@ static inline void nf_ct_set_acct(struct net *net, bool enable)
2246 #endif
2247 }
2248
2249+void nf_ct_acct_add(struct nf_conn *ct, u32 dir, unsigned int packets,
2250+ unsigned int bytes);
2251+
2252+static inline void nf_ct_acct_update(struct nf_conn *ct, u32 dir,
2253+ unsigned int bytes)
2254+{
2255+#if IS_ENABLED(CONFIG_NF_CONNTRACK)
2256+ nf_ct_acct_add(ct, dir, 1, bytes);
2257+#endif
2258+}
2259+
2260 void nf_conntrack_acct_pernet_init(struct net *net);
2261
2262 int nf_conntrack_acct_init(void);
2263diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h
developerb7c46752022-07-04 19:51:38 +08002264index 68d7fc92..7cf89767 100644
developer8cb3ac72022-07-04 10:55:14 +08002265--- a/include/net/netfilter/nf_flow_table.h
2266+++ b/include/net/netfilter/nf_flow_table.h
2267@@ -8,31 +8,99 @@
2268 #include <linux/rcupdate.h>
2269 #include <linux/netfilter.h>
2270 #include <linux/netfilter/nf_conntrack_tuple_common.h>
2271+#include <net/flow_offload.h>
2272 #include <net/dst.h>
2273+#include <linux/if_pppox.h>
2274+#include <linux/ppp_defs.h>
2275
2276 struct nf_flowtable;
2277+struct nf_flow_rule;
2278+struct flow_offload;
2279+enum flow_offload_tuple_dir;
2280+
2281+struct nf_flow_key {
2282+ struct flow_dissector_key_meta meta;
2283+ struct flow_dissector_key_control control;
2284+ struct flow_dissector_key_control enc_control;
2285+ struct flow_dissector_key_basic basic;
2286+ struct flow_dissector_key_vlan vlan;
2287+ struct flow_dissector_key_vlan cvlan;
2288+ union {
2289+ struct flow_dissector_key_ipv4_addrs ipv4;
2290+ struct flow_dissector_key_ipv6_addrs ipv6;
2291+ };
2292+ struct flow_dissector_key_keyid enc_key_id;
2293+ union {
2294+ struct flow_dissector_key_ipv4_addrs enc_ipv4;
2295+ struct flow_dissector_key_ipv6_addrs enc_ipv6;
2296+ };
2297+ struct flow_dissector_key_tcp tcp;
2298+ struct flow_dissector_key_ports tp;
2299+} __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
2300+
2301+struct nf_flow_match {
2302+ struct flow_dissector dissector;
2303+ struct nf_flow_key key;
2304+ struct nf_flow_key mask;
2305+};
2306+
2307+struct nf_flow_rule {
2308+ struct nf_flow_match match;
2309+ struct flow_rule *rule;
2310+};
2311
2312 struct nf_flowtable_type {
2313 struct list_head list;
2314 int family;
2315 int (*init)(struct nf_flowtable *ft);
2316+ int (*setup)(struct nf_flowtable *ft,
2317+ struct net_device *dev,
2318+ enum flow_block_command cmd);
2319+ int (*action)(struct net *net,
2320+ const struct flow_offload *flow,
2321+ enum flow_offload_tuple_dir dir,
2322+ struct nf_flow_rule *flow_rule);
2323 void (*free)(struct nf_flowtable *ft);
2324 nf_hookfn *hook;
2325 struct module *owner;
2326 };
2327
2328+enum nf_flowtable_flags {
2329+ NF_FLOWTABLE_HW_OFFLOAD = 0x1, /* NFT_FLOWTABLE_HW_OFFLOAD */
2330+ NF_FLOWTABLE_COUNTER = 0x2, /* NFT_FLOWTABLE_COUNTER */
2331+};
2332+
2333 struct nf_flowtable {
2334 struct list_head list;
2335 struct rhashtable rhashtable;
2336+ int priority;
2337 const struct nf_flowtable_type *type;
2338 struct delayed_work gc_work;
2339+ unsigned int flags;
2340+ struct flow_block flow_block;
2341+ struct rw_semaphore flow_block_lock; /* Guards flow_block */
2342+ possible_net_t net;
2343 };
2344
2345+static inline bool nf_flowtable_hw_offload(struct nf_flowtable *flowtable)
2346+{
2347+ return flowtable->flags & NF_FLOWTABLE_HW_OFFLOAD;
2348+}
2349+
2350 enum flow_offload_tuple_dir {
2351 FLOW_OFFLOAD_DIR_ORIGINAL = IP_CT_DIR_ORIGINAL,
2352 FLOW_OFFLOAD_DIR_REPLY = IP_CT_DIR_REPLY,
2353- FLOW_OFFLOAD_DIR_MAX = IP_CT_DIR_MAX
2354 };
2355+#define FLOW_OFFLOAD_DIR_MAX IP_CT_DIR_MAX
2356+
2357+enum flow_offload_xmit_type {
2358+ FLOW_OFFLOAD_XMIT_UNSPEC = 0,
2359+ FLOW_OFFLOAD_XMIT_NEIGH,
2360+ FLOW_OFFLOAD_XMIT_XFRM,
2361+ FLOW_OFFLOAD_XMIT_DIRECT,
2362+};
2363+
2364+#define NF_FLOW_TABLE_ENCAP_MAX 2
2365
2366 struct flow_offload_tuple {
2367 union {
developerb7c46752022-07-04 19:51:38 +08002368@@ -52,13 +120,30 @@ struct flow_offload_tuple {
developer8cb3ac72022-07-04 10:55:14 +08002369
2370 u8 l3proto;
2371 u8 l4proto;
2372- u8 dir;
2373+ struct {
2374+ u16 id;
2375+ __be16 proto;
2376+ } encap[NF_FLOW_TABLE_ENCAP_MAX];
2377
2378- u16 mtu;
2379+ /* All members above are keys for lookups, see flow_offload_hash(). */
2380+ struct { } __hash;
2381
developerb7c46752022-07-04 19:51:38 +08002382- struct {
2383- struct dst_entry *dst_cache;
2384- u32 dst_cookie;
developer8cb3ac72022-07-04 10:55:14 +08002385+ u8 dir:2,
2386+ xmit_type:2,
2387+ encap_num:2,
2388+ in_vlan_ingress:2;
2389+ u16 mtu;
2390+ union {
2391+ struct {
2392+ struct dst_entry *dst_cache;
2393+ u32 dst_cookie;
2394+ };
2395+ struct {
2396+ u32 ifidx;
2397+ u32 hw_ifidx;
2398+ u8 h_source[ETH_ALEN];
2399+ u8 h_dest[ETH_ALEN];
2400+ } out;
developerb7c46752022-07-04 19:51:38 +08002401 };
developer8cb3ac72022-07-04 10:55:14 +08002402 };
2403
developerb7c46752022-07-04 19:51:38 +08002404@@ -67,52 +152,139 @@ struct flow_offload_tuple_rhash {
developer8cb3ac72022-07-04 10:55:14 +08002405 struct flow_offload_tuple tuple;
2406 };
2407
2408-#define FLOW_OFFLOAD_SNAT 0x1
2409-#define FLOW_OFFLOAD_DNAT 0x2
2410-#define FLOW_OFFLOAD_DYING 0x4
2411-#define FLOW_OFFLOAD_TEARDOWN 0x8
2412+enum nf_flow_flags {
2413+ NF_FLOW_SNAT,
2414+ NF_FLOW_DNAT,
2415+ NF_FLOW_TEARDOWN,
2416+ NF_FLOW_HW,
2417+ NF_FLOW_HW_DYING,
2418+ NF_FLOW_HW_DEAD,
2419+ NF_FLOW_HW_PENDING,
2420+};
2421+
2422+enum flow_offload_type {
2423+ NF_FLOW_OFFLOAD_UNSPEC = 0,
2424+ NF_FLOW_OFFLOAD_ROUTE,
2425+};
2426
2427 struct flow_offload {
2428 struct flow_offload_tuple_rhash tuplehash[FLOW_OFFLOAD_DIR_MAX];
2429- u32 flags;
2430- union {
2431- /* Your private driver data here. */
2432- u32 timeout;
2433- };
2434+ struct nf_conn *ct;
2435+ unsigned long flags;
2436+ u16 type;
2437+ u32 timeout;
2438+ struct rcu_head rcu_head;
2439 };
2440
2441 #define NF_FLOW_TIMEOUT (30 * HZ)
2442+#define nf_flowtable_time_stamp (u32)jiffies
2443+
2444+unsigned long flow_offload_get_timeout(struct flow_offload *flow);
2445+
2446+static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
2447+{
2448+ return (__s32)(timeout - nf_flowtable_time_stamp);
2449+}
2450
2451 struct nf_flow_route {
2452 struct {
2453- struct dst_entry *dst;
2454+ struct dst_entry *dst;
2455+ struct {
2456+ u32 ifindex;
2457+ struct {
2458+ u16 id;
2459+ __be16 proto;
2460+ } encap[NF_FLOW_TABLE_ENCAP_MAX];
2461+ u8 num_encaps:2,
2462+ ingress_vlans:2;
2463+ } in;
2464+ struct {
2465+ u32 ifindex;
2466+ u32 hw_ifindex;
2467+ u8 h_source[ETH_ALEN];
2468+ u8 h_dest[ETH_ALEN];
2469+ } out;
2470+ enum flow_offload_xmit_type xmit_type;
2471 } tuple[FLOW_OFFLOAD_DIR_MAX];
2472 };
2473
2474-struct flow_offload *flow_offload_alloc(struct nf_conn *ct,
2475- struct nf_flow_route *route);
2476+struct flow_offload *flow_offload_alloc(struct nf_conn *ct);
2477 void flow_offload_free(struct flow_offload *flow);
2478
2479+static inline int
2480+nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
2481+ flow_setup_cb_t *cb, void *cb_priv)
2482+{
2483+ struct flow_block *block = &flow_table->flow_block;
2484+ struct flow_block_cb *block_cb;
2485+ int err = 0;
2486+
2487+ down_write(&flow_table->flow_block_lock);
2488+ block_cb = flow_block_cb_lookup(block, cb, cb_priv);
2489+ if (block_cb) {
2490+ err = -EEXIST;
2491+ goto unlock;
2492+ }
2493+
2494+ block_cb = flow_block_cb_alloc(cb, cb_priv, cb_priv, NULL);
2495+ if (IS_ERR(block_cb)) {
2496+ err = PTR_ERR(block_cb);
2497+ goto unlock;
2498+ }
2499+
2500+ list_add_tail(&block_cb->list, &block->cb_list);
2501+
2502+unlock:
2503+ up_write(&flow_table->flow_block_lock);
2504+ return err;
2505+}
2506+
2507+static inline void
2508+nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
2509+ flow_setup_cb_t *cb, void *cb_priv)
2510+{
2511+ struct flow_block *block = &flow_table->flow_block;
2512+ struct flow_block_cb *block_cb;
2513+
2514+ down_write(&flow_table->flow_block_lock);
2515+ block_cb = flow_block_cb_lookup(block, cb, cb_priv);
2516+ if (block_cb) {
2517+ list_del(&block_cb->list);
2518+ flow_block_cb_free(block_cb);
2519+ } else {
2520+ WARN_ON(true);
2521+ }
2522+ up_write(&flow_table->flow_block_lock);
2523+}
2524+
2525+int flow_offload_route_init(struct flow_offload *flow,
2526+ const struct nf_flow_route *route);
2527+
2528 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
2529+void flow_offload_refresh(struct nf_flowtable *flow_table,
2530+ struct flow_offload *flow);
2531+
2532 struct flow_offload_tuple_rhash *flow_offload_lookup(struct nf_flowtable *flow_table,
2533 struct flow_offload_tuple *tuple);
2534+void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
2535+ struct net_device *dev);
2536 void nf_flow_table_cleanup(struct net_device *dev);
2537
2538 int nf_flow_table_init(struct nf_flowtable *flow_table);
2539 void nf_flow_table_free(struct nf_flowtable *flow_table);
2540
2541 void flow_offload_teardown(struct flow_offload *flow);
2542-static inline void flow_offload_dead(struct flow_offload *flow)
2543-{
2544- flow->flags |= FLOW_OFFLOAD_DYING;
2545-}
2546
2547-int nf_flow_snat_port(const struct flow_offload *flow,
2548- struct sk_buff *skb, unsigned int thoff,
2549- u8 protocol, enum flow_offload_tuple_dir dir);
2550-int nf_flow_dnat_port(const struct flow_offload *flow,
2551- struct sk_buff *skb, unsigned int thoff,
2552- u8 protocol, enum flow_offload_tuple_dir dir);
2553+int nf_flow_table_iterate(struct nf_flowtable *flow_table,
2554+ void (*iter)(struct flow_offload *flow, void *data),
2555+ void *data);
2556+
2557+void nf_flow_snat_port(const struct flow_offload *flow,
2558+ struct sk_buff *skb, unsigned int thoff,
2559+ u8 protocol, enum flow_offload_tuple_dir dir);
2560+void nf_flow_dnat_port(const struct flow_offload *flow,
2561+ struct sk_buff *skb, unsigned int thoff,
2562+ u8 protocol, enum flow_offload_tuple_dir dir);
2563
2564 struct flow_ports {
2565 __be16 source, dest;
developerb7c46752022-07-04 19:51:38 +08002566@@ -126,4 +298,41 @@ unsigned int nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08002567 #define MODULE_ALIAS_NF_FLOWTABLE(family) \
2568 MODULE_ALIAS("nf-flowtable-" __stringify(family))
2569
2570+void nf_flow_offload_add(struct nf_flowtable *flowtable,
2571+ struct flow_offload *flow);
2572+void nf_flow_offload_del(struct nf_flowtable *flowtable,
2573+ struct flow_offload *flow);
2574+void nf_flow_offload_stats(struct nf_flowtable *flowtable,
2575+ struct flow_offload *flow);
2576+
2577+void nf_flow_table_offload_flush(struct nf_flowtable *flowtable);
2578+int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
2579+ struct net_device *dev,
2580+ enum flow_block_command cmd);
2581+int nf_flow_rule_route_ipv4(struct net *net, const struct flow_offload *flow,
2582+ enum flow_offload_tuple_dir dir,
2583+ struct nf_flow_rule *flow_rule);
2584+int nf_flow_rule_route_ipv6(struct net *net, const struct flow_offload *flow,
2585+ enum flow_offload_tuple_dir dir,
2586+ struct nf_flow_rule *flow_rule);
2587+
2588+int nf_flow_table_offload_init(void);
2589+void nf_flow_table_offload_exit(void);
2590+
2591+static inline __be16 nf_flow_pppoe_proto(const struct sk_buff *skb)
2592+{
2593+ __be16 proto;
2594+
2595+ proto = *((__be16 *)(skb_mac_header(skb) + ETH_HLEN +
2596+ sizeof(struct pppoe_hdr)));
2597+ switch (proto) {
2598+ case htons(PPP_IP):
2599+ return htons(ETH_P_IP);
2600+ case htons(PPP_IPV6):
2601+ return htons(ETH_P_IPV6);
2602+ }
2603+
2604+ return 0;
2605+}
2606+
2607 #endif /* _NF_FLOW_TABLE_H */
2608diff --git a/include/net/netns/conntrack.h b/include/net/netns/conntrack.h
2609index 806454e76..9e3963c8f 100644
2610--- a/include/net/netns/conntrack.h
2611+++ b/include/net/netns/conntrack.h
2612@@ -27,6 +27,9 @@ struct nf_tcp_net {
2613 int tcp_loose;
2614 int tcp_be_liberal;
2615 int tcp_max_retrans;
2616+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
2617+ unsigned int offload_timeout;
2618+#endif
2619 };
2620
2621 enum udp_conntrack {
2622@@ -37,6 +40,9 @@ enum udp_conntrack {
2623
2624 struct nf_udp_net {
2625 unsigned int timeouts[UDP_CT_MAX];
2626+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
2627+ unsigned int offload_timeout;
2628+#endif
2629 };
2630
2631 struct nf_icmp_net {
2632diff --git a/include/uapi/linux/netfilter/nf_conntrack_common.h b/include/uapi/linux/netfilter/nf_conntrack_common.h
2633index 336014bf8..ae698d11c 100644
2634--- a/include/uapi/linux/netfilter/nf_conntrack_common.h
2635+++ b/include/uapi/linux/netfilter/nf_conntrack_common.h
2636@@ -105,14 +105,19 @@ enum ip_conntrack_status {
2637 IPS_OFFLOAD_BIT = 14,
2638 IPS_OFFLOAD = (1 << IPS_OFFLOAD_BIT),
2639
2640+ /* Conntrack has been offloaded to hardware. */
2641+ IPS_HW_OFFLOAD_BIT = 15,
2642+ IPS_HW_OFFLOAD = (1 << IPS_HW_OFFLOAD_BIT),
2643+
2644 /* Be careful here, modifying these bits can make things messy,
2645 * so don't let users modify them directly.
2646 */
2647 IPS_UNCHANGEABLE_MASK = (IPS_NAT_DONE_MASK | IPS_NAT_MASK |
2648 IPS_EXPECTED | IPS_CONFIRMED | IPS_DYING |
2649- IPS_SEQ_ADJUST | IPS_TEMPLATE | IPS_OFFLOAD),
2650+ IPS_SEQ_ADJUST | IPS_TEMPLATE |
2651+ IPS_OFFLOAD | IPS_HW_OFFLOAD),
2652
2653- __IPS_MAX_BIT = 15,
2654+ __IPS_MAX_BIT = 16,
2655 };
2656
2657 /* Connection tracking event types */
2658diff --git a/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
2659new file mode 100644
2660index 000000000..5841bbe0e
2661--- /dev/null
2662+++ b/include/uapi/linux/netfilter/xt_FLOWOFFLOAD.h
2663@@ -0,0 +1,17 @@
2664+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2665+#ifndef _XT_FLOWOFFLOAD_H
2666+#define _XT_FLOWOFFLOAD_H
2667+
2668+#include <linux/types.h>
2669+
2670+enum {
2671+ XT_FLOWOFFLOAD_HW = 1 << 0,
2672+
2673+ XT_FLOWOFFLOAD_MASK = XT_FLOWOFFLOAD_HW
2674+};
2675+
2676+struct xt_flowoffload_target_info {
2677+ __u32 flags;
2678+};
2679+
2680+#endif /* _XT_FLOWOFFLOAD_H */
2681diff --git a/net/8021q/vlan_dev.c b/net/8021q/vlan_dev.c
2682index 589615ec4..444ab5fae 100644
2683--- a/net/8021q/vlan_dev.c
2684+++ b/net/8021q/vlan_dev.c
2685@@ -747,6 +747,26 @@ static int vlan_dev_get_iflink(const struct net_device *dev)
2686 return real_dev->ifindex;
2687 }
2688
2689+static int vlan_dev_fill_forward_path(struct net_device_path_ctx *ctx,
2690+ struct net_device_path *path)
2691+{
2692+ struct vlan_dev_priv *vlan = vlan_dev_priv(ctx->dev);
2693+
2694+ path->type = DEV_PATH_VLAN;
2695+ path->encap.id = vlan->vlan_id;
2696+ path->encap.proto = vlan->vlan_proto;
2697+ path->dev = ctx->dev;
2698+ ctx->dev = vlan->real_dev;
2699+ if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
2700+ return -ENOSPC;
2701+
2702+ ctx->vlan[ctx->num_vlans].id = vlan->vlan_id;
2703+ ctx->vlan[ctx->num_vlans].proto = vlan->vlan_proto;
2704+ ctx->num_vlans++;
2705+
2706+ return 0;
2707+}
2708+
2709 static const struct ethtool_ops vlan_ethtool_ops = {
2710 .get_link_ksettings = vlan_ethtool_get_link_ksettings,
2711 .get_drvinfo = vlan_ethtool_get_drvinfo,
2712@@ -785,6 +805,7 @@ static const struct net_device_ops vlan_netdev_ops = {
2713 #endif
2714 .ndo_fix_features = vlan_dev_fix_features,
2715 .ndo_get_iflink = vlan_dev_get_iflink,
2716+ .ndo_fill_forward_path = vlan_dev_fill_forward_path,
2717 };
2718
2719 static void vlan_dev_free(struct net_device *dev)
2720diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c
2721index 501f77f0f..0940b44cd 100644
2722--- a/net/bridge/br_device.c
2723+++ b/net/bridge/br_device.c
2724@@ -377,6 +377,54 @@ static int br_del_slave(struct net_device *dev, struct net_device *slave_dev)
2725 return br_del_if(br, slave_dev);
2726 }
2727
2728+static int br_fill_forward_path(struct net_device_path_ctx *ctx,
2729+ struct net_device_path *path)
2730+{
2731+ struct net_bridge_fdb_entry *f;
2732+ struct net_bridge_port *dst;
2733+ struct net_bridge *br;
2734+
2735+ if (netif_is_bridge_port(ctx->dev))
2736+ return -1;
2737+
2738+ br = netdev_priv(ctx->dev);
2739+
2740+ br_vlan_fill_forward_path_pvid(br, ctx, path);
2741+
2742+ f = br_fdb_find_rcu(br, ctx->daddr, path->bridge.vlan_id);
2743+ if (!f || !f->dst)
2744+ return -1;
2745+
2746+ dst = READ_ONCE(f->dst);
2747+ if (!dst)
2748+ return -1;
2749+
2750+ if (br_vlan_fill_forward_path_mode(br, dst, path))
2751+ return -1;
2752+
2753+ path->type = DEV_PATH_BRIDGE;
2754+ path->dev = dst->br->dev;
2755+ ctx->dev = dst->dev;
2756+
2757+ switch (path->bridge.vlan_mode) {
2758+ case DEV_PATH_BR_VLAN_TAG:
2759+ if (ctx->num_vlans >= ARRAY_SIZE(ctx->vlan))
2760+ return -ENOSPC;
2761+ ctx->vlan[ctx->num_vlans].id = path->bridge.vlan_id;
2762+ ctx->vlan[ctx->num_vlans].proto = path->bridge.vlan_proto;
2763+ ctx->num_vlans++;
2764+ break;
2765+ case DEV_PATH_BR_VLAN_UNTAG_HW:
2766+ case DEV_PATH_BR_VLAN_UNTAG:
2767+ ctx->num_vlans--;
2768+ break;
2769+ case DEV_PATH_BR_VLAN_KEEP:
2770+ break;
2771+ }
2772+
2773+ return 0;
2774+}
2775+
2776 static const struct ethtool_ops br_ethtool_ops = {
2777 .get_drvinfo = br_getinfo,
2778 .get_link = ethtool_op_get_link,
2779@@ -410,6 +458,7 @@ static const struct net_device_ops br_netdev_ops = {
2780 .ndo_bridge_setlink = br_setlink,
2781 .ndo_bridge_dellink = br_dellink,
2782 .ndo_features_check = passthru_features_check,
2783+ .ndo_fill_forward_path = br_fill_forward_path,
2784 };
2785
2786 static struct device_type br_type = {
2787diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
2788index a736be8a1..4bd9e9b57 100644
2789--- a/net/bridge/br_private.h
2790+++ b/net/bridge/br_private.h
2791@@ -912,6 +912,13 @@ void br_vlan_port_event(struct net_bridge_port *p, unsigned long event);
2792 int br_vlan_bridge_event(struct net_device *dev, unsigned long event,
2793 void *ptr);
2794
2795+void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
2796+ struct net_device_path_ctx *ctx,
2797+ struct net_device_path *path);
2798+int br_vlan_fill_forward_path_mode(struct net_bridge *br,
2799+ struct net_bridge_port *dst,
2800+ struct net_device_path *path);
2801+
2802 static inline struct net_bridge_vlan_group *br_vlan_group(
2803 const struct net_bridge *br)
2804 {
2805@@ -1066,6 +1073,19 @@ static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
2806 return 0;
2807 }
2808
2809+static inline void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
2810+ struct net_device_path_ctx *ctx,
2811+ struct net_device_path *path)
2812+{
2813+}
2814+
2815+static inline int br_vlan_fill_forward_path_mode(struct net_bridge *br,
2816+ struct net_bridge_port *dst,
2817+ struct net_device_path *path)
2818+{
2819+ return 0;
2820+}
2821+
2822 static inline struct net_bridge_vlan_group *br_vlan_group(
2823 const struct net_bridge *br)
2824 {
2825diff --git a/net/bridge/br_vlan.c b/net/bridge/br_vlan.c
2826index 9257292bd..bcfd16924 100644
2827--- a/net/bridge/br_vlan.c
2828+++ b/net/bridge/br_vlan.c
2829@@ -1268,6 +1268,61 @@ int br_vlan_get_pvid_rcu(const struct net_device *dev, u16 *p_pvid)
2830 }
2831 EXPORT_SYMBOL_GPL(br_vlan_get_pvid_rcu);
2832
2833+void br_vlan_fill_forward_path_pvid(struct net_bridge *br,
2834+ struct net_device_path_ctx *ctx,
2835+ struct net_device_path *path)
2836+{
2837+ struct net_bridge_vlan_group *vg;
2838+ int idx = ctx->num_vlans - 1;
2839+ u16 vid;
2840+
2841+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
2842+
2843+ if (!br_opt_get(br, BROPT_VLAN_ENABLED))
2844+ return;
2845+
2846+ vg = br_vlan_group(br);
2847+
2848+ if (idx >= 0 &&
2849+ ctx->vlan[idx].proto == br->vlan_proto) {
2850+ vid = ctx->vlan[idx].id;
2851+ } else {
2852+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_TAG;
2853+ vid = br_get_pvid(vg);
2854+ }
2855+
2856+ path->bridge.vlan_id = vid;
2857+ path->bridge.vlan_proto = br->vlan_proto;
2858+}
2859+
2860+int br_vlan_fill_forward_path_mode(struct net_bridge *br,
2861+ struct net_bridge_port *dst,
2862+ struct net_device_path *path)
2863+{
2864+ struct net_bridge_vlan_group *vg;
2865+ struct net_bridge_vlan *v;
2866+
2867+ if (!br_opt_get(br, BROPT_VLAN_ENABLED))
2868+ return 0;
2869+
2870+ vg = nbp_vlan_group_rcu(dst);
2871+ v = br_vlan_find(vg, path->bridge.vlan_id);
2872+ if (!v || !br_vlan_should_use(v))
2873+ return -EINVAL;
2874+
2875+ if (!(v->flags & BRIDGE_VLAN_INFO_UNTAGGED))
2876+ return 0;
2877+
2878+ if (path->bridge.vlan_mode == DEV_PATH_BR_VLAN_TAG)
2879+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
2880+ else if (v->priv_flags & BR_VLFLAG_ADDED_BY_SWITCHDEV)
2881+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG_HW;
2882+ else
2883+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_UNTAG;
2884+
2885+ return 0;
2886+}
2887+
2888 int br_vlan_get_info(const struct net_device *dev, u16 vid,
2889 struct bridge_vlan_info *p_vinfo)
2890 {
2891diff --git a/net/core/dev.c b/net/core/dev.c
2892index fe2c856b9..4f0edb218 100644
2893--- a/net/core/dev.c
2894+++ b/net/core/dev.c
2895@@ -639,6 +639,52 @@ int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2896 }
2897 EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
2898
2899+static struct net_device_path *dev_fwd_path(struct net_device_path_stack *stack)
2900+{
2901+ int k = stack->num_paths++;
2902+
2903+ if (WARN_ON_ONCE(k >= NET_DEVICE_PATH_STACK_MAX))
2904+ return NULL;
2905+
2906+ return &stack->path[k];
2907+}
2908+
2909+int dev_fill_forward_path(const struct net_device *dev, const u8 *daddr,
2910+ struct net_device_path_stack *stack)
2911+{
2912+ const struct net_device *last_dev;
2913+ struct net_device_path_ctx ctx = {
2914+ .dev = dev,
2915+ };
2916+ struct net_device_path *path;
2917+ int ret = 0;
2918+
2919+ memcpy(ctx.daddr, daddr, sizeof(ctx.daddr));
2920+ stack->num_paths = 0;
2921+ while (ctx.dev && ctx.dev->netdev_ops->ndo_fill_forward_path) {
2922+ last_dev = ctx.dev;
2923+ path = dev_fwd_path(stack);
2924+ if (!path)
2925+ return -1;
2926+
2927+ memset(path, 0, sizeof(struct net_device_path));
2928+ ret = ctx.dev->netdev_ops->ndo_fill_forward_path(&ctx, path);
2929+ if (ret < 0)
2930+ return -1;
2931+
2932+ if (WARN_ON_ONCE(last_dev == ctx.dev))
2933+ return -1;
2934+ }
2935+ path = dev_fwd_path(stack);
2936+ if (!path)
2937+ return -1;
2938+ path->type = DEV_PATH_ETHERNET;
2939+ path->dev = ctx.dev;
2940+
2941+ return ret;
2942+}
2943+EXPORT_SYMBOL_GPL(dev_fill_forward_path);
2944+
2945 /**
2946 * __dev_get_by_name - find a device by its name
2947 * @net: the applicable net namespace
2948diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
2949index ca80f8699..35a1249a9 100644
2950--- a/net/dsa/dsa.c
2951+++ b/net/dsa/dsa.c
2952@@ -329,6 +329,15 @@ int call_dsa_notifiers(unsigned long val, struct net_device *dev,
2953 }
2954 EXPORT_SYMBOL_GPL(call_dsa_notifiers);
2955
2956+struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
2957+{
2958+ if (!netdev || !dsa_slave_dev_check(netdev))
2959+ return ERR_PTR(-ENODEV);
2960+
2961+ return dsa_slave_to_port(netdev);
2962+}
2963+EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
2964+
2965 static int __init dsa_init_module(void)
2966 {
2967 int rc;
2968diff --git a/net/dsa/slave.c b/net/dsa/slave.c
2969index 036fda317..2dfaa1eac 100644
2970--- a/net/dsa/slave.c
2971+++ b/net/dsa/slave.c
2972@@ -22,8 +22,6 @@
2973
2974 #include "dsa_priv.h"
2975
2976-static bool dsa_slave_dev_check(const struct net_device *dev);
2977-
2978 /* slave mii_bus handling ***************************************************/
2979 static int dsa_slave_phy_read(struct mii_bus *bus, int addr, int reg)
2980 {
2981@@ -1033,14 +1031,32 @@ static int dsa_slave_setup_tc_block(struct net_device *dev,
2982 }
2983 }
2984
2985+static int dsa_slave_setup_ft_block(struct dsa_switch *ds, int port,
2986+ void *type_data)
2987+{
2988+ struct dsa_port *cpu_dp = dsa_to_port(ds, port)->cpu_dp;
2989+ struct net_device *master = cpu_dp->master;
2990+
2991+ if (!master->netdev_ops->ndo_setup_tc)
2992+ return -EOPNOTSUPP;
2993+
2994+ return master->netdev_ops->ndo_setup_tc(master, TC_SETUP_FT, type_data);
2995+}
2996+
2997 static int dsa_slave_setup_tc(struct net_device *dev, enum tc_setup_type type,
2998 void *type_data)
2999 {
3000 struct dsa_port *dp = dsa_slave_to_port(dev);
3001 struct dsa_switch *ds = dp->ds;
3002
3003- if (type == TC_SETUP_BLOCK)
3004+ switch (type) {
3005+ case TC_SETUP_BLOCK:
3006 return dsa_slave_setup_tc_block(dev, type_data);
3007+ case TC_SETUP_FT:
3008+ return dsa_slave_setup_ft_block(ds, dp->index, type_data);
3009+ default:
3010+ break;
3011+ }
3012
3013 if (!ds->ops->port_setup_tc)
3014 return -EOPNOTSUPP;
3015@@ -1226,6 +1242,21 @@ static struct devlink_port *dsa_slave_get_devlink_port(struct net_device *dev)
3016 return dp->ds->devlink ? &dp->devlink_port : NULL;
3017 }
3018
3019+static int dsa_slave_fill_forward_path(struct net_device_path_ctx *ctx,
3020+ struct net_device_path *path)
3021+{
3022+ struct dsa_port *dp = dsa_slave_to_port(ctx->dev);
3023+ struct dsa_port *cpu_dp = dp->cpu_dp;
3024+
3025+ path->dev = ctx->dev;
3026+ path->type = DEV_PATH_DSA;
3027+ path->dsa.proto = cpu_dp->tag_ops->proto;
3028+ path->dsa.port = dp->index;
3029+ ctx->dev = cpu_dp->master;
3030+
3031+ return 0;
3032+}
3033+
3034 static const struct net_device_ops dsa_slave_netdev_ops = {
3035 .ndo_open = dsa_slave_open,
3036 .ndo_stop = dsa_slave_close,
3037@@ -1250,6 +1281,7 @@ static const struct net_device_ops dsa_slave_netdev_ops = {
3038 .ndo_vlan_rx_add_vid = dsa_slave_vlan_rx_add_vid,
3039 .ndo_vlan_rx_kill_vid = dsa_slave_vlan_rx_kill_vid,
3040 .ndo_get_devlink_port = dsa_slave_get_devlink_port,
3041+ .ndo_fill_forward_path = dsa_slave_fill_forward_path,
3042 };
3043
3044 static struct device_type dsa_type = {
3045@@ -1497,10 +1529,11 @@ void dsa_slave_destroy(struct net_device *slave_dev)
3046 free_netdev(slave_dev);
3047 }
3048
3049-static bool dsa_slave_dev_check(const struct net_device *dev)
3050+bool dsa_slave_dev_check(const struct net_device *dev)
3051 {
3052 return dev->netdev_ops == &dsa_slave_netdev_ops;
3053 }
3054+EXPORT_SYMBOL_GPL(dsa_slave_dev_check);
3055
3056 static int dsa_slave_changeupper(struct net_device *dev,
3057 struct netdev_notifier_changeupper_info *info)
3058diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
3059index f17b40211..803b92e4c 100644
3060--- a/net/ipv4/netfilter/Kconfig
3061+++ b/net/ipv4/netfilter/Kconfig
3062@@ -56,8 +56,6 @@ config NF_TABLES_ARP
3063 help
3064 This option enables the ARP support for nf_tables.
3065
3066-endif # NF_TABLES
3067-
3068 config NF_FLOW_TABLE_IPV4
3069 tristate "Netfilter flow table IPv4 module"
3070 depends on NF_FLOW_TABLE
3071@@ -66,6 +64,8 @@ config NF_FLOW_TABLE_IPV4
3072
3073 To compile it as a module, choose M here.
3074
3075+endif # NF_TABLES
3076+
3077 config NF_DUP_IPV4
3078 tristate "Netfilter IPv4 packet duplication to alternate destination"
3079 depends on !NF_CONNTRACK || NF_CONNTRACK
3080diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
3081index 5585e3a94..bb76f6061 100644
3082--- a/net/ipv6/ip6_output.c
3083+++ b/net/ipv6/ip6_output.c
3084@@ -607,7 +607,7 @@ int ip6_forward(struct sk_buff *skb)
3085 }
3086 }
3087
3088- mtu = ip6_dst_mtu_forward(dst);
3089+ mtu = ip6_dst_mtu_maybe_forward(dst, true);
3090 if (mtu < IPV6_MIN_MTU)
3091 mtu = IPV6_MIN_MTU;
3092
3093diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig
3094index 69443e9a3..0b481d236 100644
3095--- a/net/ipv6/netfilter/Kconfig
3096+++ b/net/ipv6/netfilter/Kconfig
3097@@ -45,7 +45,6 @@ config NFT_FIB_IPV6
3098 multicast or blackhole.
3099
3100 endif # NF_TABLES_IPV6
3101-endif # NF_TABLES
3102
3103 config NF_FLOW_TABLE_IPV6
3104 tristate "Netfilter flow table IPv6 module"
3105@@ -55,6 +54,8 @@ config NF_FLOW_TABLE_IPV6
3106
3107 To compile it as a module, choose M here.
3108
3109+endif # NF_TABLES
3110+
3111 config NF_DUP_IPV6
3112 tristate "Netfilter IPv6 packet duplication to alternate destination"
3113 depends on !NF_CONNTRACK || NF_CONNTRACK
3114diff --git a/net/ipv6/route.c b/net/ipv6/route.c
3115index 98aaf0b79..2b357ac71 100644
3116--- a/net/ipv6/route.c
3117+++ b/net/ipv6/route.c
3118@@ -83,7 +83,7 @@ enum rt6_nud_state {
3119
3120 static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie);
3121 static unsigned int ip6_default_advmss(const struct dst_entry *dst);
3122-static unsigned int ip6_mtu(const struct dst_entry *dst);
3123+static unsigned int ip6_mtu(const struct dst_entry *dst);
3124 static struct dst_entry *ip6_negative_advice(struct dst_entry *);
3125 static void ip6_dst_destroy(struct dst_entry *);
3126 static void ip6_dst_ifdown(struct dst_entry *,
3127@@ -3125,25 +3125,7 @@ static unsigned int ip6_default_advmss(const struct dst_entry *dst)
3128
3129 static unsigned int ip6_mtu(const struct dst_entry *dst)
3130 {
3131- struct inet6_dev *idev;
3132- unsigned int mtu;
3133-
3134- mtu = dst_metric_raw(dst, RTAX_MTU);
3135- if (mtu)
3136- goto out;
3137-
3138- mtu = IPV6_MIN_MTU;
3139-
3140- rcu_read_lock();
3141- idev = __in6_dev_get(dst->dev);
3142- if (idev)
3143- mtu = idev->cnf.mtu6;
3144- rcu_read_unlock();
3145-
3146-out:
3147- mtu = min_t(unsigned int, mtu, IP6_MAX_MTU);
3148-
3149- return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
3150+ return ip6_dst_mtu_maybe_forward(dst, false);
3151 }
3152
3153 /* MTU selection:
3154diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
3155index b967763f5..c040e713a 100644
3156--- a/net/netfilter/Kconfig
3157+++ b/net/netfilter/Kconfig
3158@@ -690,8 +690,6 @@ config NFT_FIB_NETDEV
3159
3160 endif # NF_TABLES_NETDEV
3161
3162-endif # NF_TABLES
3163-
3164 config NF_FLOW_TABLE_INET
3165 tristate "Netfilter flow table mixed IPv4/IPv6 module"
3166 depends on NF_FLOW_TABLE
3167@@ -700,11 +698,12 @@ config NF_FLOW_TABLE_INET
3168
3169 To compile it as a module, choose M here.
3170
3171+endif # NF_TABLES
3172+
3173 config NF_FLOW_TABLE
3174 tristate "Netfilter flow table module"
3175 depends on NETFILTER_INGRESS
3176 depends on NF_CONNTRACK
3177- depends on NF_TABLES
3178 help
3179 This option adds the flow table core infrastructure.
3180
3181@@ -984,6 +983,15 @@ config NETFILTER_XT_TARGET_NOTRACK
3182 depends on NETFILTER_ADVANCED
3183 select NETFILTER_XT_TARGET_CT
3184
3185+config NETFILTER_XT_TARGET_FLOWOFFLOAD
3186+ tristate '"FLOWOFFLOAD" target support'
3187+ depends on NF_FLOW_TABLE
3188+ depends on NETFILTER_INGRESS
3189+ help
3190+ This option adds a `FLOWOFFLOAD' target, which uses the nf_flow_offload
3191+ module to speed up processing of packets by bypassing the usual
3192+ netfilter chains
3193+
3194 config NETFILTER_XT_TARGET_RATEEST
3195 tristate '"RATEEST" target support'
3196 depends on NETFILTER_ADVANCED
3197diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
3198index 4fc075b61..d93a121bc 100644
3199--- a/net/netfilter/Makefile
3200+++ b/net/netfilter/Makefile
3201@@ -120,7 +120,8 @@ obj-$(CONFIG_NFT_FWD_NETDEV) += nft_fwd_netdev.o
3202
3203 # flow table infrastructure
3204 obj-$(CONFIG_NF_FLOW_TABLE) += nf_flow_table.o
3205-nf_flow_table-objs := nf_flow_table_core.o nf_flow_table_ip.o
3206+nf_flow_table-objs := nf_flow_table_core.o nf_flow_table_ip.o \
3207+ nf_flow_table_offload.o
3208
3209 obj-$(CONFIG_NF_FLOW_TABLE_INET) += nf_flow_table_inet.o
3210
3211@@ -140,6 +141,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CLASSIFY) += xt_CLASSIFY.o
3212 obj-$(CONFIG_NETFILTER_XT_TARGET_CONNSECMARK) += xt_CONNSECMARK.o
3213 obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o
3214 obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o
3215+obj-$(CONFIG_NETFILTER_XT_TARGET_FLOWOFFLOAD) += xt_FLOWOFFLOAD.o
3216 obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o
3217 obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o
3218 obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o
3219diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c
3220index f6ab6f484..f689e19d8 100644
3221--- a/net/netfilter/nf_conntrack_core.c
3222+++ b/net/netfilter/nf_conntrack_core.c
3223@@ -864,9 +864,8 @@ out:
3224 }
3225 EXPORT_SYMBOL_GPL(nf_conntrack_hash_check_insert);
3226
3227-static inline void nf_ct_acct_update(struct nf_conn *ct,
3228- enum ip_conntrack_info ctinfo,
3229- unsigned int len)
3230+void nf_ct_acct_add(struct nf_conn *ct, u32 dir, unsigned int packets,
3231+ unsigned int bytes)
3232 {
3233 struct nf_conn_acct *acct;
3234
3235@@ -874,10 +873,11 @@ static inline void nf_ct_acct_update(struct nf_conn *ct,
3236 if (acct) {
3237 struct nf_conn_counter *counter = acct->counter;
3238
3239- atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
3240- atomic64_add(len, &counter[CTINFO2DIR(ctinfo)].bytes);
3241+ atomic64_add(packets, &counter[dir].packets);
3242+ atomic64_add(bytes, &counter[dir].bytes);
3243 }
3244 }
3245+EXPORT_SYMBOL_GPL(nf_ct_acct_add);
3246
3247 static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
3248 const struct nf_conn *loser_ct)
3249@@ -891,7 +891,7 @@ static void nf_ct_acct_merge(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
3250
3251 /* u32 should be fine since we must have seen one packet. */
3252 bytes = atomic64_read(&counter[CTINFO2DIR(ctinfo)].bytes);
3253- nf_ct_acct_update(ct, ctinfo, bytes);
3254+ nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), bytes);
3255 }
3256 }
3257
3258@@ -1238,8 +1238,10 @@ static void gc_worker(struct work_struct *work)
3259
3260 tmp = nf_ct_tuplehash_to_ctrack(h);
3261
3262- if (test_bit(IPS_OFFLOAD_BIT, &tmp->status))
3263+ if (test_bit(IPS_OFFLOAD_BIT, &tmp->status)) {
3264+ nf_ct_offload_timeout(tmp);
3265 continue;
3266+ }
3267
3268 if (nf_ct_is_expired(tmp)) {
3269 nf_ct_gc_expired(tmp);
3270@@ -1763,7 +1765,7 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,
3271 WRITE_ONCE(ct->timeout, extra_jiffies);
3272 acct:
3273 if (do_acct)
3274- nf_ct_acct_update(ct, ctinfo, skb->len);
3275+ nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len);
3276 }
3277 EXPORT_SYMBOL_GPL(__nf_ct_refresh_acct);
3278
3279@@ -1771,7 +1773,7 @@ bool nf_ct_kill_acct(struct nf_conn *ct,
3280 enum ip_conntrack_info ctinfo,
3281 const struct sk_buff *skb)
3282 {
3283- nf_ct_acct_update(ct, ctinfo, skb->len);
3284+ nf_ct_acct_update(ct, CTINFO2DIR(ctinfo), skb->len);
3285
3286 return nf_ct_delete(ct, 0, 0);
3287 }
3288diff --git a/net/netfilter/nf_conntrack_proto_tcp.c b/net/netfilter/nf_conntrack_proto_tcp.c
3289index 7204f0366..3742bae21 100644
3290--- a/net/netfilter/nf_conntrack_proto_tcp.c
3291+++ b/net/netfilter/nf_conntrack_proto_tcp.c
3292@@ -1453,6 +1453,10 @@ void nf_conntrack_tcp_init_net(struct net *net)
3293 tn->tcp_loose = nf_ct_tcp_loose;
3294 tn->tcp_be_liberal = nf_ct_tcp_be_liberal;
3295 tn->tcp_max_retrans = nf_ct_tcp_max_retrans;
3296+
3297+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3298+ tn->offload_timeout = 30 * HZ;
3299+#endif
3300 }
3301
3302 const struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp =
3303diff --git a/net/netfilter/nf_conntrack_proto_udp.c b/net/netfilter/nf_conntrack_proto_udp.c
3304index e3a2d018f..a1579d6c3 100644
3305--- a/net/netfilter/nf_conntrack_proto_udp.c
3306+++ b/net/netfilter/nf_conntrack_proto_udp.c
3307@@ -267,6 +267,10 @@ void nf_conntrack_udp_init_net(struct net *net)
3308
3309 for (i = 0; i < UDP_CT_MAX; i++)
3310 un->timeouts[i] = udp_timeouts[i];
3311+
3312+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3313+ un->offload_timeout = 30 * HZ;
3314+#endif
3315 }
3316
3317 const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
3318diff --git a/net/netfilter/nf_conntrack_standalone.c b/net/netfilter/nf_conntrack_standalone.c
3319index 9c6259c28..10d9f93ce 100644
3320--- a/net/netfilter/nf_conntrack_standalone.c
3321+++ b/net/netfilter/nf_conntrack_standalone.c
3322@@ -353,7 +353,9 @@ static int ct_seq_show(struct seq_file *s, void *v)
3323 if (seq_print_acct(s, ct, IP_CT_DIR_REPLY))
3324 goto release;
3325
3326- if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
3327+ if (test_bit(IPS_HW_OFFLOAD_BIT, &ct->status))
3328+ seq_puts(s, "[HW_OFFLOAD] ");
3329+ else if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
3330 seq_puts(s, "[OFFLOAD] ");
3331 else if (test_bit(IPS_ASSURED_BIT, &ct->status))
3332 seq_puts(s, "[ASSURED] ");
3333@@ -620,11 +622,17 @@ enum nf_ct_sysctl_index {
3334 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_CLOSE,
3335 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_RETRANS,
3336 NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_UNACK,
3337+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3338+ NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD,
3339+#endif
3340 NF_SYSCTL_CT_PROTO_TCP_LOOSE,
3341 NF_SYSCTL_CT_PROTO_TCP_LIBERAL,
3342 NF_SYSCTL_CT_PROTO_TCP_MAX_RETRANS,
3343 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP,
3344 NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM,
3345+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3346+ NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD,
3347+#endif
3348 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP,
3349 NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6,
3350 #ifdef CONFIG_NF_CT_PROTO_SCTP
3351@@ -812,6 +820,14 @@ static struct ctl_table nf_ct_sysctl_table[] = {
3352 .mode = 0644,
3353 .proc_handler = proc_dointvec_jiffies,
3354 },
3355+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3356+ [NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD] = {
3357+ .procname = "nf_flowtable_tcp_timeout",
3358+ .maxlen = sizeof(unsigned int),
3359+ .mode = 0644,
3360+ .proc_handler = proc_dointvec_jiffies,
3361+ },
3362+#endif
3363 [NF_SYSCTL_CT_PROTO_TCP_LOOSE] = {
3364 .procname = "nf_conntrack_tcp_loose",
3365 .maxlen = sizeof(int),
3366@@ -846,6 +862,14 @@ static struct ctl_table nf_ct_sysctl_table[] = {
3367 .mode = 0644,
3368 .proc_handler = proc_dointvec_jiffies,
3369 },
3370+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3371+ [NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD] = {
3372+ .procname = "nf_flowtable_udp_timeout",
3373+ .maxlen = sizeof(unsigned int),
3374+ .mode = 0644,
3375+ .proc_handler = proc_dointvec_jiffies,
3376+ },
3377+#endif
3378 [NF_SYSCTL_CT_PROTO_TIMEOUT_ICMP] = {
3379 .procname = "nf_conntrack_icmp_timeout",
3380 .maxlen = sizeof(unsigned int),
3381@@ -1028,6 +1052,11 @@ static void nf_conntrack_standalone_init_tcp_sysctl(struct net *net,
3382 XASSIGN(LIBERAL, &tn->tcp_be_liberal);
3383 XASSIGN(MAX_RETRANS, &tn->tcp_max_retrans);
3384 #undef XASSIGN
3385+
3386+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3387+ table[NF_SYSCTL_CT_PROTO_TIMEOUT_TCP_OFFLOAD].data = &tn->offload_timeout;
3388+#endif
3389+
3390 }
3391
3392 static void nf_conntrack_standalone_init_sctp_sysctl(struct net *net,
3393@@ -1115,6 +1144,9 @@ static int nf_conntrack_standalone_init_sysctl(struct net *net)
3394 table[NF_SYSCTL_CT_PROTO_TIMEOUT_ICMPV6].data = &nf_icmpv6_pernet(net)->timeout;
3395 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP].data = &un->timeouts[UDP_CT_UNREPLIED];
3396 table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_STREAM].data = &un->timeouts[UDP_CT_REPLIED];
3397+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE)
3398+ table[NF_SYSCTL_CT_PROTO_TIMEOUT_UDP_OFFLOAD].data = &un->offload_timeout;
3399+#endif
3400
3401 nf_conntrack_standalone_init_tcp_sysctl(net, table);
3402 nf_conntrack_standalone_init_sctp_sysctl(net, table);
3403diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
developerb7c46752022-07-04 19:51:38 +08003404index f212cec0..10365581 100644
developer8cb3ac72022-07-04 10:55:14 +08003405--- a/net/netfilter/nf_flow_table_core.c
3406+++ b/net/netfilter/nf_flow_table_core.c
developerb7c46752022-07-04 19:51:38 +08003407@@ -7,43 +7,21 @@
developer8cb3ac72022-07-04 10:55:14 +08003408 #include <linux/netdevice.h>
3409 #include <net/ip.h>
3410 #include <net/ip6_route.h>
3411-#include <net/netfilter/nf_tables.h>
3412 #include <net/netfilter/nf_flow_table.h>
3413 #include <net/netfilter/nf_conntrack.h>
3414 #include <net/netfilter/nf_conntrack_core.h>
3415 #include <net/netfilter/nf_conntrack_l4proto.h>
3416 #include <net/netfilter/nf_conntrack_tuple.h>
3417
3418-struct flow_offload_entry {
3419- struct flow_offload flow;
3420- struct nf_conn *ct;
3421- struct rcu_head rcu_head;
3422-};
3423-
3424 static DEFINE_MUTEX(flowtable_lock);
3425 static LIST_HEAD(flowtables);
3426
developerb7c46752022-07-04 19:51:38 +08003427-static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
3428-{
3429- const struct rt6_info *rt;
3430-
3431- if (flow_tuple->l3proto == NFPROTO_IPV6) {
3432- rt = (const struct rt6_info *)flow_tuple->dst_cache;
3433- return rt6_get_cookie(rt);
3434- }
3435-
3436- return 0;
3437-}
3438-
developer8cb3ac72022-07-04 10:55:14 +08003439 static void
3440-flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
3441- struct nf_flow_route *route,
3442+flow_offload_fill_dir(struct flow_offload *flow,
3443 enum flow_offload_tuple_dir dir)
3444 {
3445 struct flow_offload_tuple *ft = &flow->tuplehash[dir].tuple;
3446- struct nf_conntrack_tuple *ctt = &ct->tuplehash[dir].tuple;
3447- struct dst_entry *other_dst = route->tuple[!dir].dst;
3448- struct dst_entry *dst = route->tuple[dir].dst;
3449+ struct nf_conntrack_tuple *ctt = &flow->ct->tuplehash[dir].tuple;
3450
3451 ft->dir = dir;
3452
developerb7c46752022-07-04 19:51:38 +08003453@@ -51,12 +29,10 @@ flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
developer8cb3ac72022-07-04 10:55:14 +08003454 case NFPROTO_IPV4:
3455 ft->src_v4 = ctt->src.u3.in;
3456 ft->dst_v4 = ctt->dst.u3.in;
3457- ft->mtu = ip_dst_mtu_maybe_forward(dst, true);
3458 break;
3459 case NFPROTO_IPV6:
3460 ft->src_v6 = ctt->src.u3.in6;
3461 ft->dst_v6 = ctt->dst.u3.in6;
3462- ft->mtu = ip6_dst_mtu_forward(dst);
3463 break;
3464 }
3465
developerb7c46752022-07-04 19:51:38 +08003466@@ -64,50 +40,32 @@ flow_offload_fill_dir(struct flow_offload *flow, struct nf_conn *ct,
developer8cb3ac72022-07-04 10:55:14 +08003467 ft->l4proto = ctt->dst.protonum;
3468 ft->src_port = ctt->src.u.tcp.port;
3469 ft->dst_port = ctt->dst.u.tcp.port;
3470-
3471- ft->iifidx = other_dst->dev->ifindex;
3472- ft->dst_cache = dst;
developerb7c46752022-07-04 19:51:38 +08003473- ft->dst_cookie = flow_offload_dst_cookie(ft);
developer8cb3ac72022-07-04 10:55:14 +08003474 }
3475
3476-struct flow_offload *
3477-flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
3478+struct flow_offload *flow_offload_alloc(struct nf_conn *ct)
3479 {
3480- struct flow_offload_entry *entry;
3481 struct flow_offload *flow;
3482
3483 if (unlikely(nf_ct_is_dying(ct) ||
3484 !atomic_inc_not_zero(&ct->ct_general.use)))
3485 return NULL;
3486
3487- entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3488- if (!entry)
3489+ flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
3490+ if (!flow)
3491 goto err_ct_refcnt;
3492
3493- flow = &entry->flow;
developerb7c46752022-07-04 19:51:38 +08003494-
developer8cb3ac72022-07-04 10:55:14 +08003495- if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst))
3496- goto err_dst_cache_original;
3497-
3498- if (!dst_hold_safe(route->tuple[FLOW_OFFLOAD_DIR_REPLY].dst))
3499- goto err_dst_cache_reply;
developerb7c46752022-07-04 19:51:38 +08003500+ flow->ct = ct;
3501
developer8cb3ac72022-07-04 10:55:14 +08003502- entry->ct = ct;
3503-
3504- flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_ORIGINAL);
3505- flow_offload_fill_dir(flow, ct, route, FLOW_OFFLOAD_DIR_REPLY);
3506+ flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
3507+ flow_offload_fill_dir(flow, FLOW_OFFLOAD_DIR_REPLY);
3508
3509 if (ct->status & IPS_SRC_NAT)
3510- flow->flags |= FLOW_OFFLOAD_SNAT;
3511+ __set_bit(NF_FLOW_SNAT, &flow->flags);
3512 if (ct->status & IPS_DST_NAT)
3513- flow->flags |= FLOW_OFFLOAD_DNAT;
3514+ __set_bit(NF_FLOW_DNAT, &flow->flags);
3515
3516 return flow;
3517
3518-err_dst_cache_reply:
3519- dst_release(route->tuple[FLOW_OFFLOAD_DIR_ORIGINAL].dst);
3520-err_dst_cache_original:
3521- kfree(entry);
3522 err_ct_refcnt:
3523 nf_ct_put(ct);
3524
developerb7c46752022-07-04 19:51:38 +08003525@@ -115,40 +73,135 @@ flow_offload_alloc(struct nf_conn *ct, struct nf_flow_route *route)
developer8cb3ac72022-07-04 10:55:14 +08003526 }
3527 EXPORT_SYMBOL_GPL(flow_offload_alloc);
3528
3529-static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
3530+static u32 flow_offload_dst_cookie(struct flow_offload_tuple *flow_tuple)
3531 {
3532- tcp->state = TCP_CONNTRACK_ESTABLISHED;
3533- tcp->seen[0].td_maxwin = 0;
3534- tcp->seen[1].td_maxwin = 0;
3535+ const struct rt6_info *rt;
3536+
3537+ if (flow_tuple->l3proto == NFPROTO_IPV6) {
3538+ rt = (const struct rt6_info *)flow_tuple->dst_cache;
3539+ return rt6_get_cookie(rt);
3540+ }
3541+
3542+ return 0;
3543 }
3544
3545-#define NF_FLOWTABLE_TCP_PICKUP_TIMEOUT (120 * HZ)
3546-#define NF_FLOWTABLE_UDP_PICKUP_TIMEOUT (30 * HZ)
3547+static int flow_offload_fill_route(struct flow_offload *flow,
3548+ const struct nf_flow_route *route,
3549+ enum flow_offload_tuple_dir dir)
3550+{
3551+ struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
3552+ struct dst_entry *dst = route->tuple[dir].dst;
3553+ int i, j = 0;
3554+
3555+ switch (flow_tuple->l3proto) {
3556+ case NFPROTO_IPV4:
3557+ flow_tuple->mtu = ip_dst_mtu_maybe_forward(dst, true);
3558+ break;
3559+ case NFPROTO_IPV6:
3560+ flow_tuple->mtu = ip6_dst_mtu_maybe_forward(dst, true);
3561+ break;
3562+ }
3563+
3564+ flow_tuple->iifidx = route->tuple[dir].in.ifindex;
3565+ for (i = route->tuple[dir].in.num_encaps - 1; i >= 0; i--) {
3566+ flow_tuple->encap[j].id = route->tuple[dir].in.encap[i].id;
3567+ flow_tuple->encap[j].proto = route->tuple[dir].in.encap[i].proto;
3568+ if (route->tuple[dir].in.ingress_vlans & BIT(i))
3569+ flow_tuple->in_vlan_ingress |= BIT(j);
3570+ j++;
3571+ }
3572+ flow_tuple->encap_num = route->tuple[dir].in.num_encaps;
3573+
3574+ switch (route->tuple[dir].xmit_type) {
3575+ case FLOW_OFFLOAD_XMIT_DIRECT:
3576+ memcpy(flow_tuple->out.h_dest, route->tuple[dir].out.h_dest,
3577+ ETH_ALEN);
3578+ memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
3579+ ETH_ALEN);
3580+ flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
3581+ flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
3582+ break;
3583+ case FLOW_OFFLOAD_XMIT_XFRM:
3584+ case FLOW_OFFLOAD_XMIT_NEIGH:
3585+ if (!dst_hold_safe(route->tuple[dir].dst))
3586+ return -1;
3587+
3588+ flow_tuple->dst_cache = dst;
3589+ flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
3590+ break;
3591+ default:
3592+ WARN_ON_ONCE(1);
3593+ break;
3594+ }
3595+ flow_tuple->xmit_type = route->tuple[dir].xmit_type;
developerb7c46752022-07-04 19:51:38 +08003596+
developer8cb3ac72022-07-04 10:55:14 +08003597+ return 0;
3598+}
3599+
3600+static void nft_flow_dst_release(struct flow_offload *flow,
3601+ enum flow_offload_tuple_dir dir)
3602+{
3603+ if (flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
3604+ flow->tuplehash[dir].tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)
3605+ dst_release(flow->tuplehash[dir].tuple.dst_cache);
3606+}
3607+
3608+int flow_offload_route_init(struct flow_offload *flow,
3609+ const struct nf_flow_route *route)
3610+{
3611+ int err;
3612+
3613+ err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
3614+ if (err < 0)
3615+ return err;
3616+
3617+ err = flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
3618+ if (err < 0)
3619+ goto err_route_reply;
3620+
3621+ flow->type = NF_FLOW_OFFLOAD_ROUTE;
3622+
3623+ return 0;
3624+
3625+err_route_reply:
3626+ nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
3627+
3628+ return err;
3629+}
3630+EXPORT_SYMBOL_GPL(flow_offload_route_init);
developerb7c46752022-07-04 19:51:38 +08003631
3632-static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
developer8cb3ac72022-07-04 10:55:14 +08003633+static void flow_offload_fixup_tcp(struct ip_ct_tcp *tcp)
3634 {
3635- return (__s32)(timeout - (u32)jiffies);
3636+ tcp->state = TCP_CONNTRACK_ESTABLISHED;
3637+ tcp->seen[0].td_maxwin = 0;
3638+ tcp->seen[1].td_maxwin = 0;
3639 }
3640
3641 static void flow_offload_fixup_ct_timeout(struct nf_conn *ct)
3642 {
3643- const struct nf_conntrack_l4proto *l4proto;
3644+ struct net *net = nf_ct_net(ct);
3645 int l4num = nf_ct_protonum(ct);
3646- unsigned int timeout;
3647+ s32 timeout;
3648
3649- l4proto = nf_ct_l4proto_find(l4num);
3650- if (!l4proto)
3651- return;
3652+ if (l4num == IPPROTO_TCP) {
3653+ struct nf_tcp_net *tn = nf_tcp_pernet(net);
3654
3655- if (l4num == IPPROTO_TCP)
3656- timeout = NF_FLOWTABLE_TCP_PICKUP_TIMEOUT;
3657- else if (l4num == IPPROTO_UDP)
3658- timeout = NF_FLOWTABLE_UDP_PICKUP_TIMEOUT;
3659- else
3660+ timeout = tn->timeouts[TCP_CONNTRACK_ESTABLISHED];
3661+ timeout -= tn->offload_timeout;
3662+ } else if (l4num == IPPROTO_UDP) {
3663+ struct nf_udp_net *tn = nf_udp_pernet(net);
3664+
3665+ timeout = tn->timeouts[UDP_CT_REPLIED];
3666+ timeout -= tn->offload_timeout;
3667+ } else {
3668 return;
3669+ }
3670+
3671+ if (timeout < 0)
3672+ timeout = 0;
3673
3674- if (nf_flow_timeout_delta(ct->timeout) > (__s32)timeout)
3675- ct->timeout = nfct_time_stamp + timeout;
3676+ if (nf_flow_timeout_delta(READ_ONCE(ct->timeout)) > (__s32)timeout)
3677+ WRITE_ONCE(ct->timeout, nfct_time_stamp + timeout);
3678 }
3679
3680 static void flow_offload_fixup_ct_state(struct nf_conn *ct)
developerb7c46752022-07-04 19:51:38 +08003681@@ -163,17 +216,23 @@ static void flow_offload_fixup_ct(struct nf_conn *ct)
developer8cb3ac72022-07-04 10:55:14 +08003682 flow_offload_fixup_ct_timeout(ct);
3683 }
3684
3685-void flow_offload_free(struct flow_offload *flow)
3686+static void flow_offload_route_release(struct flow_offload *flow)
3687 {
3688- struct flow_offload_entry *e;
3689+ nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_ORIGINAL);
3690+ nft_flow_dst_release(flow, FLOW_OFFLOAD_DIR_REPLY);
3691+}
3692
3693- dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_cache);
3694- dst_release(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_cache);
3695- e = container_of(flow, struct flow_offload_entry, flow);
3696- if (flow->flags & FLOW_OFFLOAD_DYING)
3697- nf_ct_delete(e->ct, 0, 0);
3698- nf_ct_put(e->ct);
3699- kfree_rcu(e, rcu_head);
3700+void flow_offload_free(struct flow_offload *flow)
3701+{
3702+ switch (flow->type) {
3703+ case NF_FLOW_OFFLOAD_ROUTE:
3704+ flow_offload_route_release(flow);
3705+ break;
3706+ default:
3707+ break;
3708+ }
3709+ nf_ct_put(flow->ct);
3710+ kfree_rcu(flow, rcu_head);
3711 }
3712 EXPORT_SYMBOL_GPL(flow_offload_free);
3713
developerb7c46752022-07-04 19:51:38 +08003714@@ -181,14 +240,14 @@ static u32 flow_offload_hash(const void *data, u32 len, u32 seed)
developer8cb3ac72022-07-04 10:55:14 +08003715 {
3716 const struct flow_offload_tuple *tuple = data;
3717
3718- return jhash(tuple, offsetof(struct flow_offload_tuple, dir), seed);
3719+ return jhash(tuple, offsetof(struct flow_offload_tuple, __hash), seed);
3720 }
3721
3722 static u32 flow_offload_hash_obj(const void *data, u32 len, u32 seed)
3723 {
3724 const struct flow_offload_tuple_rhash *tuplehash = data;
3725
3726- return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, dir), seed);
3727+ return jhash(&tuplehash->tuple, offsetof(struct flow_offload_tuple, __hash), seed);
3728 }
3729
3730 static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
developerb7c46752022-07-04 19:51:38 +08003731@@ -197,7 +256,7 @@ static int flow_offload_hash_cmp(struct rhashtable_compare_arg *arg,
developer8cb3ac72022-07-04 10:55:14 +08003732 const struct flow_offload_tuple *tuple = arg->key;
3733 const struct flow_offload_tuple_rhash *x = ptr;
3734
3735- if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, dir)))
3736+ if (memcmp(&x->tuple, tuple, offsetof(struct flow_offload_tuple, __hash)))
3737 return 1;
3738
3739 return 0;
developerb7c46752022-07-04 19:51:38 +08003740@@ -211,30 +270,30 @@ static const struct rhashtable_params nf_flow_offload_rhash_params = {
developer8cb3ac72022-07-04 10:55:14 +08003741 .automatic_shrinking = true,
3742 };
3743
3744-#define DAY (86400 * HZ)
3745-
3746-/* Set an arbitrary timeout large enough not to ever expire, this save
3747- * us a check for the IPS_OFFLOAD_BIT from the packet path via
3748- * nf_ct_is_expired().
3749- */
3750-static void nf_ct_offload_timeout(struct flow_offload *flow)
3751+unsigned long flow_offload_get_timeout(struct flow_offload *flow)
3752 {
3753- struct flow_offload_entry *entry;
3754- struct nf_conn *ct;
3755+ unsigned long timeout = NF_FLOW_TIMEOUT;
3756+ struct net *net = nf_ct_net(flow->ct);
3757+ int l4num = nf_ct_protonum(flow->ct);
developer8cb3ac72022-07-04 10:55:14 +08003758
3759- entry = container_of(flow, struct flow_offload_entry, flow);
3760- ct = entry->ct;
developerb7c46752022-07-04 19:51:38 +08003761+ if (l4num == IPPROTO_TCP) {
3762+ struct nf_tcp_net *tn = nf_tcp_pernet(net);
3763
3764- if (nf_ct_expires(ct) < DAY / 2)
3765- ct->timeout = nfct_time_stamp + DAY;
developer8cb3ac72022-07-04 10:55:14 +08003766+ timeout = tn->offload_timeout;
3767+ } else if (l4num == IPPROTO_UDP) {
3768+ struct nf_udp_net *tn = nf_udp_pernet(net);
3769+
3770+ timeout = tn->offload_timeout;
3771+ }
developerb7c46752022-07-04 19:51:38 +08003772+
developer8cb3ac72022-07-04 10:55:14 +08003773+ return timeout;
3774 }
3775
3776 int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
3777 {
3778 int err;
3779
3780- nf_ct_offload_timeout(flow);
3781- flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
3782+ flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
3783
3784 err = rhashtable_insert_fast(&flow_table->rhashtable,
3785 &flow->tuplehash[0].node,
developerb7c46752022-07-04 19:51:38 +08003786@@ -252,10 +311,35 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
developer8cb3ac72022-07-04 10:55:14 +08003787 return err;
3788 }
3789
3790+ nf_ct_offload_timeout(flow->ct);
3791+
3792+ if (nf_flowtable_hw_offload(flow_table)) {
3793+ __set_bit(NF_FLOW_HW, &flow->flags);
3794+ nf_flow_offload_add(flow_table, flow);
3795+ }
3796+
3797 return 0;
3798 }
3799 EXPORT_SYMBOL_GPL(flow_offload_add);
3800
3801+void flow_offload_refresh(struct nf_flowtable *flow_table,
3802+ struct flow_offload *flow)
3803+{
3804+ u32 timeout;
3805+
3806+ timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
3807+ if (timeout - READ_ONCE(flow->timeout) > HZ)
3808+ WRITE_ONCE(flow->timeout, timeout);
3809+ else
3810+ return;
3811+
3812+ if (likely(!nf_flowtable_hw_offload(flow_table)))
3813+ return;
3814+
3815+ nf_flow_offload_add(flow_table, flow);
3816+}
3817+EXPORT_SYMBOL_GPL(flow_offload_refresh);
3818+
3819 static inline bool nf_flow_has_expired(const struct flow_offload *flow)
3820 {
3821 return nf_flow_timeout_delta(flow->timeout) <= 0;
developerb7c46752022-07-04 19:51:38 +08003822@@ -264,8 +348,6 @@ static inline bool nf_flow_has_expired(const struct flow_offload *flow)
developer8cb3ac72022-07-04 10:55:14 +08003823 static void flow_offload_del(struct nf_flowtable *flow_table,
3824 struct flow_offload *flow)
3825 {
3826- struct flow_offload_entry *e;
3827-
3828 rhashtable_remove_fast(&flow_table->rhashtable,
3829 &flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].node,
3830 nf_flow_offload_rhash_params);
developerb7c46752022-07-04 19:51:38 +08003831@@ -273,28 +355,21 @@ static void flow_offload_del(struct nf_flowtable *flow_table,
developer8cb3ac72022-07-04 10:55:14 +08003832 &flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].node,
3833 nf_flow_offload_rhash_params);
3834
3835- e = container_of(flow, struct flow_offload_entry, flow);
3836- clear_bit(IPS_OFFLOAD_BIT, &e->ct->status);
3837+ clear_bit(IPS_OFFLOAD_BIT, &flow->ct->status);
3838
3839 if (nf_flow_has_expired(flow))
3840- flow_offload_fixup_ct(e->ct);
3841- else if (flow->flags & FLOW_OFFLOAD_TEARDOWN)
3842- flow_offload_fixup_ct_timeout(e->ct);
3843-
3844- if (!(flow->flags & FLOW_OFFLOAD_TEARDOWN))
3845- flow_offload_fixup_ct_state(e->ct);
3846+ flow_offload_fixup_ct(flow->ct);
3847+ else
3848+ flow_offload_fixup_ct_timeout(flow->ct);
3849
3850 flow_offload_free(flow);
3851 }
3852
3853 void flow_offload_teardown(struct flow_offload *flow)
3854 {
3855- struct flow_offload_entry *e;
developerb7c46752022-07-04 19:51:38 +08003856-
3857- flow->flags |= FLOW_OFFLOAD_TEARDOWN;
developer8cb3ac72022-07-04 10:55:14 +08003858+ set_bit(NF_FLOW_TEARDOWN, &flow->flags);
3859
developer8cb3ac72022-07-04 10:55:14 +08003860- e = container_of(flow, struct flow_offload_entry, flow);
3861- flow_offload_fixup_ct_state(e->ct);
3862+ flow_offload_fixup_ct_state(flow->ct);
3863 }
3864 EXPORT_SYMBOL_GPL(flow_offload_teardown);
3865
developerb7c46752022-07-04 19:51:38 +08003866@@ -304,7 +379,6 @@ flow_offload_lookup(struct nf_flowtable *flow_table,
developer8cb3ac72022-07-04 10:55:14 +08003867 {
3868 struct flow_offload_tuple_rhash *tuplehash;
3869 struct flow_offload *flow;
3870- struct flow_offload_entry *e;
3871 int dir;
3872
3873 tuplehash = rhashtable_lookup(&flow_table->rhashtable, tuple,
developerb7c46752022-07-04 19:51:38 +08003874@@ -314,19 +388,17 @@ flow_offload_lookup(struct nf_flowtable *flow_table,
developer8cb3ac72022-07-04 10:55:14 +08003875
3876 dir = tuplehash->tuple.dir;
3877 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
3878- if (flow->flags & (FLOW_OFFLOAD_DYING | FLOW_OFFLOAD_TEARDOWN))
3879+ if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
3880 return NULL;
3881
3882- e = container_of(flow, struct flow_offload_entry, flow);
3883- if (unlikely(nf_ct_is_dying(e->ct)))
3884+ if (unlikely(nf_ct_is_dying(flow->ct)))
3885 return NULL;
3886
3887 return tuplehash;
3888 }
3889 EXPORT_SYMBOL_GPL(flow_offload_lookup);
3890
3891-static int
3892-nf_flow_table_iterate(struct nf_flowtable *flow_table,
3893+int nf_flow_table_iterate(struct nf_flowtable *flow_table,
3894 void (*iter)(struct flow_offload *flow, void *data),
3895 void *data)
3896 {
developerb7c46752022-07-04 19:51:38 +08003897@@ -339,7 +411,6 @@ nf_flow_table_iterate(struct nf_flowtable *flow_table,
developer8cb3ac72022-07-04 10:55:14 +08003898 rhashtable_walk_start(&hti);
3899
3900 while ((tuplehash = rhashtable_walk_next(&hti))) {
3901-
3902 if (IS_ERR(tuplehash)) {
3903 if (PTR_ERR(tuplehash) != -EAGAIN) {
3904 err = PTR_ERR(tuplehash);
developerb7c46752022-07-04 19:51:38 +08003905@@ -359,23 +430,49 @@ nf_flow_table_iterate(struct nf_flowtable *flow_table,
developer8cb3ac72022-07-04 10:55:14 +08003906
3907 return err;
3908 }
3909+EXPORT_SYMBOL_GPL(nf_flow_table_iterate);
3910
3911-static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
3912+static bool flow_offload_stale_dst(struct flow_offload_tuple *tuple)
3913 {
3914- struct nf_flowtable *flow_table = data;
3915- struct flow_offload_entry *e;
3916- bool teardown;
3917+ struct dst_entry *dst;
3918
3919- e = container_of(flow, struct flow_offload_entry, flow);
3920+ if (tuple->xmit_type == FLOW_OFFLOAD_XMIT_NEIGH ||
3921+ tuple->xmit_type == FLOW_OFFLOAD_XMIT_XFRM) {
3922+ dst = tuple->dst_cache;
3923+ if (!dst_check(dst, tuple->dst_cookie))
3924+ return true;
3925+ }
3926
3927- teardown = flow->flags & (FLOW_OFFLOAD_DYING |
3928- FLOW_OFFLOAD_TEARDOWN);
3929+ return false;
3930+}
3931
3932- if (!teardown)
3933- nf_ct_offload_timeout(flow);
3934+static bool nf_flow_has_stale_dst(struct flow_offload *flow)
3935+{
3936+ return flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
3937+ flow_offload_stale_dst(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple);
3938+}
3939
3940- if (nf_flow_has_expired(flow) || teardown)
3941- flow_offload_del(flow_table, flow);
3942+static void nf_flow_offload_gc_step(struct flow_offload *flow, void *data)
3943+{
3944+ struct nf_flowtable *flow_table = data;
3945+
3946+ if (nf_flow_has_expired(flow) ||
3947+ nf_ct_is_dying(flow->ct) ||
3948+ nf_flow_has_stale_dst(flow))
3949+ set_bit(NF_FLOW_TEARDOWN, &flow->flags);
3950+
3951+ if (test_bit(NF_FLOW_TEARDOWN, &flow->flags)) {
3952+ if (test_bit(NF_FLOW_HW, &flow->flags)) {
3953+ if (!test_bit(NF_FLOW_HW_DYING, &flow->flags))
3954+ nf_flow_offload_del(flow_table, flow);
3955+ else if (test_bit(NF_FLOW_HW_DEAD, &flow->flags))
3956+ flow_offload_del(flow_table, flow);
3957+ } else {
3958+ flow_offload_del(flow_table, flow);
3959+ }
3960+ } else if (test_bit(NF_FLOW_HW, &flow->flags)) {
3961+ nf_flow_offload_stats(flow_table, flow);
3962+ }
3963 }
3964
3965 static void nf_flow_offload_work_gc(struct work_struct *work)
developerb7c46752022-07-04 19:51:38 +08003966@@ -387,30 +484,20 @@ static void nf_flow_offload_work_gc(struct work_struct *work)
developer8cb3ac72022-07-04 10:55:14 +08003967 queue_delayed_work(system_power_efficient_wq, &flow_table->gc_work, HZ);
3968 }
3969
3970-static int nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
3971- __be16 port, __be16 new_port)
3972+static void nf_flow_nat_port_tcp(struct sk_buff *skb, unsigned int thoff,
3973+ __be16 port, __be16 new_port)
3974 {
3975 struct tcphdr *tcph;
3976
3977- if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
3978- skb_try_make_writable(skb, thoff + sizeof(*tcph)))
3979- return -1;
3980-
3981 tcph = (void *)(skb_network_header(skb) + thoff);
3982 inet_proto_csum_replace2(&tcph->check, skb, port, new_port, false);
3983-
3984- return 0;
3985 }
3986
3987-static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
3988- __be16 port, __be16 new_port)
3989+static void nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
3990+ __be16 port, __be16 new_port)
3991 {
3992 struct udphdr *udph;
3993
3994- if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
3995- skb_try_make_writable(skb, thoff + sizeof(*udph)))
3996- return -1;
3997-
3998 udph = (void *)(skb_network_header(skb) + thoff);
3999 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
4000 inet_proto_csum_replace2(&udph->check, skb, port,
developerb7c46752022-07-04 19:51:38 +08004001@@ -418,38 +505,28 @@ static int nf_flow_nat_port_udp(struct sk_buff *skb, unsigned int thoff,
developer8cb3ac72022-07-04 10:55:14 +08004002 if (!udph->check)
4003 udph->check = CSUM_MANGLED_0;
4004 }
4005-
4006- return 0;
4007 }
4008
4009-static int nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
4010- u8 protocol, __be16 port, __be16 new_port)
4011+static void nf_flow_nat_port(struct sk_buff *skb, unsigned int thoff,
4012+ u8 protocol, __be16 port, __be16 new_port)
4013 {
4014 switch (protocol) {
4015 case IPPROTO_TCP:
4016- if (nf_flow_nat_port_tcp(skb, thoff, port, new_port) < 0)
4017- return NF_DROP;
4018+ nf_flow_nat_port_tcp(skb, thoff, port, new_port);
4019 break;
4020 case IPPROTO_UDP:
4021- if (nf_flow_nat_port_udp(skb, thoff, port, new_port) < 0)
4022- return NF_DROP;
4023+ nf_flow_nat_port_udp(skb, thoff, port, new_port);
4024 break;
4025 }
4026-
4027- return 0;
4028 }
4029
4030-int nf_flow_snat_port(const struct flow_offload *flow,
4031- struct sk_buff *skb, unsigned int thoff,
4032- u8 protocol, enum flow_offload_tuple_dir dir)
4033+void nf_flow_snat_port(const struct flow_offload *flow,
4034+ struct sk_buff *skb, unsigned int thoff,
4035+ u8 protocol, enum flow_offload_tuple_dir dir)
4036 {
4037 struct flow_ports *hdr;
4038 __be16 port, new_port;
4039
4040- if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
4041- skb_try_make_writable(skb, thoff + sizeof(*hdr)))
4042- return -1;
4043-
4044 hdr = (void *)(skb_network_header(skb) + thoff);
4045
4046 switch (dir) {
developerb7c46752022-07-04 19:51:38 +08004047@@ -463,25 +540,19 @@ int nf_flow_snat_port(const struct flow_offload *flow,
developer8cb3ac72022-07-04 10:55:14 +08004048 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port;
4049 hdr->dest = new_port;
4050 break;
4051- default:
4052- return -1;
4053 }
4054
4055- return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
4056+ nf_flow_nat_port(skb, thoff, protocol, port, new_port);
4057 }
4058 EXPORT_SYMBOL_GPL(nf_flow_snat_port);
4059
4060-int nf_flow_dnat_port(const struct flow_offload *flow,
4061- struct sk_buff *skb, unsigned int thoff,
4062- u8 protocol, enum flow_offload_tuple_dir dir)
4063+void nf_flow_dnat_port(const struct flow_offload *flow, struct sk_buff *skb,
4064+ unsigned int thoff, u8 protocol,
4065+ enum flow_offload_tuple_dir dir)
4066 {
4067 struct flow_ports *hdr;
4068 __be16 port, new_port;
4069
4070- if (!pskb_may_pull(skb, thoff + sizeof(*hdr)) ||
4071- skb_try_make_writable(skb, thoff + sizeof(*hdr)))
4072- return -1;
4073-
4074 hdr = (void *)(skb_network_header(skb) + thoff);
4075
4076 switch (dir) {
developerb7c46752022-07-04 19:51:38 +08004077@@ -495,11 +566,9 @@ int nf_flow_dnat_port(const struct flow_offload *flow,
developer8cb3ac72022-07-04 10:55:14 +08004078 new_port = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port;
4079 hdr->source = new_port;
4080 break;
4081- default:
4082- return -1;
4083 }
4084
4085- return nf_flow_nat_port(skb, thoff, protocol, port, new_port);
4086+ nf_flow_nat_port(skb, thoff, protocol, port, new_port);
4087 }
4088 EXPORT_SYMBOL_GPL(nf_flow_dnat_port);
4089
developerb7c46752022-07-04 19:51:38 +08004090@@ -507,7 +576,9 @@ int nf_flow_table_init(struct nf_flowtable *flowtable)
developer8cb3ac72022-07-04 10:55:14 +08004091 {
4092 int err;
4093
4094- INIT_DEFERRABLE_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
4095+ INIT_DELAYED_WORK(&flowtable->gc_work, nf_flow_offload_work_gc);
4096+ flow_block_init(&flowtable->flow_block);
4097+ init_rwsem(&flowtable->flow_block_lock);
4098
4099 err = rhashtable_init(&flowtable->rhashtable,
4100 &nf_flow_offload_rhash_params);
developerb7c46752022-07-04 19:51:38 +08004101@@ -528,25 +599,24 @@ EXPORT_SYMBOL_GPL(nf_flow_table_init);
developer8cb3ac72022-07-04 10:55:14 +08004102 static void nf_flow_table_do_cleanup(struct flow_offload *flow, void *data)
4103 {
4104 struct net_device *dev = data;
4105- struct flow_offload_entry *e;
4106-
4107- e = container_of(flow, struct flow_offload_entry, flow);
4108
4109 if (!dev) {
4110 flow_offload_teardown(flow);
4111 return;
4112 }
4113- if (net_eq(nf_ct_net(e->ct), dev_net(dev)) &&
4114+
4115+ if (net_eq(nf_ct_net(flow->ct), dev_net(dev)) &&
4116 (flow->tuplehash[0].tuple.iifidx == dev->ifindex ||
4117 flow->tuplehash[1].tuple.iifidx == dev->ifindex))
4118- flow_offload_dead(flow);
4119+ flow_offload_teardown(flow);
4120 }
4121
4122-static void nf_flow_table_iterate_cleanup(struct nf_flowtable *flowtable,
4123- struct net_device *dev)
4124+void nf_flow_table_gc_cleanup(struct nf_flowtable *flowtable,
4125+ struct net_device *dev)
4126 {
4127 nf_flow_table_iterate(flowtable, nf_flow_table_do_cleanup, dev);
4128 flush_delayed_work(&flowtable->gc_work);
4129+ nf_flow_table_offload_flush(flowtable);
4130 }
4131
4132 void nf_flow_table_cleanup(struct net_device *dev)
developerb7c46752022-07-04 19:51:38 +08004133@@ -555,7 +625,7 @@ void nf_flow_table_cleanup(struct net_device *dev)
developer8cb3ac72022-07-04 10:55:14 +08004134
4135 mutex_lock(&flowtable_lock);
4136 list_for_each_entry(flowtable, &flowtables, list)
4137- nf_flow_table_iterate_cleanup(flowtable, dev);
4138+ nf_flow_table_gc_cleanup(flowtable, dev);
4139 mutex_unlock(&flowtable_lock);
4140 }
4141 EXPORT_SYMBOL_GPL(nf_flow_table_cleanup);
developerb7c46752022-07-04 19:51:38 +08004142@@ -565,9 +635,14 @@ void nf_flow_table_free(struct nf_flowtable *flow_table)
developer8cb3ac72022-07-04 10:55:14 +08004143 mutex_lock(&flowtable_lock);
4144 list_del(&flow_table->list);
4145 mutex_unlock(&flowtable_lock);
4146+
4147 cancel_delayed_work_sync(&flow_table->gc_work);
4148 nf_flow_table_iterate(flow_table, nf_flow_table_do_cleanup, NULL);
4149 nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step, flow_table);
4150+ nf_flow_table_offload_flush(flow_table);
4151+ if (nf_flowtable_hw_offload(flow_table))
4152+ nf_flow_table_iterate(flow_table, nf_flow_offload_gc_step,
4153+ flow_table);
4154 rhashtable_destroy(&flow_table->rhashtable);
4155 }
4156 EXPORT_SYMBOL_GPL(nf_flow_table_free);
developerb7c46752022-07-04 19:51:38 +08004157@@ -591,12 +666,23 @@ static struct notifier_block flow_offload_netdev_notifier = {
developer8cb3ac72022-07-04 10:55:14 +08004158
4159 static int __init nf_flow_table_module_init(void)
4160 {
4161- return register_netdevice_notifier(&flow_offload_netdev_notifier);
4162+ int ret;
4163+
4164+ ret = nf_flow_table_offload_init();
4165+ if (ret)
4166+ return ret;
4167+
4168+ ret = register_netdevice_notifier(&flow_offload_netdev_notifier);
4169+ if (ret)
4170+ nf_flow_table_offload_exit();
4171+
4172+ return ret;
4173 }
4174
4175 static void __exit nf_flow_table_module_exit(void)
4176 {
4177 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
4178+ nf_flow_table_offload_exit();
4179 }
4180
4181 module_init(nf_flow_table_module_init);
developerb7c46752022-07-04 19:51:38 +08004182@@ -604,3 +690,4 @@ module_exit(nf_flow_table_module_exit);
developer8cb3ac72022-07-04 10:55:14 +08004183
4184 MODULE_LICENSE("GPL");
4185 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
4186+MODULE_DESCRIPTION("Netfilter flow table module");
4187diff --git a/net/netfilter/nf_flow_table_ip.c b/net/netfilter/nf_flow_table_ip.c
developerb7c46752022-07-04 19:51:38 +08004188index 397129b2..6257d87c 100644
developer8cb3ac72022-07-04 10:55:14 +08004189--- a/net/netfilter/nf_flow_table_ip.c
4190+++ b/net/netfilter/nf_flow_table_ip.c
4191@@ -7,11 +7,13 @@
4192 #include <linux/ip.h>
4193 #include <linux/ipv6.h>
4194 #include <linux/netdevice.h>
4195+#include <linux/if_ether.h>
4196 #include <net/ip.h>
4197 #include <net/ipv6.h>
4198 #include <net/ip6_route.h>
4199 #include <net/neighbour.h>
4200 #include <net/netfilter/nf_flow_table.h>
4201+#include <net/netfilter/nf_conntrack_acct.h>
4202 /* For layer 4 checksum field offset. */
4203 #include <linux/tcp.h>
4204 #include <linux/udp.h>
4205@@ -24,9 +26,6 @@ static int nf_flow_state_check(struct flow_offload *flow, int proto,
4206 if (proto != IPPROTO_TCP)
4207 return 0;
4208
4209- if (!pskb_may_pull(skb, thoff + sizeof(*tcph)))
4210- return -1;
4211-
4212 tcph = (void *)(skb_network_header(skb) + thoff);
4213 if (unlikely(tcph->fin || tcph->rst)) {
4214 flow_offload_teardown(flow);
4215@@ -36,30 +35,20 @@ static int nf_flow_state_check(struct flow_offload *flow, int proto,
4216 return 0;
4217 }
4218
4219-static int nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
4220- __be32 addr, __be32 new_addr)
4221+static void nf_flow_nat_ip_tcp(struct sk_buff *skb, unsigned int thoff,
4222+ __be32 addr, __be32 new_addr)
4223 {
4224 struct tcphdr *tcph;
4225
4226- if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
4227- skb_try_make_writable(skb, thoff + sizeof(*tcph)))
4228- return -1;
4229-
4230 tcph = (void *)(skb_network_header(skb) + thoff);
4231 inet_proto_csum_replace4(&tcph->check, skb, addr, new_addr, true);
4232-
4233- return 0;
4234 }
4235
4236-static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
4237- __be32 addr, __be32 new_addr)
4238+static void nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
4239+ __be32 addr, __be32 new_addr)
4240 {
4241 struct udphdr *udph;
4242
4243- if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
4244- skb_try_make_writable(skb, thoff + sizeof(*udph)))
4245- return -1;
4246-
4247 udph = (void *)(skb_network_header(skb) + thoff);
4248 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
4249 inet_proto_csum_replace4(&udph->check, skb, addr,
4250@@ -67,31 +56,25 @@ static int nf_flow_nat_ip_udp(struct sk_buff *skb, unsigned int thoff,
4251 if (!udph->check)
4252 udph->check = CSUM_MANGLED_0;
4253 }
4254-
4255- return 0;
4256 }
4257
4258-static int nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
4259- unsigned int thoff, __be32 addr,
4260- __be32 new_addr)
4261+static void nf_flow_nat_ip_l4proto(struct sk_buff *skb, struct iphdr *iph,
4262+ unsigned int thoff, __be32 addr,
4263+ __be32 new_addr)
4264 {
4265 switch (iph->protocol) {
4266 case IPPROTO_TCP:
4267- if (nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr) < 0)
4268- return NF_DROP;
4269+ nf_flow_nat_ip_tcp(skb, thoff, addr, new_addr);
4270 break;
4271 case IPPROTO_UDP:
4272- if (nf_flow_nat_ip_udp(skb, thoff, addr, new_addr) < 0)
4273- return NF_DROP;
4274+ nf_flow_nat_ip_udp(skb, thoff, addr, new_addr);
4275 break;
4276 }
4277-
4278- return 0;
4279 }
4280
4281-static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4282- struct iphdr *iph, unsigned int thoff,
4283- enum flow_offload_tuple_dir dir)
4284+static void nf_flow_snat_ip(const struct flow_offload *flow,
4285+ struct sk_buff *skb, struct iphdr *iph,
4286+ unsigned int thoff, enum flow_offload_tuple_dir dir)
4287 {
4288 __be32 addr, new_addr;
4289
4290@@ -106,17 +89,15 @@ static int nf_flow_snat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4291 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
4292 iph->daddr = new_addr;
4293 break;
4294- default:
4295- return -1;
4296 }
4297 csum_replace4(&iph->check, addr, new_addr);
4298
4299- return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
4300+ nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
4301 }
4302
4303-static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4304- struct iphdr *iph, unsigned int thoff,
4305- enum flow_offload_tuple_dir dir)
4306+static void nf_flow_dnat_ip(const struct flow_offload *flow,
4307+ struct sk_buff *skb, struct iphdr *iph,
4308+ unsigned int thoff, enum flow_offload_tuple_dir dir)
4309 {
4310 __be32 addr, new_addr;
4311
4312@@ -131,29 +112,24 @@ static int nf_flow_dnat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4313 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
4314 iph->saddr = new_addr;
4315 break;
4316- default:
4317- return -1;
4318 }
4319 csum_replace4(&iph->check, addr, new_addr);
4320
4321- return nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
4322+ nf_flow_nat_ip_l4proto(skb, iph, thoff, addr, new_addr);
4323 }
4324
4325-static int nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4326- unsigned int thoff, enum flow_offload_tuple_dir dir)
4327+static void nf_flow_nat_ip(const struct flow_offload *flow, struct sk_buff *skb,
4328+ unsigned int thoff, enum flow_offload_tuple_dir dir,
4329+ struct iphdr *iph)
4330 {
4331- struct iphdr *iph = ip_hdr(skb);
4332-
4333- if (flow->flags & FLOW_OFFLOAD_SNAT &&
4334- (nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
4335- nf_flow_snat_ip(flow, skb, iph, thoff, dir) < 0))
4336- return -1;
4337- if (flow->flags & FLOW_OFFLOAD_DNAT &&
4338- (nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir) < 0 ||
4339- nf_flow_dnat_ip(flow, skb, iph, thoff, dir) < 0))
4340- return -1;
4341-
4342- return 0;
4343+ if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
4344+ nf_flow_snat_port(flow, skb, thoff, iph->protocol, dir);
4345+ nf_flow_snat_ip(flow, skb, iph, thoff, dir);
4346+ }
4347+ if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
4348+ nf_flow_dnat_port(flow, skb, thoff, iph->protocol, dir);
4349+ nf_flow_dnat_ip(flow, skb, iph, thoff, dir);
4350+ }
4351 }
4352
4353 static bool ip_has_options(unsigned int thoff)
4354@@ -161,35 +137,70 @@ static bool ip_has_options(unsigned int thoff)
4355 return thoff != sizeof(struct iphdr);
4356 }
4357
4358+static void nf_flow_tuple_encap(struct sk_buff *skb,
4359+ struct flow_offload_tuple *tuple)
4360+{
4361+ struct vlan_ethhdr *veth;
4362+ struct pppoe_hdr *phdr;
4363+ int i = 0;
4364+
4365+ if (skb_vlan_tag_present(skb)) {
4366+ tuple->encap[i].id = skb_vlan_tag_get(skb);
4367+ tuple->encap[i].proto = skb->vlan_proto;
4368+ i++;
4369+ }
4370+ switch (skb->protocol) {
4371+ case htons(ETH_P_8021Q):
4372+ veth = (struct vlan_ethhdr *)skb_mac_header(skb);
4373+ tuple->encap[i].id = ntohs(veth->h_vlan_TCI);
4374+ tuple->encap[i].proto = skb->protocol;
4375+ break;
4376+ case htons(ETH_P_PPP_SES):
4377+ phdr = (struct pppoe_hdr *)skb_mac_header(skb);
4378+ tuple->encap[i].id = ntohs(phdr->sid);
4379+ tuple->encap[i].proto = skb->protocol;
4380+ break;
4381+ }
4382+}
4383+
4384 static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
4385- struct flow_offload_tuple *tuple)
4386+ struct flow_offload_tuple *tuple, u32 *hdrsize,
4387+ u32 offset)
4388 {
4389 struct flow_ports *ports;
4390 unsigned int thoff;
4391 struct iphdr *iph;
4392
4393- if (!pskb_may_pull(skb, sizeof(*iph)))
4394+ if (!pskb_may_pull(skb, sizeof(*iph) + offset))
4395 return -1;
4396
4397- iph = ip_hdr(skb);
4398- thoff = iph->ihl * 4;
4399+ iph = (struct iphdr *)(skb_network_header(skb) + offset);
4400+ thoff = (iph->ihl * 4);
4401
4402 if (ip_is_fragment(iph) ||
4403 unlikely(ip_has_options(thoff)))
4404 return -1;
4405
4406- if (iph->protocol != IPPROTO_TCP &&
4407- iph->protocol != IPPROTO_UDP)
4408+ thoff += offset;
4409+
4410+ switch (iph->protocol) {
4411+ case IPPROTO_TCP:
4412+ *hdrsize = sizeof(struct tcphdr);
4413+ break;
4414+ case IPPROTO_UDP:
4415+ *hdrsize = sizeof(struct udphdr);
4416+ break;
4417+ default:
4418 return -1;
4419+ }
4420
4421 if (iph->ttl <= 1)
4422 return -1;
4423
4424- thoff = iph->ihl * 4;
4425- if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
4426+ if (!pskb_may_pull(skb, thoff + *hdrsize))
4427 return -1;
4428
4429- iph = ip_hdr(skb);
4430+ iph = (struct iphdr *)(skb_network_header(skb) + offset);
4431 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
4432
4433 tuple->src_v4.s_addr = iph->saddr;
4434@@ -199,6 +210,7 @@ static int nf_flow_tuple_ip(struct sk_buff *skb, const struct net_device *dev,
4435 tuple->l3proto = AF_INET;
4436 tuple->l4proto = iph->protocol;
4437 tuple->iifidx = dev->ifindex;
4438+ nf_flow_tuple_encap(skb, tuple);
4439
4440 return 0;
4441 }
developerb7c46752022-07-04 19:51:38 +08004442@@ -225,6 +237,75 @@ static unsigned int nf_flow_xmit_xfrm(struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08004443 return NF_STOLEN;
4444 }
4445
4446+static bool nf_flow_skb_encap_protocol(const struct sk_buff *skb, __be16 proto,
4447+ u32 *offset)
4448+{
4449+ struct vlan_ethhdr *veth;
4450+
4451+ switch (skb->protocol) {
4452+ case htons(ETH_P_8021Q):
4453+ veth = (struct vlan_ethhdr *)skb_mac_header(skb);
4454+ if (veth->h_vlan_encapsulated_proto == proto) {
4455+ *offset += VLAN_HLEN;
4456+ return true;
4457+ }
4458+ break;
4459+ case htons(ETH_P_PPP_SES):
4460+ if (nf_flow_pppoe_proto(skb) == proto) {
4461+ *offset += PPPOE_SES_HLEN;
4462+ return true;
4463+ }
4464+ break;
4465+ }
4466+
4467+ return false;
4468+}
4469+
4470+static void nf_flow_encap_pop(struct sk_buff *skb,
4471+ struct flow_offload_tuple_rhash *tuplehash)
4472+{
4473+ struct vlan_hdr *vlan_hdr;
4474+ int i;
4475+
4476+ for (i = 0; i < tuplehash->tuple.encap_num; i++) {
4477+ if (skb_vlan_tag_present(skb)) {
4478+ __vlan_hwaccel_clear_tag(skb);
4479+ continue;
4480+ }
4481+ switch (skb->protocol) {
4482+ case htons(ETH_P_8021Q):
4483+ vlan_hdr = (struct vlan_hdr *)skb->data;
4484+ __skb_pull(skb, VLAN_HLEN);
4485+ vlan_set_encap_proto(skb, vlan_hdr);
4486+ skb_reset_network_header(skb);
4487+ break;
4488+ case htons(ETH_P_PPP_SES):
4489+ skb->protocol = nf_flow_pppoe_proto(skb);
4490+ skb_pull(skb, PPPOE_SES_HLEN);
4491+ skb_reset_network_header(skb);
4492+ break;
4493+ }
4494+ }
4495+}
4496+
4497+static unsigned int nf_flow_queue_xmit(struct net *net, struct sk_buff *skb,
4498+ const struct flow_offload_tuple_rhash *tuplehash,
4499+ unsigned short type)
4500+{
4501+ struct net_device *outdev;
4502+
4503+ outdev = dev_get_by_index_rcu(net, tuplehash->tuple.out.ifidx);
4504+ if (!outdev)
4505+ return NF_DROP;
4506+
4507+ skb->dev = outdev;
4508+ dev_hard_header(skb, skb->dev, type, tuplehash->tuple.out.h_dest,
4509+ tuplehash->tuple.out.h_source, skb->len);
4510+ dev_queue_xmit(skb);
4511+
4512+ return NF_STOLEN;
4513+}
4514+
4515 unsigned int
4516 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
4517 const struct nf_hook_state *state)
developerb7c46752022-07-04 19:51:38 +08004518@@ -235,15 +316,18 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08004519 enum flow_offload_tuple_dir dir;
4520 struct flow_offload *flow;
4521 struct net_device *outdev;
4522+ u32 hdrsize, offset = 0;
4523+ unsigned int thoff, mtu;
4524 struct rtable *rt;
4525- unsigned int thoff;
4526 struct iphdr *iph;
4527 __be32 nexthop;
4528+ int ret;
4529
4530- if (skb->protocol != htons(ETH_P_IP))
4531+ if (skb->protocol != htons(ETH_P_IP) &&
4532+ !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IP), &offset))
4533 return NF_ACCEPT;
4534
4535- if (nf_flow_tuple_ip(skb, state->in, &tuple) < 0)
4536+ if (nf_flow_tuple_ip(skb, state->in, &tuple, &hdrsize, offset) < 0)
4537 return NF_ACCEPT;
4538
4539 tuplehash = flow_offload_lookup(flow_table, &tuple);
developerb7c46752022-07-04 19:51:38 +08004540@@ -252,75 +336,80 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08004541
4542 dir = tuplehash->tuple.dir;
4543 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
4544- rt = (struct rtable *)flow->tuplehash[dir].tuple.dst_cache;
4545- outdev = rt->dst.dev;
4546-
4547- if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
4548- return NF_ACCEPT;
developerb7c46752022-07-04 19:51:38 +08004549
developer8cb3ac72022-07-04 10:55:14 +08004550- if (skb_try_make_writable(skb, sizeof(*iph)))
4551- return NF_DROP;
developerb7c46752022-07-04 19:51:38 +08004552-
developer8cb3ac72022-07-04 10:55:14 +08004553- thoff = ip_hdr(skb)->ihl * 4;
4554- if (nf_flow_state_check(flow, ip_hdr(skb)->protocol, skb, thoff))
4555+ mtu = flow->tuplehash[dir].tuple.mtu + offset;
4556+ if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
4557 return NF_ACCEPT;
4558
developerb7c46752022-07-04 19:51:38 +08004559- if (!dst_check(&rt->dst, 0)) {
developer8cb3ac72022-07-04 10:55:14 +08004560- flow_offload_teardown(flow);
4561+ iph = (struct iphdr *)(skb_network_header(skb) + offset);
4562+ thoff = (iph->ihl * 4) + offset;
4563+ if (nf_flow_state_check(flow, iph->protocol, skb, thoff))
4564 return NF_ACCEPT;
4565- }
4566
4567- if (nf_flow_nat_ip(flow, skb, thoff, dir) < 0)
4568+ if (skb_try_make_writable(skb, thoff + hdrsize))
4569 return NF_DROP;
4570
4571- flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
4572+ flow_offload_refresh(flow_table, flow);
4573+
4574+ nf_flow_encap_pop(skb, tuplehash);
4575+ thoff -= offset;
4576+
4577 iph = ip_hdr(skb);
4578+ nf_flow_nat_ip(flow, skb, thoff, dir, iph);
4579+
4580 ip_decrease_ttl(iph);
4581 skb->tstamp = 0;
4582
4583- if (unlikely(dst_xfrm(&rt->dst))) {
4584+ if (flow_table->flags & NF_FLOWTABLE_COUNTER)
4585+ nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
4586+
4587+ if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
4588+ rt = (struct rtable *)tuplehash->tuple.dst_cache;
4589 memset(skb->cb, 0, sizeof(struct inet_skb_parm));
4590 IPCB(skb)->iif = skb->dev->ifindex;
4591 IPCB(skb)->flags = IPSKB_FORWARDED;
4592 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
4593 }
4594
4595- skb->dev = outdev;
4596- nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
4597- skb_dst_set_noref(skb, &rt->dst);
4598- neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
4599+ switch (tuplehash->tuple.xmit_type) {
4600+ case FLOW_OFFLOAD_XMIT_NEIGH:
4601+ rt = (struct rtable *)tuplehash->tuple.dst_cache;
4602+ outdev = rt->dst.dev;
4603+ skb->dev = outdev;
4604+ nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
4605+ skb_dst_set_noref(skb, &rt->dst);
4606+ neigh_xmit(NEIGH_ARP_TABLE, outdev, &nexthop, skb);
4607+ ret = NF_STOLEN;
4608+ break;
4609+ case FLOW_OFFLOAD_XMIT_DIRECT:
4610+ ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IP);
4611+ if (ret == NF_DROP)
4612+ flow_offload_teardown(flow);
4613+ break;
4614+ }
4615
4616- return NF_STOLEN;
4617+ return ret;
4618 }
4619 EXPORT_SYMBOL_GPL(nf_flow_offload_ip_hook);
4620
4621-static int nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
4622- struct in6_addr *addr,
4623- struct in6_addr *new_addr)
4624+static void nf_flow_nat_ipv6_tcp(struct sk_buff *skb, unsigned int thoff,
4625+ struct in6_addr *addr,
4626+ struct in6_addr *new_addr,
4627+ struct ipv6hdr *ip6h)
4628 {
4629 struct tcphdr *tcph;
4630
4631- if (!pskb_may_pull(skb, thoff + sizeof(*tcph)) ||
4632- skb_try_make_writable(skb, thoff + sizeof(*tcph)))
4633- return -1;
4634-
4635 tcph = (void *)(skb_network_header(skb) + thoff);
4636 inet_proto_csum_replace16(&tcph->check, skb, addr->s6_addr32,
4637 new_addr->s6_addr32, true);
4638-
4639- return 0;
4640 }
4641
4642-static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
4643- struct in6_addr *addr,
4644- struct in6_addr *new_addr)
4645+static void nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
4646+ struct in6_addr *addr,
4647+ struct in6_addr *new_addr)
4648 {
4649 struct udphdr *udph;
4650
4651- if (!pskb_may_pull(skb, thoff + sizeof(*udph)) ||
4652- skb_try_make_writable(skb, thoff + sizeof(*udph)))
4653- return -1;
4654-
4655 udph = (void *)(skb_network_header(skb) + thoff);
4656 if (udph->check || skb->ip_summed == CHECKSUM_PARTIAL) {
4657 inet_proto_csum_replace16(&udph->check, skb, addr->s6_addr32,
developerb7c46752022-07-04 19:51:38 +08004658@@ -328,32 +417,26 @@ static int nf_flow_nat_ipv6_udp(struct sk_buff *skb, unsigned int thoff,
developer8cb3ac72022-07-04 10:55:14 +08004659 if (!udph->check)
4660 udph->check = CSUM_MANGLED_0;
4661 }
4662-
4663- return 0;
4664 }
4665
4666-static int nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
4667- unsigned int thoff, struct in6_addr *addr,
4668- struct in6_addr *new_addr)
4669+static void nf_flow_nat_ipv6_l4proto(struct sk_buff *skb, struct ipv6hdr *ip6h,
4670+ unsigned int thoff, struct in6_addr *addr,
4671+ struct in6_addr *new_addr)
4672 {
4673 switch (ip6h->nexthdr) {
4674 case IPPROTO_TCP:
4675- if (nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr) < 0)
4676- return NF_DROP;
4677+ nf_flow_nat_ipv6_tcp(skb, thoff, addr, new_addr, ip6h);
4678 break;
4679 case IPPROTO_UDP:
4680- if (nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr) < 0)
4681- return NF_DROP;
4682+ nf_flow_nat_ipv6_udp(skb, thoff, addr, new_addr);
4683 break;
4684 }
4685-
4686- return 0;
4687 }
4688
4689-static int nf_flow_snat_ipv6(const struct flow_offload *flow,
4690- struct sk_buff *skb, struct ipv6hdr *ip6h,
4691- unsigned int thoff,
4692- enum flow_offload_tuple_dir dir)
4693+static void nf_flow_snat_ipv6(const struct flow_offload *flow,
4694+ struct sk_buff *skb, struct ipv6hdr *ip6h,
4695+ unsigned int thoff,
4696+ enum flow_offload_tuple_dir dir)
4697 {
4698 struct in6_addr addr, new_addr;
4699
developerb7c46752022-07-04 19:51:38 +08004700@@ -368,17 +451,15 @@ static int nf_flow_snat_ipv6(const struct flow_offload *flow,
developer8cb3ac72022-07-04 10:55:14 +08004701 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6;
4702 ip6h->daddr = new_addr;
4703 break;
4704- default:
4705- return -1;
4706 }
4707
4708- return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
4709+ nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
4710 }
4711
4712-static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
4713- struct sk_buff *skb, struct ipv6hdr *ip6h,
4714- unsigned int thoff,
4715- enum flow_offload_tuple_dir dir)
4716+static void nf_flow_dnat_ipv6(const struct flow_offload *flow,
4717+ struct sk_buff *skb, struct ipv6hdr *ip6h,
4718+ unsigned int thoff,
4719+ enum flow_offload_tuple_dir dir)
4720 {
4721 struct in6_addr addr, new_addr;
4722
developerb7c46752022-07-04 19:51:38 +08004723@@ -393,56 +474,60 @@ static int nf_flow_dnat_ipv6(const struct flow_offload *flow,
developer8cb3ac72022-07-04 10:55:14 +08004724 new_addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6;
4725 ip6h->saddr = new_addr;
4726 break;
4727- default:
4728- return -1;
4729 }
4730
4731- return nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
4732+ nf_flow_nat_ipv6_l4proto(skb, ip6h, thoff, &addr, &new_addr);
4733 }
4734
4735-static int nf_flow_nat_ipv6(const struct flow_offload *flow,
4736- struct sk_buff *skb,
4737- enum flow_offload_tuple_dir dir)
4738+static void nf_flow_nat_ipv6(const struct flow_offload *flow,
4739+ struct sk_buff *skb,
4740+ enum flow_offload_tuple_dir dir,
4741+ struct ipv6hdr *ip6h)
4742 {
4743- struct ipv6hdr *ip6h = ipv6_hdr(skb);
4744 unsigned int thoff = sizeof(*ip6h);
4745
4746- if (flow->flags & FLOW_OFFLOAD_SNAT &&
4747- (nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
4748- nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
4749- return -1;
4750- if (flow->flags & FLOW_OFFLOAD_DNAT &&
4751- (nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir) < 0 ||
4752- nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir) < 0))
4753- return -1;
4754-
4755- return 0;
4756+ if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
4757+ nf_flow_snat_port(flow, skb, thoff, ip6h->nexthdr, dir);
4758+ nf_flow_snat_ipv6(flow, skb, ip6h, thoff, dir);
4759+ }
4760+ if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
4761+ nf_flow_dnat_port(flow, skb, thoff, ip6h->nexthdr, dir);
4762+ nf_flow_dnat_ipv6(flow, skb, ip6h, thoff, dir);
4763+ }
4764 }
4765
4766 static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
4767- struct flow_offload_tuple *tuple)
4768+ struct flow_offload_tuple *tuple, u32 *hdrsize,
4769+ u32 offset)
4770 {
4771 struct flow_ports *ports;
4772 struct ipv6hdr *ip6h;
4773 unsigned int thoff;
4774
4775- if (!pskb_may_pull(skb, sizeof(*ip6h)))
4776+ thoff = sizeof(*ip6h) + offset;
4777+ if (!pskb_may_pull(skb, thoff))
4778 return -1;
4779
4780- ip6h = ipv6_hdr(skb);
4781+ ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
4782
4783- if (ip6h->nexthdr != IPPROTO_TCP &&
4784- ip6h->nexthdr != IPPROTO_UDP)
4785+ switch (ip6h->nexthdr) {
4786+ case IPPROTO_TCP:
4787+ *hdrsize = sizeof(struct tcphdr);
4788+ break;
4789+ case IPPROTO_UDP:
4790+ *hdrsize = sizeof(struct udphdr);
4791+ break;
4792+ default:
4793 return -1;
4794+ }
4795
4796 if (ip6h->hop_limit <= 1)
4797 return -1;
4798
4799- thoff = sizeof(*ip6h);
4800- if (!pskb_may_pull(skb, thoff + sizeof(*ports)))
4801+ if (!pskb_may_pull(skb, thoff + *hdrsize))
4802 return -1;
4803
4804- ip6h = ipv6_hdr(skb);
4805+ ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
4806 ports = (struct flow_ports *)(skb_network_header(skb) + thoff);
4807
4808 tuple->src_v6 = ip6h->saddr;
developerb7c46752022-07-04 19:51:38 +08004809@@ -452,6 +537,7 @@ static int nf_flow_tuple_ipv6(struct sk_buff *skb, const struct net_device *dev,
developer8cb3ac72022-07-04 10:55:14 +08004810 tuple->l3proto = AF_INET6;
4811 tuple->l4proto = ip6h->nexthdr;
4812 tuple->iifidx = dev->ifindex;
4813+ nf_flow_tuple_encap(skb, tuple);
4814
4815 return 0;
4816 }
developerb7c46752022-07-04 19:51:38 +08004817@@ -467,13 +553,17 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08004818 const struct in6_addr *nexthop;
4819 struct flow_offload *flow;
4820 struct net_device *outdev;
4821+ unsigned int thoff, mtu;
4822+ u32 hdrsize, offset = 0;
4823 struct ipv6hdr *ip6h;
4824 struct rt6_info *rt;
4825+ int ret;
4826
4827- if (skb->protocol != htons(ETH_P_IPV6))
4828+ if (skb->protocol != htons(ETH_P_IPV6) &&
4829+ !nf_flow_skb_encap_protocol(skb, htons(ETH_P_IPV6), &offset))
4830 return NF_ACCEPT;
4831
4832- if (nf_flow_tuple_ipv6(skb, state->in, &tuple) < 0)
4833+ if (nf_flow_tuple_ipv6(skb, state->in, &tuple, &hdrsize, offset) < 0)
4834 return NF_ACCEPT;
4835
4836 tuplehash = flow_offload_lookup(flow_table, &tuple);
developerb7c46752022-07-04 19:51:38 +08004837@@ -482,44 +572,57 @@ nf_flow_offload_ipv6_hook(void *priv, struct sk_buff *skb,
developer8cb3ac72022-07-04 10:55:14 +08004838
4839 dir = tuplehash->tuple.dir;
4840 flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
4841- rt = (struct rt6_info *)flow->tuplehash[dir].tuple.dst_cache;
4842- outdev = rt->dst.dev;
developer8cb3ac72022-07-04 10:55:14 +08004843
developerb7c46752022-07-04 19:51:38 +08004844- if (unlikely(nf_flow_exceeds_mtu(skb, flow->tuplehash[dir].tuple.mtu)))
developer8cb3ac72022-07-04 10:55:14 +08004845+ mtu = flow->tuplehash[dir].tuple.mtu + offset;
4846+ if (unlikely(nf_flow_exceeds_mtu(skb, mtu)))
4847 return NF_ACCEPT;
4848
developerb7c46752022-07-04 19:51:38 +08004849- if (nf_flow_state_check(flow, ipv6_hdr(skb)->nexthdr, skb,
4850- sizeof(*ip6h)))
developer8cb3ac72022-07-04 10:55:14 +08004851+ ip6h = (struct ipv6hdr *)(skb_network_header(skb) + offset);
4852+ thoff = sizeof(*ip6h) + offset;
4853+ if (nf_flow_state_check(flow, ip6h->nexthdr, skb, thoff))
4854 return NF_ACCEPT;
developer8cb3ac72022-07-04 10:55:14 +08004855
developerb7c46752022-07-04 19:51:38 +08004856- if (!dst_check(&rt->dst, tuplehash->tuple.dst_cookie)) {
4857- flow_offload_teardown(flow);
4858- return NF_ACCEPT;
4859- }
4860-
developer8cb3ac72022-07-04 10:55:14 +08004861- if (skb_try_make_writable(skb, sizeof(*ip6h)))
4862+ if (skb_try_make_writable(skb, thoff + hdrsize))
4863 return NF_DROP;
4864
4865- if (nf_flow_nat_ipv6(flow, skb, dir) < 0)
4866- return NF_DROP;
4867+ flow_offload_refresh(flow_table, flow);
4868+
4869+ nf_flow_encap_pop(skb, tuplehash);
4870
4871- flow->timeout = (u32)jiffies + NF_FLOW_TIMEOUT;
4872 ip6h = ipv6_hdr(skb);
4873+ nf_flow_nat_ipv6(flow, skb, dir, ip6h);
4874+
4875 ip6h->hop_limit--;
4876 skb->tstamp = 0;
4877
4878- if (unlikely(dst_xfrm(&rt->dst))) {
4879+ if (flow_table->flags & NF_FLOWTABLE_COUNTER)
4880+ nf_ct_acct_update(flow->ct, tuplehash->tuple.dir, skb->len);
4881+
4882+ if (unlikely(tuplehash->tuple.xmit_type == FLOW_OFFLOAD_XMIT_XFRM)) {
4883+ rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
4884 memset(skb->cb, 0, sizeof(struct inet6_skb_parm));
4885 IP6CB(skb)->iif = skb->dev->ifindex;
4886 IP6CB(skb)->flags = IP6SKB_FORWARDED;
4887 return nf_flow_xmit_xfrm(skb, state, &rt->dst);
4888 }
4889
4890- skb->dev = outdev;
4891- nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
4892- skb_dst_set_noref(skb, &rt->dst);
4893- neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
4894+ switch (tuplehash->tuple.xmit_type) {
4895+ case FLOW_OFFLOAD_XMIT_NEIGH:
4896+ rt = (struct rt6_info *)tuplehash->tuple.dst_cache;
4897+ outdev = rt->dst.dev;
4898+ skb->dev = outdev;
4899+ nexthop = rt6_nexthop(rt, &flow->tuplehash[!dir].tuple.src_v6);
4900+ skb_dst_set_noref(skb, &rt->dst);
4901+ neigh_xmit(NEIGH_ND_TABLE, outdev, nexthop, skb);
4902+ ret = NF_STOLEN;
4903+ break;
4904+ case FLOW_OFFLOAD_XMIT_DIRECT:
4905+ ret = nf_flow_queue_xmit(state->net, skb, tuplehash, ETH_P_IPV6);
4906+ if (ret == NF_DROP)
4907+ flow_offload_teardown(flow);
4908+ break;
4909+ }
4910
4911- return NF_STOLEN;
4912+ return ret;
4913 }
4914 EXPORT_SYMBOL_GPL(nf_flow_offload_ipv6_hook);
4915diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
4916new file mode 100644
4917index 000000000..d94c6fb92
4918--- /dev/null
4919+++ b/net/netfilter/nf_flow_table_offload.c
4920@@ -0,0 +1,1191 @@
4921+#include <linux/kernel.h>
4922+#include <linux/init.h>
4923+#include <linux/module.h>
4924+#include <linux/netfilter.h>
4925+#include <linux/rhashtable.h>
4926+#include <linux/netdevice.h>
4927+#include <linux/tc_act/tc_csum.h>
4928+#include <net/flow_offload.h>
4929+#include <net/netfilter/nf_flow_table.h>
4930+#include <net/netfilter/nf_tables.h>
4931+#include <net/netfilter/nf_conntrack.h>
4932+#include <net/netfilter/nf_conntrack_acct.h>
4933+#include <net/netfilter/nf_conntrack_core.h>
4934+#include <net/netfilter/nf_conntrack_tuple.h>
4935+
4936+static struct workqueue_struct *nf_flow_offload_add_wq;
4937+static struct workqueue_struct *nf_flow_offload_del_wq;
4938+static struct workqueue_struct *nf_flow_offload_stats_wq;
4939+
4940+struct flow_offload_work {
4941+ struct list_head list;
4942+ enum flow_cls_command cmd;
4943+ int priority;
4944+ struct nf_flowtable *flowtable;
4945+ struct flow_offload *flow;
4946+ struct work_struct work;
4947+};
4948+
4949+#define NF_FLOW_DISSECTOR(__match, __type, __field) \
4950+ (__match)->dissector.offset[__type] = \
4951+ offsetof(struct nf_flow_key, __field)
4952+
4953+static void nf_flow_rule_lwt_match(struct nf_flow_match *match,
4954+ struct ip_tunnel_info *tun_info)
4955+{
4956+ struct nf_flow_key *mask = &match->mask;
4957+ struct nf_flow_key *key = &match->key;
4958+ unsigned int enc_keys;
4959+
4960+ if (!tun_info || !(tun_info->mode & IP_TUNNEL_INFO_TX))
4961+ return;
4962+
4963+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_ENC_CONTROL, enc_control);
4964+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
4965+ key->enc_key_id.keyid = tunnel_id_to_key32(tun_info->key.tun_id);
4966+ mask->enc_key_id.keyid = 0xffffffff;
4967+ enc_keys = BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) |
4968+ BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL);
4969+
4970+ if (ip_tunnel_info_af(tun_info) == AF_INET) {
4971+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
4972+ enc_ipv4);
4973+ key->enc_ipv4.src = tun_info->key.u.ipv4.dst;
4974+ key->enc_ipv4.dst = tun_info->key.u.ipv4.src;
4975+ if (key->enc_ipv4.src)
4976+ mask->enc_ipv4.src = 0xffffffff;
4977+ if (key->enc_ipv4.dst)
4978+ mask->enc_ipv4.dst = 0xffffffff;
4979+ enc_keys |= BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS);
4980+ key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
4981+ } else {
4982+ memcpy(&key->enc_ipv6.src, &tun_info->key.u.ipv6.dst,
4983+ sizeof(struct in6_addr));
4984+ memcpy(&key->enc_ipv6.dst, &tun_info->key.u.ipv6.src,
4985+ sizeof(struct in6_addr));
4986+ if (memcmp(&key->enc_ipv6.src, &in6addr_any,
4987+ sizeof(struct in6_addr)))
4988+ memset(&mask->enc_ipv6.src, 0xff,
4989+ sizeof(struct in6_addr));
4990+ if (memcmp(&key->enc_ipv6.dst, &in6addr_any,
4991+ sizeof(struct in6_addr)))
4992+ memset(&mask->enc_ipv6.dst, 0xff,
4993+ sizeof(struct in6_addr));
4994+ enc_keys |= BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS);
4995+ key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
4996+ }
4997+
4998+ match->dissector.used_keys |= enc_keys;
4999+}
5000+
5001+static void nf_flow_rule_vlan_match(struct flow_dissector_key_vlan *key,
5002+ struct flow_dissector_key_vlan *mask,
5003+ u16 vlan_id, __be16 proto)
5004+{
5005+ key->vlan_id = vlan_id;
5006+ mask->vlan_id = VLAN_VID_MASK;
5007+ key->vlan_tpid = proto;
5008+ mask->vlan_tpid = 0xffff;
5009+}
5010+
5011+static int nf_flow_rule_match(struct nf_flow_match *match,
5012+ const struct flow_offload_tuple *tuple,
5013+ struct dst_entry *other_dst)
5014+{
5015+ struct nf_flow_key *mask = &match->mask;
5016+ struct nf_flow_key *key = &match->key;
5017+ struct ip_tunnel_info *tun_info;
5018+ bool vlan_encap = false;
5019+
5020+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_META, meta);
5021+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_CONTROL, control);
5022+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_BASIC, basic);
5023+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
5024+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
5025+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_TCP, tcp);
5026+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_PORTS, tp);
5027+
5028+ if (other_dst && other_dst->lwtstate) {
5029+ tun_info = lwt_tun_info(other_dst->lwtstate);
5030+ nf_flow_rule_lwt_match(match, tun_info);
5031+ }
5032+
5033+ key->meta.ingress_ifindex = tuple->iifidx;
5034+ mask->meta.ingress_ifindex = 0xffffffff;
5035+
5036+ if (tuple->encap_num > 0 && !(tuple->in_vlan_ingress & BIT(0)) &&
5037+ tuple->encap[0].proto == htons(ETH_P_8021Q)) {
5038+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_VLAN, vlan);
5039+ nf_flow_rule_vlan_match(&key->vlan, &mask->vlan,
5040+ tuple->encap[0].id,
5041+ tuple->encap[0].proto);
5042+ vlan_encap = true;
5043+ }
5044+
5045+ if (tuple->encap_num > 1 && !(tuple->in_vlan_ingress & BIT(1)) &&
5046+ tuple->encap[1].proto == htons(ETH_P_8021Q)) {
5047+ if (vlan_encap) {
5048+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_CVLAN,
5049+ cvlan);
5050+ nf_flow_rule_vlan_match(&key->cvlan, &mask->cvlan,
5051+ tuple->encap[1].id,
5052+ tuple->encap[1].proto);
5053+ } else {
5054+ NF_FLOW_DISSECTOR(match, FLOW_DISSECTOR_KEY_VLAN,
5055+ vlan);
5056+ nf_flow_rule_vlan_match(&key->vlan, &mask->vlan,
5057+ tuple->encap[1].id,
5058+ tuple->encap[1].proto);
5059+ }
5060+ }
5061+
5062+ switch (tuple->l3proto) {
5063+ case AF_INET:
5064+ key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
5065+ key->basic.n_proto = htons(ETH_P_IP);
5066+ key->ipv4.src = tuple->src_v4.s_addr;
5067+ mask->ipv4.src = 0xffffffff;
5068+ key->ipv4.dst = tuple->dst_v4.s_addr;
5069+ mask->ipv4.dst = 0xffffffff;
5070+ break;
5071+ case AF_INET6:
5072+ key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
5073+ key->basic.n_proto = htons(ETH_P_IPV6);
5074+ key->ipv6.src = tuple->src_v6;
5075+ memset(&mask->ipv6.src, 0xff, sizeof(mask->ipv6.src));
5076+ key->ipv6.dst = tuple->dst_v6;
5077+ memset(&mask->ipv6.dst, 0xff, sizeof(mask->ipv6.dst));
5078+ break;
5079+ default:
5080+ return -EOPNOTSUPP;
5081+ }
5082+ mask->control.addr_type = 0xffff;
5083+ match->dissector.used_keys |= BIT(key->control.addr_type);
5084+ mask->basic.n_proto = 0xffff;
5085+
5086+ switch (tuple->l4proto) {
5087+ case IPPROTO_TCP:
5088+ key->tcp.flags = 0;
5089+ mask->tcp.flags = cpu_to_be16(be32_to_cpu(TCP_FLAG_RST | TCP_FLAG_FIN) >> 16);
5090+ match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_TCP);
5091+ break;
5092+ case IPPROTO_UDP:
5093+ break;
5094+ default:
5095+ return -EOPNOTSUPP;
5096+ }
5097+
5098+ key->basic.ip_proto = tuple->l4proto;
5099+ mask->basic.ip_proto = 0xff;
5100+
5101+ key->tp.src = tuple->src_port;
5102+ mask->tp.src = 0xffff;
5103+ key->tp.dst = tuple->dst_port;
5104+ mask->tp.dst = 0xffff;
5105+
5106+ match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_META) |
5107+ BIT(FLOW_DISSECTOR_KEY_CONTROL) |
5108+ BIT(FLOW_DISSECTOR_KEY_BASIC) |
5109+ BIT(FLOW_DISSECTOR_KEY_PORTS);
5110+ return 0;
5111+}
5112+
5113+static void flow_offload_mangle(struct flow_action_entry *entry,
5114+ enum flow_action_mangle_base htype, u32 offset,
5115+ const __be32 *value, const __be32 *mask)
5116+{
5117+ entry->id = FLOW_ACTION_MANGLE;
5118+ entry->mangle.htype = htype;
5119+ entry->mangle.offset = offset;
5120+ memcpy(&entry->mangle.mask, mask, sizeof(u32));
5121+ memcpy(&entry->mangle.val, value, sizeof(u32));
5122+}
5123+
5124+static inline struct flow_action_entry *
5125+flow_action_entry_next(struct nf_flow_rule *flow_rule)
5126+{
5127+ int i = flow_rule->rule->action.num_entries++;
5128+
5129+ return &flow_rule->rule->action.entries[i];
5130+}
5131+
5132+static int flow_offload_eth_src(struct net *net,
5133+ const struct flow_offload *flow,
5134+ enum flow_offload_tuple_dir dir,
5135+ struct nf_flow_rule *flow_rule)
5136+{
5137+ struct flow_action_entry *entry0 = flow_action_entry_next(flow_rule);
5138+ struct flow_action_entry *entry1 = flow_action_entry_next(flow_rule);
5139+ const struct flow_offload_tuple *other_tuple, *this_tuple;
5140+ struct net_device *dev = NULL;
5141+ const unsigned char *addr;
5142+ u32 mask, val;
5143+ u16 val16;
5144+
5145+ this_tuple = &flow->tuplehash[dir].tuple;
5146+
5147+ switch (this_tuple->xmit_type) {
5148+ case FLOW_OFFLOAD_XMIT_DIRECT:
5149+ addr = this_tuple->out.h_source;
5150+ break;
5151+ case FLOW_OFFLOAD_XMIT_NEIGH:
5152+ other_tuple = &flow->tuplehash[!dir].tuple;
5153+ dev = dev_get_by_index(net, other_tuple->iifidx);
5154+ if (!dev)
5155+ return -ENOENT;
5156+
5157+ addr = dev->dev_addr;
5158+ break;
5159+ default:
5160+ return -EOPNOTSUPP;
5161+ }
5162+
5163+ mask = ~0xffff0000;
5164+ memcpy(&val16, addr, 2);
5165+ val = val16 << 16;
5166+ flow_offload_mangle(entry0, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 4,
5167+ &val, &mask);
5168+
5169+ mask = ~0xffffffff;
5170+ memcpy(&val, addr + 2, 4);
5171+ flow_offload_mangle(entry1, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 8,
5172+ &val, &mask);
5173+
5174+ if (dev)
5175+ dev_put(dev);
5176+
5177+ return 0;
5178+}
5179+
5180+static int flow_offload_eth_dst(struct net *net,
5181+ const struct flow_offload *flow,
5182+ enum flow_offload_tuple_dir dir,
5183+ struct nf_flow_rule *flow_rule)
5184+{
5185+ struct flow_action_entry *entry0 = flow_action_entry_next(flow_rule);
5186+ struct flow_action_entry *entry1 = flow_action_entry_next(flow_rule);
5187+ const struct flow_offload_tuple *other_tuple, *this_tuple;
5188+ const struct dst_entry *dst_cache;
5189+ unsigned char ha[ETH_ALEN];
5190+ struct neighbour *n;
5191+ const void *daddr;
5192+ u32 mask, val;
5193+ u8 nud_state;
5194+ u16 val16;
5195+
5196+ this_tuple = &flow->tuplehash[dir].tuple;
5197+
5198+ switch (this_tuple->xmit_type) {
5199+ case FLOW_OFFLOAD_XMIT_DIRECT:
5200+ ether_addr_copy(ha, this_tuple->out.h_dest);
5201+ break;
5202+ case FLOW_OFFLOAD_XMIT_NEIGH:
5203+ other_tuple = &flow->tuplehash[!dir].tuple;
5204+ daddr = &other_tuple->src_v4;
5205+ dst_cache = this_tuple->dst_cache;
5206+ n = dst_neigh_lookup(dst_cache, daddr);
5207+ if (!n)
5208+ return -ENOENT;
5209+
5210+ read_lock_bh(&n->lock);
5211+ nud_state = n->nud_state;
5212+ ether_addr_copy(ha, n->ha);
5213+ read_unlock_bh(&n->lock);
5214+ neigh_release(n);
5215+
5216+ if (!(nud_state & NUD_VALID))
5217+ return -ENOENT;
5218+ break;
5219+ default:
5220+ return -EOPNOTSUPP;
5221+ }
5222+
5223+ mask = ~0xffffffff;
5224+ memcpy(&val, ha, 4);
5225+ flow_offload_mangle(entry0, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 0,
5226+ &val, &mask);
5227+
5228+ mask = ~0x0000ffff;
5229+ memcpy(&val16, ha + 4, 2);
5230+ val = val16;
5231+ flow_offload_mangle(entry1, FLOW_ACT_MANGLE_HDR_TYPE_ETH, 4,
5232+ &val, &mask);
5233+
5234+ return 0;
5235+}
5236+
5237+static void flow_offload_ipv4_snat(struct net *net,
5238+ const struct flow_offload *flow,
5239+ enum flow_offload_tuple_dir dir,
5240+ struct nf_flow_rule *flow_rule)
5241+{
5242+ struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
5243+ u32 mask = ~htonl(0xffffffff);
5244+ __be32 addr;
5245+ u32 offset;
5246+
5247+ switch (dir) {
5248+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5249+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v4.s_addr;
5250+ offset = offsetof(struct iphdr, saddr);
5251+ break;
5252+ case FLOW_OFFLOAD_DIR_REPLY:
5253+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v4.s_addr;
5254+ offset = offsetof(struct iphdr, daddr);
5255+ break;
5256+ default:
5257+ return;
5258+ }
5259+
5260+ flow_offload_mangle(entry, FLOW_ACT_MANGLE_HDR_TYPE_IP4, offset,
5261+ &addr, &mask);
5262+}
5263+
5264+static void flow_offload_ipv4_dnat(struct net *net,
5265+ const struct flow_offload *flow,
5266+ enum flow_offload_tuple_dir dir,
5267+ struct nf_flow_rule *flow_rule)
5268+{
5269+ struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
5270+ u32 mask = ~htonl(0xffffffff);
5271+ __be32 addr;
5272+ u32 offset;
5273+
5274+ switch (dir) {
5275+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5276+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v4.s_addr;
5277+ offset = offsetof(struct iphdr, daddr);
5278+ break;
5279+ case FLOW_OFFLOAD_DIR_REPLY:
5280+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v4.s_addr;
5281+ offset = offsetof(struct iphdr, saddr);
5282+ break;
5283+ default:
5284+ return;
5285+ }
5286+
5287+ flow_offload_mangle(entry, FLOW_ACT_MANGLE_HDR_TYPE_IP4, offset,
5288+ &addr, &mask);
5289+}
5290+
5291+static void flow_offload_ipv6_mangle(struct nf_flow_rule *flow_rule,
5292+ unsigned int offset,
5293+ const __be32 *addr, const __be32 *mask)
5294+{
5295+ struct flow_action_entry *entry;
5296+ int i, j;
5297+
5298+ for (i = 0, j = 0; i < sizeof(struct in6_addr) / sizeof(u32); i += sizeof(u32), j++) {
5299+ entry = flow_action_entry_next(flow_rule);
5300+ flow_offload_mangle(entry, FLOW_ACT_MANGLE_HDR_TYPE_IP6,
5301+ offset + i, &addr[j], mask);
5302+ }
5303+}
5304+
5305+static void flow_offload_ipv6_snat(struct net *net,
5306+ const struct flow_offload *flow,
5307+ enum flow_offload_tuple_dir dir,
5308+ struct nf_flow_rule *flow_rule)
5309+{
5310+ u32 mask = ~htonl(0xffffffff);
5311+ const __be32 *addr;
5312+ u32 offset;
5313+
5314+ switch (dir) {
5315+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5316+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_v6.s6_addr32;
5317+ offset = offsetof(struct ipv6hdr, saddr);
5318+ break;
5319+ case FLOW_OFFLOAD_DIR_REPLY:
5320+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_v6.s6_addr32;
5321+ offset = offsetof(struct ipv6hdr, daddr);
5322+ break;
5323+ default:
5324+ return;
5325+ }
5326+
5327+ flow_offload_ipv6_mangle(flow_rule, offset, addr, &mask);
5328+}
5329+
5330+static void flow_offload_ipv6_dnat(struct net *net,
5331+ const struct flow_offload *flow,
5332+ enum flow_offload_tuple_dir dir,
5333+ struct nf_flow_rule *flow_rule)
5334+{
5335+ u32 mask = ~htonl(0xffffffff);
5336+ const __be32 *addr;
5337+ u32 offset;
5338+
5339+ switch (dir) {
5340+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5341+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_v6.s6_addr32;
5342+ offset = offsetof(struct ipv6hdr, daddr);
5343+ break;
5344+ case FLOW_OFFLOAD_DIR_REPLY:
5345+ addr = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_v6.s6_addr32;
5346+ offset = offsetof(struct ipv6hdr, saddr);
5347+ break;
5348+ default:
5349+ return;
5350+ }
5351+
5352+ flow_offload_ipv6_mangle(flow_rule, offset, addr, &mask);
5353+}
5354+
5355+static int flow_offload_l4proto(const struct flow_offload *flow)
5356+{
5357+ u8 protonum = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.l4proto;
5358+ u8 type = 0;
5359+
5360+ switch (protonum) {
5361+ case IPPROTO_TCP:
5362+ type = FLOW_ACT_MANGLE_HDR_TYPE_TCP;
5363+ break;
5364+ case IPPROTO_UDP:
5365+ type = FLOW_ACT_MANGLE_HDR_TYPE_UDP;
5366+ break;
5367+ default:
5368+ break;
5369+ }
5370+
5371+ return type;
5372+}
5373+
5374+static void flow_offload_port_snat(struct net *net,
5375+ const struct flow_offload *flow,
5376+ enum flow_offload_tuple_dir dir,
5377+ struct nf_flow_rule *flow_rule)
5378+{
5379+ struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
5380+ u32 mask, port;
5381+ u32 offset;
5382+
5383+ switch (dir) {
5384+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5385+ port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.dst_port);
5386+ offset = 0; /* offsetof(struct tcphdr, source); */
5387+ port = htonl(port << 16);
5388+ mask = ~htonl(0xffff0000);
5389+ break;
5390+ case FLOW_OFFLOAD_DIR_REPLY:
5391+ port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.src_port);
5392+ offset = 0; /* offsetof(struct tcphdr, dest); */
5393+ port = htonl(port);
5394+ mask = ~htonl(0xffff);
5395+ break;
5396+ default:
5397+ return;
5398+ }
5399+
5400+ flow_offload_mangle(entry, flow_offload_l4proto(flow), offset,
5401+ &port, &mask);
5402+}
5403+
5404+static void flow_offload_port_dnat(struct net *net,
5405+ const struct flow_offload *flow,
5406+ enum flow_offload_tuple_dir dir,
5407+ struct nf_flow_rule *flow_rule)
5408+{
5409+ struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
5410+ u32 mask, port;
5411+ u32 offset;
5412+
5413+ switch (dir) {
5414+ case FLOW_OFFLOAD_DIR_ORIGINAL:
5415+ port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple.src_port);
5416+ offset = 0; /* offsetof(struct tcphdr, dest); */
5417+ port = htonl(port);
5418+ mask = ~htonl(0xffff);
5419+ break;
5420+ case FLOW_OFFLOAD_DIR_REPLY:
5421+ port = ntohs(flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.dst_port);
5422+ offset = 0; /* offsetof(struct tcphdr, source); */
5423+ port = htonl(port << 16);
5424+ mask = ~htonl(0xffff0000);
5425+ break;
5426+ default:
5427+ return;
5428+ }
5429+
5430+ flow_offload_mangle(entry, flow_offload_l4proto(flow), offset,
5431+ &port, &mask);
5432+}
5433+
5434+static void flow_offload_ipv4_checksum(struct net *net,
5435+ const struct flow_offload *flow,
5436+ struct nf_flow_rule *flow_rule)
5437+{
5438+ u8 protonum = flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple.l4proto;
5439+ struct flow_action_entry *entry = flow_action_entry_next(flow_rule);
5440+
5441+ entry->id = FLOW_ACTION_CSUM;
5442+ entry->csum_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR;
5443+
5444+ switch (protonum) {
5445+ case IPPROTO_TCP:
5446+ entry->csum_flags |= TCA_CSUM_UPDATE_FLAG_TCP;
5447+ break;
5448+ case IPPROTO_UDP:
5449+ entry->csum_flags |= TCA_CSUM_UPDATE_FLAG_UDP;
5450+ break;
5451+ }
5452+}
5453+
5454+static void flow_offload_redirect(struct net *net,
5455+ const struct flow_offload *flow,
5456+ enum flow_offload_tuple_dir dir,
5457+ struct nf_flow_rule *flow_rule)
5458+{
5459+ const struct flow_offload_tuple *this_tuple, *other_tuple;
5460+ struct flow_action_entry *entry;
5461+ struct net_device *dev;
5462+ int ifindex;
5463+
5464+ this_tuple = &flow->tuplehash[dir].tuple;
5465+ switch (this_tuple->xmit_type) {
5466+ case FLOW_OFFLOAD_XMIT_DIRECT:
5467+ this_tuple = &flow->tuplehash[dir].tuple;
5468+ ifindex = this_tuple->out.hw_ifidx;
5469+ break;
5470+ case FLOW_OFFLOAD_XMIT_NEIGH:
5471+ other_tuple = &flow->tuplehash[!dir].tuple;
5472+ ifindex = other_tuple->iifidx;
5473+ break;
5474+ default:
5475+ return;
5476+ }
5477+
5478+ dev = dev_get_by_index(net, ifindex);
5479+ if (!dev)
5480+ return;
5481+
5482+ entry = flow_action_entry_next(flow_rule);
5483+ entry->id = FLOW_ACTION_REDIRECT;
5484+ entry->dev = dev;
5485+}
5486+
5487+static void flow_offload_encap_tunnel(const struct flow_offload *flow,
5488+ enum flow_offload_tuple_dir dir,
5489+ struct nf_flow_rule *flow_rule)
5490+{
5491+ const struct flow_offload_tuple *this_tuple;
5492+ struct flow_action_entry *entry;
5493+ struct dst_entry *dst;
5494+
5495+ this_tuple = &flow->tuplehash[dir].tuple;
5496+ if (this_tuple->xmit_type == FLOW_OFFLOAD_XMIT_DIRECT)
5497+ return;
5498+
5499+ dst = this_tuple->dst_cache;
5500+ if (dst && dst->lwtstate) {
5501+ struct ip_tunnel_info *tun_info;
5502+
5503+ tun_info = lwt_tun_info(dst->lwtstate);
5504+ if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX)) {
5505+ entry = flow_action_entry_next(flow_rule);
5506+ entry->id = FLOW_ACTION_TUNNEL_ENCAP;
5507+ entry->tunnel = tun_info;
5508+ }
5509+ }
5510+}
5511+
5512+static void flow_offload_decap_tunnel(const struct flow_offload *flow,
5513+ enum flow_offload_tuple_dir dir,
5514+ struct nf_flow_rule *flow_rule)
5515+{
5516+ const struct flow_offload_tuple *other_tuple;
5517+ struct flow_action_entry *entry;
5518+ struct dst_entry *dst;
5519+
5520+ other_tuple = &flow->tuplehash[!dir].tuple;
5521+ if (other_tuple->xmit_type == FLOW_OFFLOAD_XMIT_DIRECT)
5522+ return;
5523+
5524+ dst = other_tuple->dst_cache;
5525+ if (dst && dst->lwtstate) {
5526+ struct ip_tunnel_info *tun_info;
5527+
5528+ tun_info = lwt_tun_info(dst->lwtstate);
5529+ if (tun_info && (tun_info->mode & IP_TUNNEL_INFO_TX)) {
5530+ entry = flow_action_entry_next(flow_rule);
5531+ entry->id = FLOW_ACTION_TUNNEL_DECAP;
5532+ }
5533+ }
5534+}
5535+
5536+static int
5537+nf_flow_rule_route_common(struct net *net, const struct flow_offload *flow,
5538+ enum flow_offload_tuple_dir dir,
5539+ struct nf_flow_rule *flow_rule)
5540+{
5541+ const struct flow_offload_tuple *other_tuple;
5542+ const struct flow_offload_tuple *tuple;
5543+ int i;
5544+
5545+ flow_offload_decap_tunnel(flow, dir, flow_rule);
5546+ flow_offload_encap_tunnel(flow, dir, flow_rule);
5547+
5548+ if (flow_offload_eth_src(net, flow, dir, flow_rule) < 0 ||
5549+ flow_offload_eth_dst(net, flow, dir, flow_rule) < 0)
5550+ return -1;
5551+
5552+ tuple = &flow->tuplehash[dir].tuple;
5553+
5554+ for (i = 0; i < tuple->encap_num; i++) {
5555+ struct flow_action_entry *entry;
5556+
5557+ if (tuple->in_vlan_ingress & BIT(i))
5558+ continue;
5559+
5560+ if (tuple->encap[i].proto == htons(ETH_P_8021Q)) {
5561+ entry = flow_action_entry_next(flow_rule);
5562+ entry->id = FLOW_ACTION_VLAN_POP;
5563+ }
5564+ }
5565+
5566+ other_tuple = &flow->tuplehash[!dir].tuple;
5567+
5568+ for (i = 0; i < other_tuple->encap_num; i++) {
5569+ struct flow_action_entry *entry;
5570+
5571+ if (other_tuple->in_vlan_ingress & BIT(i))
5572+ continue;
5573+
5574+ entry = flow_action_entry_next(flow_rule);
5575+
5576+ switch (other_tuple->encap[i].proto) {
5577+ case htons(ETH_P_PPP_SES):
5578+ entry->id = FLOW_ACTION_PPPOE_PUSH;
5579+ entry->pppoe.sid = other_tuple->encap[i].id;
5580+ break;
5581+ case htons(ETH_P_8021Q):
5582+ entry->id = FLOW_ACTION_VLAN_PUSH;
5583+ entry->vlan.vid = other_tuple->encap[i].id;
5584+ entry->vlan.proto = other_tuple->encap[i].proto;
5585+ break;
5586+ }
5587+ }
5588+
5589+ return 0;
5590+}
5591+
5592+int nf_flow_rule_route_ipv4(struct net *net, const struct flow_offload *flow,
5593+ enum flow_offload_tuple_dir dir,
5594+ struct nf_flow_rule *flow_rule)
5595+{
5596+ if (nf_flow_rule_route_common(net, flow, dir, flow_rule) < 0)
5597+ return -1;
5598+
5599+ if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
5600+ flow_offload_ipv4_snat(net, flow, dir, flow_rule);
5601+ flow_offload_port_snat(net, flow, dir, flow_rule);
5602+ }
5603+ if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
5604+ flow_offload_ipv4_dnat(net, flow, dir, flow_rule);
5605+ flow_offload_port_dnat(net, flow, dir, flow_rule);
5606+ }
5607+ if (test_bit(NF_FLOW_SNAT, &flow->flags) ||
5608+ test_bit(NF_FLOW_DNAT, &flow->flags))
5609+ flow_offload_ipv4_checksum(net, flow, flow_rule);
5610+
5611+ flow_offload_redirect(net, flow, dir, flow_rule);
5612+
5613+ return 0;
5614+}
5615+EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv4);
5616+
5617+int nf_flow_rule_route_ipv6(struct net *net, const struct flow_offload *flow,
5618+ enum flow_offload_tuple_dir dir,
5619+ struct nf_flow_rule *flow_rule)
5620+{
5621+ if (nf_flow_rule_route_common(net, flow, dir, flow_rule) < 0)
5622+ return -1;
5623+
5624+ if (test_bit(NF_FLOW_SNAT, &flow->flags)) {
5625+ flow_offload_ipv6_snat(net, flow, dir, flow_rule);
5626+ flow_offload_port_snat(net, flow, dir, flow_rule);
5627+ }
5628+ if (test_bit(NF_FLOW_DNAT, &flow->flags)) {
5629+ flow_offload_ipv6_dnat(net, flow, dir, flow_rule);
5630+ flow_offload_port_dnat(net, flow, dir, flow_rule);
5631+ }
5632+
5633+ flow_offload_redirect(net, flow, dir, flow_rule);
5634+
5635+ return 0;
5636+}
5637+EXPORT_SYMBOL_GPL(nf_flow_rule_route_ipv6);
5638+
5639+#define NF_FLOW_RULE_ACTION_MAX 16
5640+
5641+static struct nf_flow_rule *
5642+nf_flow_offload_rule_alloc(struct net *net,
5643+ const struct flow_offload_work *offload,
5644+ enum flow_offload_tuple_dir dir)
5645+{
5646+ const struct nf_flowtable *flowtable = offload->flowtable;
5647+ const struct flow_offload_tuple *tuple, *other_tuple;
5648+ const struct flow_offload *flow = offload->flow;
5649+ struct dst_entry *other_dst = NULL;
5650+ struct nf_flow_rule *flow_rule;
5651+ int err = -ENOMEM;
5652+
5653+ flow_rule = kzalloc(sizeof(*flow_rule), GFP_KERNEL);
5654+ if (!flow_rule)
5655+ goto err_flow;
5656+
5657+ flow_rule->rule = flow_rule_alloc(NF_FLOW_RULE_ACTION_MAX);
5658+ if (!flow_rule->rule)
5659+ goto err_flow_rule;
5660+
5661+ flow_rule->rule->match.dissector = &flow_rule->match.dissector;
5662+ flow_rule->rule->match.mask = &flow_rule->match.mask;
5663+ flow_rule->rule->match.key = &flow_rule->match.key;
5664+
5665+ tuple = &flow->tuplehash[dir].tuple;
5666+ other_tuple = &flow->tuplehash[!dir].tuple;
5667+ if (other_tuple->xmit_type == FLOW_OFFLOAD_XMIT_NEIGH)
5668+ other_dst = other_tuple->dst_cache;
5669+
5670+ err = nf_flow_rule_match(&flow_rule->match, tuple, other_dst);
5671+ if (err < 0)
5672+ goto err_flow_match;
5673+
5674+ flow_rule->rule->action.num_entries = 0;
5675+ if (flowtable->type->action(net, flow, dir, flow_rule) < 0)
5676+ goto err_flow_match;
5677+
5678+ return flow_rule;
5679+
5680+err_flow_match:
5681+ kfree(flow_rule->rule);
5682+err_flow_rule:
5683+ kfree(flow_rule);
5684+err_flow:
5685+ return NULL;
5686+}
5687+
5688+static void __nf_flow_offload_destroy(struct nf_flow_rule *flow_rule)
5689+{
5690+ struct flow_action_entry *entry;
5691+ int i;
5692+
5693+ for (i = 0; i < flow_rule->rule->action.num_entries; i++) {
5694+ entry = &flow_rule->rule->action.entries[i];
5695+ if (entry->id != FLOW_ACTION_REDIRECT)
5696+ continue;
5697+
5698+ dev_put(entry->dev);
5699+ }
5700+ kfree(flow_rule->rule);
5701+ kfree(flow_rule);
5702+}
5703+
5704+static void nf_flow_offload_destroy(struct nf_flow_rule *flow_rule[])
5705+{
5706+ int i;
5707+
5708+ for (i = 0; i < FLOW_OFFLOAD_DIR_MAX; i++)
5709+ __nf_flow_offload_destroy(flow_rule[i]);
5710+}
5711+
5712+static int nf_flow_offload_alloc(const struct flow_offload_work *offload,
5713+ struct nf_flow_rule *flow_rule[])
5714+{
5715+ struct net *net = read_pnet(&offload->flowtable->net);
5716+
5717+ flow_rule[0] = nf_flow_offload_rule_alloc(net, offload,
5718+ FLOW_OFFLOAD_DIR_ORIGINAL);
5719+ if (!flow_rule[0])
5720+ return -ENOMEM;
5721+
5722+ flow_rule[1] = nf_flow_offload_rule_alloc(net, offload,
5723+ FLOW_OFFLOAD_DIR_REPLY);
5724+ if (!flow_rule[1]) {
5725+ __nf_flow_offload_destroy(flow_rule[0]);
5726+ return -ENOMEM;
5727+ }
5728+
5729+ return 0;
5730+}
5731+
5732+static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
5733+ __be16 proto, int priority,
5734+ enum flow_cls_command cmd,
5735+ const struct flow_offload_tuple *tuple,
5736+ struct netlink_ext_ack *extack)
5737+{
5738+ cls_flow->common.protocol = proto;
5739+ cls_flow->common.prio = priority;
5740+ cls_flow->common.extack = extack;
5741+ cls_flow->command = cmd;
5742+ cls_flow->cookie = (unsigned long)tuple;
5743+}
5744+
5745+static int nf_flow_offload_tuple(struct nf_flowtable *flowtable,
5746+ struct flow_offload *flow,
5747+ struct nf_flow_rule *flow_rule,
5748+ enum flow_offload_tuple_dir dir,
5749+ int priority, int cmd,
5750+ struct flow_stats *stats,
5751+ struct list_head *block_cb_list)
5752+{
5753+ struct flow_cls_offload cls_flow = {};
5754+ struct flow_block_cb *block_cb;
5755+ struct netlink_ext_ack extack;
5756+ __be16 proto = ETH_P_ALL;
5757+ int err, i = 0;
5758+
5759+ nf_flow_offload_init(&cls_flow, proto, priority, cmd,
5760+ &flow->tuplehash[dir].tuple, &extack);
5761+ if (cmd == FLOW_CLS_REPLACE)
5762+ cls_flow.rule = flow_rule->rule;
5763+
5764+ down_read(&flowtable->flow_block_lock);
5765+ list_for_each_entry(block_cb, block_cb_list, list) {
5766+ err = block_cb->cb(TC_SETUP_CLSFLOWER, &cls_flow,
5767+ block_cb->cb_priv);
5768+ if (err < 0)
5769+ continue;
5770+
5771+ i++;
5772+ }
5773+ up_read(&flowtable->flow_block_lock);
5774+
5775+ if (cmd == FLOW_CLS_STATS)
5776+ memcpy(stats, &cls_flow.stats, sizeof(*stats));
5777+
5778+ return i;
5779+}
5780+
5781+static int flow_offload_tuple_add(struct flow_offload_work *offload,
5782+ struct nf_flow_rule *flow_rule,
5783+ enum flow_offload_tuple_dir dir)
5784+{
5785+ return nf_flow_offload_tuple(offload->flowtable, offload->flow,
5786+ flow_rule, dir, offload->priority,
5787+ FLOW_CLS_REPLACE, NULL,
5788+ &offload->flowtable->flow_block.cb_list);
5789+}
5790+
5791+static void flow_offload_tuple_del(struct flow_offload_work *offload,
5792+ enum flow_offload_tuple_dir dir)
5793+{
5794+ nf_flow_offload_tuple(offload->flowtable, offload->flow, NULL, dir,
5795+ offload->priority, FLOW_CLS_DESTROY, NULL,
5796+ &offload->flowtable->flow_block.cb_list);
5797+}
5798+
5799+static int flow_offload_rule_add(struct flow_offload_work *offload,
5800+ struct nf_flow_rule *flow_rule[])
5801+{
5802+ int ok_count = 0;
5803+
5804+ ok_count += flow_offload_tuple_add(offload, flow_rule[0],
5805+ FLOW_OFFLOAD_DIR_ORIGINAL);
5806+ ok_count += flow_offload_tuple_add(offload, flow_rule[1],
5807+ FLOW_OFFLOAD_DIR_REPLY);
5808+ if (ok_count == 0)
5809+ return -ENOENT;
5810+
5811+ return 0;
5812+}
5813+
5814+static void flow_offload_work_add(struct flow_offload_work *offload)
5815+{
5816+ struct nf_flow_rule *flow_rule[FLOW_OFFLOAD_DIR_MAX];
5817+ int err;
5818+
5819+ err = nf_flow_offload_alloc(offload, flow_rule);
5820+ if (err < 0)
5821+ return;
5822+
5823+ err = flow_offload_rule_add(offload, flow_rule);
5824+ if (err < 0)
5825+ goto out;
5826+
5827+ set_bit(IPS_HW_OFFLOAD_BIT, &offload->flow->ct->status);
5828+
5829+out:
5830+ nf_flow_offload_destroy(flow_rule);
5831+}
5832+
5833+static void flow_offload_work_del(struct flow_offload_work *offload)
5834+{
5835+ clear_bit(IPS_HW_OFFLOAD_BIT, &offload->flow->ct->status);
5836+ flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_ORIGINAL);
5837+ flow_offload_tuple_del(offload, FLOW_OFFLOAD_DIR_REPLY);
5838+ set_bit(NF_FLOW_HW_DEAD, &offload->flow->flags);
5839+}
5840+
5841+static void flow_offload_tuple_stats(struct flow_offload_work *offload,
5842+ enum flow_offload_tuple_dir dir,
5843+ struct flow_stats *stats)
5844+{
5845+ nf_flow_offload_tuple(offload->flowtable, offload->flow, NULL, dir,
5846+ offload->priority, FLOW_CLS_STATS, stats,
5847+ &offload->flowtable->flow_block.cb_list);
5848+}
5849+
5850+static void flow_offload_work_stats(struct flow_offload_work *offload)
5851+{
5852+ struct flow_stats stats[FLOW_OFFLOAD_DIR_MAX] = {};
5853+ u64 lastused;
5854+
5855+ flow_offload_tuple_stats(offload, FLOW_OFFLOAD_DIR_ORIGINAL, &stats[0]);
5856+ flow_offload_tuple_stats(offload, FLOW_OFFLOAD_DIR_REPLY, &stats[1]);
5857+
5858+ lastused = max_t(u64, stats[0].lastused, stats[1].lastused);
5859+ offload->flow->timeout = max_t(u64, offload->flow->timeout,
5860+ lastused + flow_offload_get_timeout(offload->flow));
5861+
5862+ if (offload->flowtable->flags & NF_FLOWTABLE_COUNTER) {
5863+ if (stats[0].pkts)
5864+ nf_ct_acct_add(offload->flow->ct,
5865+ FLOW_OFFLOAD_DIR_ORIGINAL,
5866+ stats[0].pkts, stats[0].bytes);
5867+ if (stats[1].pkts)
5868+ nf_ct_acct_add(offload->flow->ct,
5869+ FLOW_OFFLOAD_DIR_REPLY,
5870+ stats[1].pkts, stats[1].bytes);
5871+ }
5872+}
5873+
5874+static void flow_offload_work_handler(struct work_struct *work)
5875+{
5876+ struct flow_offload_work *offload;
5877+
5878+ offload = container_of(work, struct flow_offload_work, work);
5879+ switch (offload->cmd) {
5880+ case FLOW_CLS_REPLACE:
5881+ flow_offload_work_add(offload);
5882+ break;
5883+ case FLOW_CLS_DESTROY:
5884+ flow_offload_work_del(offload);
5885+ break;
5886+ case FLOW_CLS_STATS:
5887+ flow_offload_work_stats(offload);
5888+ break;
5889+ default:
5890+ WARN_ON_ONCE(1);
5891+ }
5892+
5893+ clear_bit(NF_FLOW_HW_PENDING, &offload->flow->flags);
5894+ kfree(offload);
5895+}
5896+
5897+static void flow_offload_queue_work(struct flow_offload_work *offload)
5898+{
5899+ if (offload->cmd == FLOW_CLS_REPLACE)
5900+ queue_work(nf_flow_offload_add_wq, &offload->work);
5901+ else if (offload->cmd == FLOW_CLS_DESTROY)
5902+ queue_work(nf_flow_offload_del_wq, &offload->work);
5903+ else
5904+ queue_work(nf_flow_offload_stats_wq, &offload->work);
5905+}
5906+
5907+static struct flow_offload_work *
5908+nf_flow_offload_work_alloc(struct nf_flowtable *flowtable,
5909+ struct flow_offload *flow, unsigned int cmd)
5910+{
5911+ struct flow_offload_work *offload;
5912+
5913+ if (test_and_set_bit(NF_FLOW_HW_PENDING, &flow->flags))
5914+ return NULL;
5915+
5916+ offload = kmalloc(sizeof(struct flow_offload_work), GFP_ATOMIC);
5917+ if (!offload) {
5918+ clear_bit(NF_FLOW_HW_PENDING, &flow->flags);
5919+ return NULL;
5920+ }
5921+
5922+ offload->cmd = cmd;
5923+ offload->flow = flow;
5924+ offload->priority = flowtable->priority;
5925+ offload->flowtable = flowtable;
5926+ INIT_WORK(&offload->work, flow_offload_work_handler);
5927+
5928+ return offload;
5929+}
5930+
5931+
5932+void nf_flow_offload_add(struct nf_flowtable *flowtable,
5933+ struct flow_offload *flow)
5934+{
5935+ struct flow_offload_work *offload;
5936+
5937+ offload = nf_flow_offload_work_alloc(flowtable, flow, FLOW_CLS_REPLACE);
5938+ if (!offload)
5939+ return;
5940+
5941+ flow_offload_queue_work(offload);
5942+}
5943+
5944+void nf_flow_offload_del(struct nf_flowtable *flowtable,
5945+ struct flow_offload *flow)
5946+{
5947+ struct flow_offload_work *offload;
5948+
5949+ offload = nf_flow_offload_work_alloc(flowtable, flow, FLOW_CLS_DESTROY);
5950+ if (!offload)
5951+ return;
5952+
5953+ set_bit(NF_FLOW_HW_DYING, &flow->flags);
5954+ flow_offload_queue_work(offload);
5955+}
5956+
5957+void nf_flow_offload_stats(struct nf_flowtable *flowtable,
5958+ struct flow_offload *flow)
5959+{
5960+ struct flow_offload_work *offload;
5961+ __s32 delta;
5962+
5963+ delta = nf_flow_timeout_delta(flow->timeout);
5964+ if ((delta >= (9 * flow_offload_get_timeout(flow)) / 10))
5965+ return;
5966+
5967+ offload = nf_flow_offload_work_alloc(flowtable, flow, FLOW_CLS_STATS);
5968+ if (!offload)
5969+ return;
5970+
5971+ flow_offload_queue_work(offload);
5972+}
5973+
5974+void nf_flow_table_offload_flush(struct nf_flowtable *flowtable)
5975+{
5976+ if (nf_flowtable_hw_offload(flowtable)) {
5977+ flush_workqueue(nf_flow_offload_add_wq);
5978+ flush_workqueue(nf_flow_offload_del_wq);
5979+ flush_workqueue(nf_flow_offload_stats_wq);
5980+ }
5981+}
5982+
5983+static int nf_flow_table_block_setup(struct nf_flowtable *flowtable,
5984+ struct flow_block_offload *bo,
5985+ enum flow_block_command cmd)
5986+{
5987+ struct flow_block_cb *block_cb, *next;
5988+ int err = 0;
5989+
5990+ switch (cmd) {
5991+ case FLOW_BLOCK_BIND:
5992+ list_splice(&bo->cb_list, &flowtable->flow_block.cb_list);
5993+ break;
5994+ case FLOW_BLOCK_UNBIND:
5995+ list_for_each_entry_safe(block_cb, next, &bo->cb_list, list) {
5996+ list_del(&block_cb->list);
5997+ flow_block_cb_free(block_cb);
5998+ }
5999+ break;
6000+ default:
6001+ WARN_ON_ONCE(1);
6002+ err = -EOPNOTSUPP;
6003+ }
6004+
6005+ return err;
6006+}
6007+
6008+static void nf_flow_table_block_offload_init(struct flow_block_offload *bo,
6009+ struct net *net,
6010+ enum flow_block_command cmd,
6011+ struct nf_flowtable *flowtable,
6012+ struct netlink_ext_ack *extack)
6013+{
6014+ memset(bo, 0, sizeof(*bo));
6015+ bo->net = net;
6016+ bo->block = &flowtable->flow_block;
6017+ bo->command = cmd;
6018+ bo->binder_type = FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS;
6019+ bo->extack = extack;
6020+ INIT_LIST_HEAD(&bo->cb_list);
6021+}
6022+
6023+static int nf_flow_table_indr_offload_cmd(struct flow_block_offload *bo,
6024+ struct nf_flowtable *flowtable,
6025+ struct net_device *dev,
6026+ enum flow_block_command cmd,
6027+ struct netlink_ext_ack *extack)
6028+{
6029+ nf_flow_table_block_offload_init(bo, dev_net(dev), cmd, flowtable,
6030+ extack);
6031+ flow_indr_block_call(dev, bo, cmd);
6032+
6033+ if (list_empty(&bo->cb_list))
6034+ return -EOPNOTSUPP;
6035+
6036+ return 0;
6037+}
6038+
6039+static int nf_flow_table_offload_cmd(struct flow_block_offload *bo,
6040+ struct nf_flowtable *flowtable,
6041+ struct net_device *dev,
6042+ enum flow_block_command cmd,
6043+ struct netlink_ext_ack *extack)
6044+{
6045+ int err;
6046+
6047+ nf_flow_table_block_offload_init(bo, dev_net(dev), cmd, flowtable,
6048+ extack);
6049+ err = dev->netdev_ops->ndo_setup_tc(dev, TC_SETUP_FT, bo);
6050+ if (err < 0)
6051+ return err;
6052+
6053+ return 0;
6054+}
6055+
6056+int nf_flow_table_offload_setup(struct nf_flowtable *flowtable,
6057+ struct net_device *dev,
6058+ enum flow_block_command cmd)
6059+{
6060+ struct netlink_ext_ack extack = {};
6061+ struct flow_block_offload bo;
6062+ int err;
6063+
6064+ if (!nf_flowtable_hw_offload(flowtable))
6065+ return 0;
6066+
6067+ if (dev->netdev_ops->ndo_setup_tc)
6068+ err = nf_flow_table_offload_cmd(&bo, flowtable, dev, cmd,
6069+ &extack);
6070+ else
6071+ err = nf_flow_table_indr_offload_cmd(&bo, flowtable, dev, cmd,
6072+ &extack);
6073+ if (err < 0)
6074+ return err;
6075+
6076+ return nf_flow_table_block_setup(flowtable, &bo, cmd);
6077+}
6078+EXPORT_SYMBOL_GPL(nf_flow_table_offload_setup);
6079+
6080+int nf_flow_table_offload_init(void)
6081+{
6082+ nf_flow_offload_add_wq = alloc_workqueue("nf_ft_offload_add",
6083+ WQ_UNBOUND | WQ_SYSFS, 0);
6084+ if (!nf_flow_offload_add_wq)
6085+ return -ENOMEM;
6086+
6087+ nf_flow_offload_del_wq = alloc_workqueue("nf_ft_offload_del",
6088+ WQ_UNBOUND | WQ_SYSFS, 0);
6089+ if (!nf_flow_offload_del_wq)
6090+ goto err_del_wq;
6091+
6092+ nf_flow_offload_stats_wq = alloc_workqueue("nf_ft_offload_stats",
6093+ WQ_UNBOUND | WQ_SYSFS, 0);
6094+ if (!nf_flow_offload_stats_wq)
6095+ goto err_stats_wq;
6096+
6097+ return 0;
6098+
6099+err_stats_wq:
6100+ destroy_workqueue(nf_flow_offload_del_wq);
6101+err_del_wq:
6102+ destroy_workqueue(nf_flow_offload_add_wq);
6103+ return -ENOMEM;
6104+}
6105+
6106+void nf_flow_table_offload_exit(void)
6107+{
6108+ destroy_workqueue(nf_flow_offload_add_wq);
6109+ destroy_workqueue(nf_flow_offload_del_wq);
6110+ destroy_workqueue(nf_flow_offload_stats_wq);
6111+}
6112diff --git a/net/netfilter/xt_FLOWOFFLOAD.c b/net/netfilter/xt_FLOWOFFLOAD.c
6113new file mode 100644
6114index 000000000..ae1eb2656
6115--- /dev/null
6116+++ b/net/netfilter/xt_FLOWOFFLOAD.c
6117@@ -0,0 +1,719 @@
6118+/*
6119+ * Copyright (C) 2018-2021 Felix Fietkau <nbd@nbd.name>
6120+ *
6121+ * This program is free software; you can redistribute it and/or modify
6122+ * it under the terms of the GNU General Public License version 2 as
6123+ * published by the Free Software Foundation.
6124+ */
6125+#include <linux/module.h>
6126+#include <linux/init.h>
6127+#include <linux/netfilter.h>
6128+#include <linux/netfilter/xt_FLOWOFFLOAD.h>
6129+#include <linux/if_vlan.h>
6130+#include <net/ip.h>
6131+#include <net/netfilter/nf_conntrack.h>
6132+#include <net/netfilter/nf_conntrack_extend.h>
6133+#include <net/netfilter/nf_conntrack_helper.h>
6134+#include <net/netfilter/nf_flow_table.h>
6135+
6136+struct xt_flowoffload_hook {
6137+ struct hlist_node list;
6138+ struct nf_hook_ops ops;
6139+ struct net *net;
6140+ bool registered;
6141+ bool used;
6142+};
6143+
6144+struct xt_flowoffload_table {
6145+ struct nf_flowtable ft;
6146+ struct hlist_head hooks;
6147+ struct delayed_work work;
6148+};
6149+
6150+struct nf_forward_info {
6151+ const struct net_device *indev;
6152+ const struct net_device *outdev;
6153+ const struct net_device *hw_outdev;
6154+ struct id {
6155+ __u16 id;
6156+ __be16 proto;
6157+ } encap[NF_FLOW_TABLE_ENCAP_MAX];
6158+ u8 num_encaps;
6159+ u8 ingress_vlans;
6160+ u8 h_source[ETH_ALEN];
6161+ u8 h_dest[ETH_ALEN];
6162+ enum flow_offload_xmit_type xmit_type;
6163+};
6164+
6165+static DEFINE_SPINLOCK(hooks_lock);
6166+
6167+struct xt_flowoffload_table flowtable[2];
6168+
6169+static unsigned int
6170+xt_flowoffload_net_hook(void *priv, struct sk_buff *skb,
6171+ const struct nf_hook_state *state)
6172+{
6173+ struct vlan_ethhdr *veth;
6174+ __be16 proto;
6175+
6176+ switch (skb->protocol) {
6177+ case htons(ETH_P_8021Q):
6178+ veth = (struct vlan_ethhdr *)skb_mac_header(skb);
6179+ proto = veth->h_vlan_encapsulated_proto;
6180+ break;
6181+ case htons(ETH_P_PPP_SES):
6182+ proto = nf_flow_pppoe_proto(skb);
6183+ break;
6184+ default:
6185+ proto = skb->protocol;
6186+ break;
6187+ }
6188+
6189+ switch (proto) {
6190+ case htons(ETH_P_IP):
6191+ return nf_flow_offload_ip_hook(priv, skb, state);
6192+ case htons(ETH_P_IPV6):
6193+ return nf_flow_offload_ipv6_hook(priv, skb, state);
6194+ }
6195+
6196+ return NF_ACCEPT;
6197+}
6198+
6199+static int
6200+xt_flowoffload_create_hook(struct xt_flowoffload_table *table,
6201+ struct net_device *dev)
6202+{
6203+ struct xt_flowoffload_hook *hook;
6204+ struct nf_hook_ops *ops;
6205+
6206+ hook = kzalloc(sizeof(*hook), GFP_ATOMIC);
6207+ if (!hook)
6208+ return -ENOMEM;
6209+
6210+ ops = &hook->ops;
6211+ ops->pf = NFPROTO_NETDEV;
6212+ ops->hooknum = NF_NETDEV_INGRESS;
6213+ ops->priority = 10;
6214+ ops->priv = &table->ft;
6215+ ops->hook = xt_flowoffload_net_hook;
6216+ ops->dev = dev;
6217+
6218+ hlist_add_head(&hook->list, &table->hooks);
6219+ mod_delayed_work(system_power_efficient_wq, &table->work, 0);
6220+
6221+ return 0;
6222+}
6223+
6224+static struct xt_flowoffload_hook *
6225+flow_offload_lookup_hook(struct xt_flowoffload_table *table,
6226+ struct net_device *dev)
6227+{
6228+ struct xt_flowoffload_hook *hook;
6229+
6230+ hlist_for_each_entry(hook, &table->hooks, list) {
6231+ if (hook->ops.dev == dev)
6232+ return hook;
6233+ }
6234+
6235+ return NULL;
6236+}
6237+
6238+static void
6239+xt_flowoffload_check_device(struct xt_flowoffload_table *table,
6240+ struct net_device *dev)
6241+{
6242+ struct xt_flowoffload_hook *hook;
6243+
6244+ if (!dev)
6245+ return;
6246+
6247+ spin_lock_bh(&hooks_lock);
6248+ hook = flow_offload_lookup_hook(table, dev);
6249+ if (hook)
6250+ hook->used = true;
6251+ else
6252+ xt_flowoffload_create_hook(table, dev);
6253+ spin_unlock_bh(&hooks_lock);
6254+}
6255+
6256+static void
6257+xt_flowoffload_register_hooks(struct xt_flowoffload_table *table)
6258+{
6259+ struct xt_flowoffload_hook *hook;
6260+
6261+restart:
6262+ hlist_for_each_entry(hook, &table->hooks, list) {
6263+ if (hook->registered)
6264+ continue;
6265+
6266+ hook->registered = true;
6267+ hook->net = dev_net(hook->ops.dev);
6268+ spin_unlock_bh(&hooks_lock);
6269+ nf_register_net_hook(hook->net, &hook->ops);
6270+ if (table->ft.flags & NF_FLOWTABLE_HW_OFFLOAD)
6271+ table->ft.type->setup(&table->ft, hook->ops.dev,
6272+ FLOW_BLOCK_BIND);
6273+ spin_lock_bh(&hooks_lock);
6274+ goto restart;
6275+ }
6276+
6277+}
6278+
6279+static bool
6280+xt_flowoffload_cleanup_hooks(struct xt_flowoffload_table *table)
6281+{
6282+ struct xt_flowoffload_hook *hook;
6283+ bool active = false;
6284+
6285+restart:
6286+ spin_lock_bh(&hooks_lock);
6287+ hlist_for_each_entry(hook, &table->hooks, list) {
6288+ if (hook->used || !hook->registered) {
6289+ active = true;
6290+ continue;
6291+ }
6292+
6293+ hlist_del(&hook->list);
6294+ spin_unlock_bh(&hooks_lock);
6295+ if (table->ft.flags & NF_FLOWTABLE_HW_OFFLOAD)
6296+ table->ft.type->setup(&table->ft, hook->ops.dev,
6297+ FLOW_BLOCK_UNBIND);
6298+ nf_unregister_net_hook(hook->net, &hook->ops);
6299+ kfree(hook);
6300+ goto restart;
6301+ }
6302+ spin_unlock_bh(&hooks_lock);
6303+
6304+ return active;
6305+}
6306+
6307+static void
6308+xt_flowoffload_check_hook(struct flow_offload *flow, void *data)
6309+{
6310+ struct xt_flowoffload_table *table = data;
6311+ struct flow_offload_tuple *tuple0 = &flow->tuplehash[0].tuple;
6312+ struct flow_offload_tuple *tuple1 = &flow->tuplehash[1].tuple;
6313+ struct xt_flowoffload_hook *hook;
6314+
6315+ spin_lock_bh(&hooks_lock);
6316+ hlist_for_each_entry(hook, &table->hooks, list) {
6317+ if (hook->ops.dev->ifindex != tuple0->iifidx &&
6318+ hook->ops.dev->ifindex != tuple1->iifidx)
6319+ continue;
6320+
6321+ hook->used = true;
6322+ }
6323+ spin_unlock_bh(&hooks_lock);
6324+}
6325+
6326+static void
6327+xt_flowoffload_hook_work(struct work_struct *work)
6328+{
6329+ struct xt_flowoffload_table *table;
6330+ struct xt_flowoffload_hook *hook;
6331+ int err;
6332+
6333+ table = container_of(work, struct xt_flowoffload_table, work.work);
6334+
6335+ spin_lock_bh(&hooks_lock);
6336+ xt_flowoffload_register_hooks(table);
6337+ hlist_for_each_entry(hook, &table->hooks, list)
6338+ hook->used = false;
6339+ spin_unlock_bh(&hooks_lock);
6340+
6341+ err = nf_flow_table_iterate(&table->ft, xt_flowoffload_check_hook,
6342+ table);
6343+ if (err && err != -EAGAIN)
6344+ goto out;
6345+
6346+ if (!xt_flowoffload_cleanup_hooks(table))
6347+ return;
6348+
6349+out:
6350+ queue_delayed_work(system_power_efficient_wq, &table->work, HZ);
6351+}
6352+
6353+static bool
6354+xt_flowoffload_skip(struct sk_buff *skb, int family)
6355+{
6356+ if (skb_sec_path(skb))
6357+ return true;
6358+
6359+ if (family == NFPROTO_IPV4) {
6360+ const struct ip_options *opt = &(IPCB(skb)->opt);
6361+
6362+ if (unlikely(opt->optlen))
6363+ return true;
6364+ }
6365+
6366+ return false;
6367+}
6368+
6369+static enum flow_offload_xmit_type nf_xmit_type(struct dst_entry *dst)
6370+{
6371+ if (dst_xfrm(dst))
6372+ return FLOW_OFFLOAD_XMIT_XFRM;
6373+
6374+ return FLOW_OFFLOAD_XMIT_NEIGH;
6375+}
6376+
6377+static void nf_default_forward_path(struct nf_flow_route *route,
6378+ struct dst_entry *dst_cache,
6379+ enum ip_conntrack_dir dir,
6380+ struct net_device **dev)
6381+{
6382+ route->tuple[!dir].in.ifindex = dst_cache->dev->ifindex;
6383+ route->tuple[dir].dst = dst_cache;
6384+ route->tuple[dir].xmit_type = nf_xmit_type(dst_cache);
6385+}
6386+
6387+static bool nf_is_valid_ether_device(const struct net_device *dev)
6388+{
6389+ if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
6390+ dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
6391+ return false;
6392+
6393+ return true;
6394+}
6395+
6396+static void nf_dev_path_info(const struct net_device_path_stack *stack,
6397+ struct nf_forward_info *info,
6398+ unsigned char *ha)
6399+{
6400+ const struct net_device_path *path;
6401+ int i;
6402+
6403+ memcpy(info->h_dest, ha, ETH_ALEN);
6404+
6405+ for (i = 0; i < stack->num_paths; i++) {
6406+ path = &stack->path[i];
6407+
6408+ info->indev = path->dev;
6409+
6410+ switch (path->type) {
6411+ case DEV_PATH_ETHERNET:
6412+ case DEV_PATH_DSA:
6413+ case DEV_PATH_VLAN:
6414+ case DEV_PATH_PPPOE:
6415+ if (is_zero_ether_addr(info->h_source))
6416+ memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
6417+
6418+ if (path->type == DEV_PATH_ETHERNET)
6419+ break;
6420+ if (path->type == DEV_PATH_DSA) {
6421+ i = stack->num_paths;
6422+ break;
6423+ }
6424+
6425+ /* DEV_PATH_VLAN and DEV_PATH_PPPOE */
6426+ if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
6427+ info->indev = NULL;
6428+ break;
6429+ }
6430+ if (!info->outdev)
6431+ info->outdev = path->dev;
6432+ info->encap[info->num_encaps].id = path->encap.id;
6433+ info->encap[info->num_encaps].proto = path->encap.proto;
6434+ info->num_encaps++;
6435+ if (path->type == DEV_PATH_PPPOE)
6436+ memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
6437+ break;
6438+ case DEV_PATH_BRIDGE:
6439+ if (is_zero_ether_addr(info->h_source))
6440+ memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
6441+
6442+ switch (path->bridge.vlan_mode) {
6443+ case DEV_PATH_BR_VLAN_UNTAG_HW:
6444+ info->ingress_vlans |= BIT(info->num_encaps - 1);
6445+ break;
6446+ case DEV_PATH_BR_VLAN_TAG:
6447+ info->encap[info->num_encaps].id = path->bridge.vlan_id;
6448+ info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
6449+ info->num_encaps++;
6450+ break;
6451+ case DEV_PATH_BR_VLAN_UNTAG:
6452+ info->num_encaps--;
6453+ break;
6454+ case DEV_PATH_BR_VLAN_KEEP:
6455+ break;
6456+ }
6457+ break;
6458+ default:
6459+ break;
6460+ }
6461+ }
6462+ if (!info->outdev)
6463+ info->outdev = info->indev;
6464+
6465+ info->hw_outdev = info->indev;
6466+
6467+ if (nf_is_valid_ether_device(info->indev))
6468+ info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
6469+}
6470+
6471+static int nf_dev_fill_forward_path(const struct nf_flow_route *route,
6472+ const struct dst_entry *dst_cache,
6473+ const struct nf_conn *ct,
6474+ enum ip_conntrack_dir dir, u8 *ha,
6475+ struct net_device_path_stack *stack)
6476+{
6477+ const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
6478+ struct net_device *dev = dst_cache->dev;
6479+ struct neighbour *n;
6480+ u8 nud_state;
6481+
6482+ if (!nf_is_valid_ether_device(dev))
6483+ goto out;
6484+
6485+ n = dst_neigh_lookup(dst_cache, daddr);
6486+ if (!n)
6487+ return -1;
6488+
6489+ read_lock_bh(&n->lock);
6490+ nud_state = n->nud_state;
6491+ ether_addr_copy(ha, n->ha);
6492+ read_unlock_bh(&n->lock);
6493+ neigh_release(n);
6494+
6495+ if (!(nud_state & NUD_VALID))
6496+ return -1;
6497+
6498+out:
6499+ return dev_fill_forward_path(dev, ha, stack);
6500+}
6501+
6502+static int nf_dev_forward_path(struct nf_flow_route *route,
6503+ const struct nf_conn *ct,
6504+ enum ip_conntrack_dir dir,
6505+ struct net_device **devs)
6506+{
6507+ const struct dst_entry *dst = route->tuple[dir].dst;
6508+ struct net_device_path_stack stack;
6509+ struct nf_forward_info info = {};
6510+ unsigned char ha[ETH_ALEN];
6511+ int i;
6512+
6513+ if (nf_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
6514+ nf_dev_path_info(&stack, &info, ha);
6515+
6516+ devs[!dir] = (struct net_device *)info.indev;
6517+ if (!info.indev)
6518+ return -1;
6519+
6520+ route->tuple[!dir].in.ifindex = info.indev->ifindex;
6521+ for (i = 0; i < info.num_encaps; i++) {
6522+ route->tuple[!dir].in.encap[i].id = info.encap[i].id;
6523+ route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
6524+ }
6525+ route->tuple[!dir].in.num_encaps = info.num_encaps;
6526+ route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
6527+
6528+ if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
6529+ memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
6530+ memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
6531+ route->tuple[dir].out.ifindex = info.outdev->ifindex;
6532+ route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
6533+ route->tuple[dir].xmit_type = info.xmit_type;
6534+ }
6535+
6536+ return 0;
6537+}
6538+
6539+static int
6540+xt_flowoffload_route_dir(struct nf_flow_route *route, const struct nf_conn *ct,
6541+ enum ip_conntrack_dir dir,
6542+ const struct xt_action_param *par, int ifindex,
6543+ struct net_device **devs)
6544+{
6545+ struct dst_entry *dst = NULL;
6546+ struct flowi fl;
6547+
6548+ memset(&fl, 0, sizeof(fl));
6549+ switch (xt_family(par)) {
6550+ case NFPROTO_IPV4:
6551+ fl.u.ip4.daddr = ct->tuplehash[!dir].tuple.src.u3.ip;
6552+ fl.u.ip4.flowi4_oif = ifindex;
6553+ break;
6554+ case NFPROTO_IPV6:
6555+ fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.dst.u3.in6;
6556+ fl.u.ip6.daddr = ct->tuplehash[!dir].tuple.src.u3.in6;
6557+ fl.u.ip6.flowi6_oif = ifindex;
6558+ break;
6559+ }
6560+
6561+ nf_route(xt_net(par), &dst, &fl, false, xt_family(par));
6562+ if (!dst)
6563+ return -ENOENT;
6564+
6565+ nf_default_forward_path(route, dst, dir, devs);
6566+
6567+ return 0;
6568+}
6569+
6570+static int
6571+xt_flowoffload_route(struct sk_buff *skb, const struct nf_conn *ct,
6572+ const struct xt_action_param *par,
6573+ struct nf_flow_route *route, enum ip_conntrack_dir dir,
6574+ struct net_device **devs)
6575+{
6576+ int ret;
6577+
6578+ ret = xt_flowoffload_route_dir(route, ct, dir, par,
6579+ devs[dir]->ifindex,
6580+ devs);
6581+ if (ret)
6582+ return ret;
6583+
6584+ ret = xt_flowoffload_route_dir(route, ct, !dir, par,
6585+ devs[!dir]->ifindex,
6586+ devs);
6587+ if (ret)
6588+ return ret;
6589+
6590+ if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
6591+ route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
6592+ if (nf_dev_forward_path(route, ct, dir, devs))
6593+ return -1;
6594+ if (nf_dev_forward_path(route, ct, !dir, devs))
6595+ return -1;
6596+ }
6597+
6598+ return 0;
6599+}
6600+
6601+static unsigned int
6602+flowoffload_tg(struct sk_buff *skb, const struct xt_action_param *par)
6603+{
6604+ struct xt_flowoffload_table *table;
6605+ const struct xt_flowoffload_target_info *info = par->targinfo;
6606+ struct tcphdr _tcph, *tcph = NULL;
6607+ enum ip_conntrack_info ctinfo;
6608+ enum ip_conntrack_dir dir;
6609+ struct nf_flow_route route = {};
6610+ struct flow_offload *flow = NULL;
6611+ struct net_device *devs[2] = {};
6612+ struct nf_conn *ct;
6613+ struct net *net;
6614+
6615+ if (xt_flowoffload_skip(skb, xt_family(par)))
6616+ return XT_CONTINUE;
6617+
6618+ ct = nf_ct_get(skb, &ctinfo);
6619+ if (ct == NULL)
6620+ return XT_CONTINUE;
6621+
6622+ switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
6623+ case IPPROTO_TCP:
6624+ if (ct->proto.tcp.state != TCP_CONNTRACK_ESTABLISHED)
6625+ return XT_CONTINUE;
6626+
6627+ tcph = skb_header_pointer(skb, par->thoff,
6628+ sizeof(_tcph), &_tcph);
6629+ if (unlikely(!tcph || tcph->fin || tcph->rst))
6630+ return XT_CONTINUE;
6631+ break;
6632+ case IPPROTO_UDP:
6633+ break;
6634+ default:
6635+ return XT_CONTINUE;
6636+ }
6637+
6638+ if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
6639+ ct->status & IPS_SEQ_ADJUST)
6640+ return XT_CONTINUE;
6641+
6642+ if (!nf_ct_is_confirmed(ct))
6643+ return XT_CONTINUE;
6644+
6645+ devs[dir] = xt_out(par);
6646+ devs[!dir] = xt_in(par);
6647+
6648+ if (!devs[dir] || !devs[!dir])
6649+ return XT_CONTINUE;
6650+
6651+ if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
6652+ return XT_CONTINUE;
6653+
6654+ dir = CTINFO2DIR(ctinfo);
6655+
6656+ if (xt_flowoffload_route(skb, ct, par, &route, dir, devs) < 0)
6657+ goto err_flow_route;
6658+
6659+ flow = flow_offload_alloc(ct);
6660+ if (!flow)
6661+ goto err_flow_alloc;
6662+
6663+ if (flow_offload_route_init(flow, &route) < 0)
6664+ goto err_flow_add;
6665+
6666+ if (tcph) {
6667+ ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
6668+ ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
6669+ }
6670+
6671+ table = &flowtable[!!(info->flags & XT_FLOWOFFLOAD_HW)];
6672+
6673+ net = read_pnet(&table->ft.net);
6674+ if (!net)
6675+ write_pnet(&table->ft.net, xt_net(par));
6676+
6677+ if (flow_offload_add(&table->ft, flow) < 0)
6678+ goto err_flow_add;
6679+
6680+ xt_flowoffload_check_device(table, devs[0]);
6681+ xt_flowoffload_check_device(table, devs[1]);
6682+
6683+ dst_release(route.tuple[!dir].dst);
6684+
6685+ return XT_CONTINUE;
6686+
6687+err_flow_add:
6688+ flow_offload_free(flow);
6689+err_flow_alloc:
6690+ dst_release(route.tuple[!dir].dst);
6691+err_flow_route:
6692+ clear_bit(IPS_OFFLOAD_BIT, &ct->status);
6693+
6694+ return XT_CONTINUE;
6695+}
6696+
6697+static int flowoffload_chk(const struct xt_tgchk_param *par)
6698+{
6699+ struct xt_flowoffload_target_info *info = par->targinfo;
6700+
6701+ if (info->flags & ~XT_FLOWOFFLOAD_MASK)
6702+ return -EINVAL;
6703+
6704+ return 0;
6705+}
6706+
6707+static struct xt_target offload_tg_reg __read_mostly = {
6708+ .family = NFPROTO_UNSPEC,
6709+ .name = "FLOWOFFLOAD",
6710+ .revision = 0,
6711+ .targetsize = sizeof(struct xt_flowoffload_target_info),
6712+ .usersize = sizeof(struct xt_flowoffload_target_info),
6713+ .checkentry = flowoffload_chk,
6714+ .target = flowoffload_tg,
6715+ .me = THIS_MODULE,
6716+};
6717+
6718+static int flow_offload_netdev_event(struct notifier_block *this,
6719+ unsigned long event, void *ptr)
6720+{
6721+ struct xt_flowoffload_hook *hook0, *hook1;
6722+ struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6723+
6724+ if (event != NETDEV_UNREGISTER)
6725+ return NOTIFY_DONE;
6726+
6727+ spin_lock_bh(&hooks_lock);
6728+ hook0 = flow_offload_lookup_hook(&flowtable[0], dev);
6729+ if (hook0)
6730+ hlist_del(&hook0->list);
6731+
6732+ hook1 = flow_offload_lookup_hook(&flowtable[1], dev);
6733+ if (hook1)
6734+ hlist_del(&hook1->list);
6735+ spin_unlock_bh(&hooks_lock);
6736+
6737+ if (hook0) {
6738+ nf_unregister_net_hook(hook0->net, &hook0->ops);
6739+ kfree(hook0);
6740+ }
6741+
6742+ if (hook1) {
6743+ nf_unregister_net_hook(hook1->net, &hook1->ops);
6744+ kfree(hook1);
6745+ }
6746+
6747+ nf_flow_table_cleanup(dev);
6748+
6749+ return NOTIFY_DONE;
6750+}
6751+
6752+static struct notifier_block flow_offload_netdev_notifier = {
6753+ .notifier_call = flow_offload_netdev_event,
6754+};
6755+
6756+static int nf_flow_rule_route_inet(struct net *net,
6757+ const struct flow_offload *flow,
6758+ enum flow_offload_tuple_dir dir,
6759+ struct nf_flow_rule *flow_rule)
6760+{
6761+ const struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
6762+ int err;
6763+
6764+ switch (flow_tuple->l3proto) {
6765+ case NFPROTO_IPV4:
6766+ err = nf_flow_rule_route_ipv4(net, flow, dir, flow_rule);
6767+ break;
6768+ case NFPROTO_IPV6:
6769+ err = nf_flow_rule_route_ipv6(net, flow, dir, flow_rule);
6770+ break;
6771+ default:
6772+ err = -1;
6773+ break;
6774+ }
6775+
6776+ return err;
6777+}
6778+
6779+static struct nf_flowtable_type flowtable_inet = {
6780+ .family = NFPROTO_INET,
6781+ .init = nf_flow_table_init,
6782+ .setup = nf_flow_table_offload_setup,
6783+ .action = nf_flow_rule_route_inet,
6784+ .free = nf_flow_table_free,
6785+ .hook = xt_flowoffload_net_hook,
6786+ .owner = THIS_MODULE,
6787+};
6788+
6789+static int init_flowtable(struct xt_flowoffload_table *tbl)
6790+{
6791+ INIT_DELAYED_WORK(&tbl->work, xt_flowoffload_hook_work);
6792+ tbl->ft.type = &flowtable_inet;
6793+
6794+ return nf_flow_table_init(&tbl->ft);
6795+}
6796+
6797+static int __init xt_flowoffload_tg_init(void)
6798+{
6799+ int ret;
6800+
6801+ register_netdevice_notifier(&flow_offload_netdev_notifier);
6802+
6803+ ret = init_flowtable(&flowtable[0]);
6804+ if (ret)
6805+ return ret;
6806+
6807+ ret = init_flowtable(&flowtable[1]);
6808+ if (ret)
6809+ goto cleanup;
6810+
6811+ flowtable[1].ft.flags = NF_FLOWTABLE_HW_OFFLOAD;
6812+
6813+ ret = xt_register_target(&offload_tg_reg);
6814+ if (ret)
6815+ goto cleanup2;
6816+
6817+ return 0;
6818+
6819+cleanup2:
6820+ nf_flow_table_free(&flowtable[1].ft);
6821+cleanup:
6822+ nf_flow_table_free(&flowtable[0].ft);
6823+ return ret;
6824+}
6825+
6826+static void __exit xt_flowoffload_tg_exit(void)
6827+{
6828+ xt_unregister_target(&offload_tg_reg);
6829+ unregister_netdevice_notifier(&flow_offload_netdev_notifier);
6830+ nf_flow_table_free(&flowtable[0].ft);
6831+ nf_flow_table_free(&flowtable[1].ft);
6832+}
6833+
6834+MODULE_LICENSE("GPL");
6835+module_init(xt_flowoffload_tg_init);
6836+module_exit(xt_flowoffload_tg_exit);
6837--
68382.18.0
6839