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