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