developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2023 MediaTek Inc. |
| 4 | * |
| 5 | * Author: Chris.Chou <chris.chou@mediatek.com> |
| 6 | * Ren-Ting Wang <ren-ting.wang@mediatek.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/bitops.h> |
| 10 | |
| 11 | #include <mtk_eth_soc.h> |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 12 | |
| 13 | #if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT) |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 14 | #include <mtk_hnat/hnat.h> |
| 15 | #include <mtk_hnat/nf_hnat_mtk.h> |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 16 | #endif // HNAT |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 17 | |
| 18 | #include <pce/cdrt.h> |
| 19 | #include <pce/cls.h> |
| 20 | #include <pce/netsys.h> |
| 21 | |
| 22 | #include <crypto-eip/ddk/configs/cs_hwpal_ext.h> |
| 23 | #include <crypto-eip/ddk/kit/iotoken/iotoken.h> |
| 24 | #include <crypto-eip/ddk/kit/iotoken/iotoken_ext.h> |
| 25 | |
| 26 | #include "crypto-eip/crypto-eip.h" |
| 27 | #include "crypto-eip/ddk-wrapper.h" |
| 28 | #include "crypto-eip/internal.h" |
| 29 | |
| 30 | static LIST_HEAD(xfrm_params_head); |
| 31 | |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 32 | #if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT) |
| 33 | extern int (*ra_sw_nat_hook_tx)(struct sk_buff *skb, int gmac_no); |
| 34 | |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 35 | static inline bool is_tops_udp_tunnel(struct sk_buff *skb) |
| 36 | { |
| 37 | return skb_hnat_tops(skb) && (ntohs(skb->protocol) == ETH_P_IP) && |
| 38 | (ip_hdr(skb)->protocol == IPPROTO_UDP); |
| 39 | } |
| 40 | |
| 41 | static inline bool is_tcp(struct sk_buff *skb) |
| 42 | { |
| 43 | return (ntohs(skb->protocol) == ETH_P_IP) && (ip_hdr(skb)->protocol == IPPROTO_TCP); |
| 44 | } |
| 45 | |
| 46 | static inline bool is_hnat_rate_reach(struct sk_buff *skb) |
| 47 | { |
| 48 | return is_magic_tag_valid(skb) && (skb_hnat_reason(skb) == HIT_UNBIND_RATE_REACH); |
| 49 | } |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 50 | #endif // HNAT |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 51 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 52 | static void mtk_xfrm_offload_cdrt_tear_down(struct mtk_xfrm_params *xfrm_params) |
| 53 | { |
| 54 | memset(&xfrm_params->cdrt->desc, 0, sizeof(struct cdrt_desc)); |
| 55 | |
| 56 | mtk_pce_cdrt_entry_write(xfrm_params->cdrt); |
| 57 | } |
| 58 | |
| 59 | static int mtk_xfrm_offload_cdrt_setup(struct mtk_xfrm_params *xfrm_params) |
| 60 | { |
| 61 | struct cdrt_desc *cdesc = &xfrm_params->cdrt->desc; |
| 62 | |
| 63 | cdesc->desc1.common.type = 3; |
| 64 | cdesc->desc1.token_len = 48; |
| 65 | cdesc->desc1.p_tr[0] = __pa(xfrm_params->p_tr) | 2; |
| 66 | |
| 67 | cdesc->desc2.hw_srv = 2; |
| 68 | cdesc->desc2.allow_pad = 1; |
| 69 | cdesc->desc2.strip_pad = 1; |
| 70 | |
| 71 | return mtk_pce_cdrt_entry_write(xfrm_params->cdrt); |
| 72 | } |
| 73 | |
developer | 8ac909d | 2023-08-25 11:03:33 +0800 | [diff] [blame] | 74 | static void mtk_xfrm_offload_cls_entry_tear_down(struct mtk_xfrm_params *xfrm_params) |
| 75 | { |
| 76 | memset(&xfrm_params->cdrt->cls->cdesc, 0, sizeof(struct cls_desc)); |
| 77 | |
| 78 | mtk_pce_cls_entry_write(xfrm_params->cdrt->cls); |
| 79 | |
| 80 | mtk_pce_cls_entry_free(xfrm_params->cdrt->cls); |
| 81 | } |
| 82 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 83 | static int mtk_xfrm_offload_cls_entry_setup(struct mtk_xfrm_params *xfrm_params) |
| 84 | { |
developer | 8ac909d | 2023-08-25 11:03:33 +0800 | [diff] [blame] | 85 | struct cls_desc *cdesc; |
| 86 | |
| 87 | xfrm_params->cdrt->cls = mtk_pce_cls_entry_alloc(); |
| 88 | if (IS_ERR(xfrm_params->cdrt->cls)) |
| 89 | return PTR_ERR(xfrm_params->cdrt->cls); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 90 | |
developer | 8ac909d | 2023-08-25 11:03:33 +0800 | [diff] [blame] | 91 | cdesc = &xfrm_params->cdrt->cls->cdesc; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 92 | |
developer | 5922d4d | 2024-02-26 19:01:58 +0800 | [diff] [blame] | 93 | if (mtk_crypto_ppe_get_num() == 1) |
| 94 | CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE0); |
| 95 | else |
| 96 | CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE1); |
developer | 8ac909d | 2023-08-25 11:03:33 +0800 | [diff] [blame] | 97 | CLS_DESC_DATA(cdesc, tport_idx, 0x2); |
| 98 | CLS_DESC_DATA(cdesc, cdrt_idx, xfrm_params->cdrt->idx); |
| 99 | |
| 100 | CLS_DESC_MASK_DATA(cdesc, tag, |
| 101 | CLS_DESC_TAG_MASK, CLS_DESC_TAG_MATCH_L4_HDR); |
| 102 | CLS_DESC_MASK_DATA(cdesc, l4_udp_hdr_nez, |
| 103 | CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK, |
| 104 | CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK); |
| 105 | CLS_DESC_MASK_DATA(cdesc, l4_type, |
| 106 | CLS_DESC_L4_TYPE_MASK, IPPROTO_ESP); |
| 107 | CLS_DESC_MASK_DATA(cdesc, l4_valid, |
| 108 | 0x3, |
| 109 | CLS_DESC_VALID_UPPER_HALF_WORD_BIT | |
| 110 | CLS_DESC_VALID_LOWER_HALF_WORD_BIT); |
| 111 | CLS_DESC_MASK_DATA(cdesc, l4_hdr_usr_data, |
| 112 | 0xFFFFFFFF, be32_to_cpu(xfrm_params->xs->id.spi)); |
| 113 | |
| 114 | return mtk_pce_cls_entry_write(xfrm_params->cdrt->cls); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void mtk_xfrm_offload_context_tear_down(struct mtk_xfrm_params *xfrm_params) |
| 118 | { |
| 119 | mtk_xfrm_offload_cdrt_tear_down(xfrm_params); |
| 120 | |
| 121 | /* TODO: free context */ |
| 122 | devm_kfree(crypto_dev, xfrm_params->p_tr); |
| 123 | |
| 124 | /* TODO: transform record tear down */ |
| 125 | } |
| 126 | |
| 127 | static int mtk_xfrm_offload_context_setup(struct mtk_xfrm_params *xfrm_params) |
| 128 | { |
| 129 | u32 *tr; |
| 130 | int ret; |
| 131 | |
| 132 | xfrm_params->p_tr = devm_kcalloc(crypto_dev, sizeof(u32), |
| 133 | TRANSFORM_RECORD_LEN, GFP_KERNEL); |
| 134 | if (unlikely(!xfrm_params->p_tr)) |
| 135 | return -ENOMEM; |
| 136 | |
| 137 | switch (xfrm_params->xs->outer_mode.encap) { |
| 138 | case XFRM_MODE_TUNNEL: |
| 139 | tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TUNNEL); |
| 140 | break; |
| 141 | case XFRM_MODE_TRANSPORT: |
| 142 | tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TRANSPORT); |
| 143 | break; |
| 144 | default: |
| 145 | ret = -ENOMEM; |
| 146 | goto err_out; |
| 147 | } |
| 148 | |
| 149 | if (!tr) { |
| 150 | ret = -EINVAL; |
| 151 | goto err_out; |
| 152 | } |
| 153 | |
| 154 | memcpy(xfrm_params->p_tr, tr, sizeof(u32) * TRANSFORM_RECORD_LEN); |
| 155 | |
| 156 | /* TODO: free tr */ |
| 157 | |
| 158 | return mtk_xfrm_offload_cdrt_setup(xfrm_params); |
| 159 | |
| 160 | err_out: |
| 161 | devm_kfree(crypto_dev, xfrm_params->p_tr); |
| 162 | |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static int mtk_xfrm_offload_state_add_outbound(struct xfrm_state *xs, |
| 167 | struct mtk_xfrm_params *xfrm_params) |
| 168 | { |
| 169 | int ret; |
| 170 | |
| 171 | xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_ENCRYPT); |
| 172 | if (IS_ERR(xfrm_params->cdrt)) |
| 173 | return PTR_ERR(xfrm_params->cdrt); |
| 174 | |
| 175 | xfrm_params->dir = SAB_DIRECTION_OUTBOUND; |
| 176 | |
| 177 | ret = mtk_xfrm_offload_context_setup(xfrm_params); |
| 178 | if (ret) |
| 179 | goto free_cdrt; |
| 180 | |
| 181 | return ret; |
| 182 | |
| 183 | free_cdrt: |
| 184 | mtk_pce_cdrt_entry_free(xfrm_params->cdrt); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
| 189 | static int mtk_xfrm_offload_state_add_inbound(struct xfrm_state *xs, |
| 190 | struct mtk_xfrm_params *xfrm_params) |
| 191 | { |
| 192 | int ret; |
| 193 | |
| 194 | xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_DECRYPT); |
| 195 | if (IS_ERR(xfrm_params->cdrt)) |
| 196 | return PTR_ERR(xfrm_params->cdrt); |
| 197 | |
| 198 | xfrm_params->dir = SAB_DIRECTION_INBOUND; |
| 199 | |
| 200 | ret = mtk_xfrm_offload_context_setup(xfrm_params); |
| 201 | if (ret) |
| 202 | goto free_cdrt; |
| 203 | |
| 204 | ret = mtk_xfrm_offload_cls_entry_setup(xfrm_params); |
| 205 | if (ret) |
| 206 | goto tear_down_context; |
| 207 | |
| 208 | return ret; |
| 209 | |
| 210 | tear_down_context: |
| 211 | mtk_xfrm_offload_context_tear_down(xfrm_params); |
| 212 | |
| 213 | free_cdrt: |
| 214 | mtk_pce_cdrt_entry_free(xfrm_params->cdrt); |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | int mtk_xfrm_offload_state_add(struct xfrm_state *xs) |
| 220 | { |
| 221 | struct mtk_xfrm_params *xfrm_params; |
| 222 | int ret = 0; |
| 223 | |
| 224 | /* TODO: maybe support IPv6 in the future? */ |
| 225 | if (xs->props.family != AF_INET) { |
| 226 | CRYPTO_NOTICE("Only IPv4 xfrm states may be offloaded\n"); |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | |
| 230 | /* only support ESP right now */ |
| 231 | if (xs->id.proto != IPPROTO_ESP) { |
| 232 | CRYPTO_NOTICE("Unsupported protocol 0x%04x\n", xs->id.proto); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | /* only support tunnel mode or transport mode */ |
| 237 | if (!(xs->outer_mode.encap == XFRM_MODE_TUNNEL |
| 238 | || xs->outer_mode.encap == XFRM_MODE_TRANSPORT)) |
| 239 | return -EINVAL; |
| 240 | |
| 241 | xfrm_params = devm_kzalloc(crypto_dev, |
| 242 | sizeof(struct mtk_xfrm_params), |
| 243 | GFP_KERNEL); |
| 244 | if (!xfrm_params) |
| 245 | return -ENOMEM; |
| 246 | |
| 247 | xfrm_params->xs = xs; |
| 248 | INIT_LIST_HEAD(&xfrm_params->node); |
| 249 | |
| 250 | if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) |
| 251 | /* rx path */ |
| 252 | ret = mtk_xfrm_offload_state_add_inbound(xs, xfrm_params); |
| 253 | else |
| 254 | /* tx path */ |
| 255 | ret = mtk_xfrm_offload_state_add_outbound(xs, xfrm_params); |
| 256 | |
| 257 | if (ret) { |
| 258 | devm_kfree(crypto_dev, xfrm_params); |
| 259 | goto out; |
| 260 | } |
| 261 | |
| 262 | xs->xso.offload_handle = (unsigned long)xfrm_params; |
| 263 | list_add_tail(&xfrm_params->node, &xfrm_params_head); |
| 264 | out: |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | void mtk_xfrm_offload_state_delete(struct xfrm_state *xs) |
| 269 | { |
| 270 | } |
| 271 | |
| 272 | void mtk_xfrm_offload_state_free(struct xfrm_state *xs) |
| 273 | { |
| 274 | struct mtk_xfrm_params *xfrm_params; |
| 275 | |
| 276 | if (!xs->xso.offload_handle) |
| 277 | return; |
| 278 | |
| 279 | xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle; |
| 280 | |
| 281 | list_del(&xfrm_params->node); |
| 282 | |
| 283 | if (xs->xso.flags & XFRM_OFFLOAD_INBOUND) |
developer | 8ac909d | 2023-08-25 11:03:33 +0800 | [diff] [blame] | 284 | mtk_xfrm_offload_cls_entry_tear_down(xfrm_params); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 285 | |
| 286 | mtk_xfrm_offload_context_tear_down(xfrm_params); |
| 287 | |
| 288 | mtk_pce_cdrt_entry_free(xfrm_params->cdrt); |
| 289 | |
| 290 | devm_kfree(crypto_dev, xfrm_params); |
| 291 | } |
| 292 | |
| 293 | void mtk_xfrm_offload_state_tear_down(void) |
| 294 | { |
| 295 | struct mtk_xfrm_params *xfrm_params, *tmp; |
| 296 | |
| 297 | list_for_each_entry_safe(xfrm_params, tmp, &xfrm_params_head, node) |
| 298 | mtk_xfrm_offload_state_free(xfrm_params->xs); |
| 299 | } |
| 300 | |
| 301 | int mtk_xfrm_offload_policy_add(struct xfrm_policy *xp) |
| 302 | { |
| 303 | return 0; |
| 304 | } |
| 305 | |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 306 | static inline struct neighbour *mtk_crypto_find_dst_mac(struct sk_buff *skb, struct xfrm_state *xs) |
| 307 | { |
| 308 | struct neighbour *neigh; |
| 309 | u32 nexthop; |
| 310 | struct dst_entry *dst = skb_dst(skb); |
| 311 | struct rtable *rt = (struct rtable *) dst; |
| 312 | |
| 313 | nexthop = (__force u32) rt_nexthop(rt, xs->id.daddr.a4); |
| 314 | neigh = __ipv4_neigh_lookup_noref(dst->dev, nexthop); |
| 315 | if (unlikely(!neigh)) { |
| 316 | CRYPTO_INFO("%s: %s No neigh (daddr=%pI4)\n", __func__, dst->dev->name, |
| 317 | &xs->id.daddr.a4); |
| 318 | neigh = __neigh_create(&arp_tbl, &xs->id.daddr.a4, dst->dev, false); |
developer | fba064e | 2024-01-09 14:52:17 +0800 | [diff] [blame] | 319 | neigh_output(neigh, skb, false); |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 320 | return NULL; |
| 321 | } |
| 322 | |
| 323 | return neigh; |
| 324 | } |
| 325 | |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 326 | bool mtk_xfrm_offload_ok(struct sk_buff *skb, |
| 327 | struct xfrm_state *xs) |
| 328 | { |
| 329 | struct mtk_xfrm_params *xfrm_params; |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 330 | struct neighbour *neigh; |
| 331 | struct dst_entry *dst = skb_dst(skb); |
| 332 | |
| 333 | rcu_read_lock_bh(); |
| 334 | |
| 335 | neigh = mtk_crypto_find_dst_mac(skb, xs); |
| 336 | if (!neigh) { |
| 337 | rcu_read_unlock_bh(); |
| 338 | return true; |
| 339 | } |
| 340 | |
| 341 | skb_push(skb, sizeof(struct ethhdr)); |
| 342 | skb_reset_mac_header(skb); |
| 343 | |
| 344 | eth_hdr(skb)->h_proto = htons(ETH_P_IP); |
| 345 | memcpy(eth_hdr(skb)->h_dest, neigh->ha, ETH_ALEN); |
| 346 | memcpy(eth_hdr(skb)->h_source, dst->dev->dev_addr, ETH_ALEN); |
| 347 | |
| 348 | rcu_read_unlock_bh(); |
| 349 | |
| 350 | xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 351 | |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 352 | #if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT) |
| 353 | skb_hnat_cdrt(skb) = xfrm_params->cdrt->idx; |
developer | 52e00b7 | 2023-09-04 11:13:18 +0800 | [diff] [blame] | 354 | /* |
| 355 | * EIP197 does not support fragmentation. As a result, we can not bind UDP |
| 356 | * flow since it may cause network fail due to fragmentation |
| 357 | */ |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 358 | if (ra_sw_nat_hook_tx && |
| 359 | ((is_tops_udp_tunnel(skb) || is_tcp(skb)) && is_hnat_rate_reach(skb))) |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 360 | hnat_bind_crypto_entry(skb, dst->dev); |
developer | 52e00b7 | 2023-09-04 11:13:18 +0800 | [diff] [blame] | 361 | |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 362 | /* Set magic tag for tport setting, reset to 0 after tport is set */ |
| 363 | skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG; |
developer | 49489b8 | 2024-03-28 15:18:08 +0800 | [diff] [blame^] | 364 | #else |
| 365 | skb_tnl_cdrt(skb) = xfrm_params->cdrt->idx; |
| 366 | skb_tnl_magic_tag(skb) = TNL_MAGIC_TAG; |
| 367 | #endif // HNAT |
| 368 | |
| 369 | /* Since we're going to tx directly, set skb->dev to dst->dev */ |
| 370 | skb->dev = dst->dev; |
developer | 05215d1 | 2024-01-04 19:14:19 +0800 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * Since skb headroom may not be copy when segment, we cannot rely on |
| 374 | * headroom data (ex. cdrt) to decide packets should send to EIP197. |
| 375 | * Here is a workaround that only skb with inner_protocol = ESP will |
| 376 | * be sent to EIP197. |
| 377 | */ |
| 378 | skb->inner_protocol = IPPROTO_ESP; |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 379 | /* |
| 380 | * Tx packet to EIP197. |
| 381 | * To avoid conflict of SW and HW sequence number |
| 382 | * All offloadable packets send to EIP197 |
| 383 | */ |
| 384 | dev_queue_xmit(skb); |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 385 | |
developer | d0a7d72 | 2023-10-19 11:24:25 +0800 | [diff] [blame] | 386 | return true; |
developer | 94c513e | 2023-08-21 17:33:25 +0800 | [diff] [blame] | 387 | } |