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