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