blob: 34b4ecc8d47c56454160675ea7159ccb4e246909 [file] [log] [blame]
developer94c513e2023-08-21 17:33:25 +08001// 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>
developer1baf13c2024-07-05 15:43:05 +080010#include <linux/spinlock.h>
developer94c513e2023-08-21 17:33:25 +080011
12#include <mtk_eth_soc.h>
developer49489b82024-03-28 15:18:08 +080013
14#if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
developer94c513e2023-08-21 17:33:25 +080015#include <mtk_hnat/hnat.h>
16#include <mtk_hnat/nf_hnat_mtk.h>
developer49489b82024-03-28 15:18:08 +080017#endif // HNAT
developer94c513e2023-08-21 17:33:25 +080018
19#include <pce/cdrt.h>
20#include <pce/cls.h>
21#include <pce/netsys.h>
22
developer94c513e2023-08-21 17:33:25 +080023#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
developer1baf13c2024-07-05 15:43:05 +080030static struct xfrm_params_list xfrm_params_list = {
31 .list = LIST_HEAD_INIT(xfrm_params_list.list),
32 .lock = __SPIN_LOCK_UNLOCKED(xfrm_params_list.lock),
33};
developer94c513e2023-08-21 17:33:25 +080034
developer49489b82024-03-28 15:18:08 +080035#if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
36extern int (*ra_sw_nat_hook_tx)(struct sk_buff *skb, int gmac_no);
37
developerc9adb362024-07-05 13:30:41 +080038static inline bool is_tops_tunnel(struct sk_buff *skb)
developerd0a7d722023-10-19 11:24:25 +080039{
40 return skb_hnat_tops(skb) && (ntohs(skb->protocol) == ETH_P_IP) &&
developerc9adb362024-07-05 13:30:41 +080041 (ip_hdr(skb)->protocol == IPPROTO_UDP ||
42 ip_hdr(skb)->protocol == IPPROTO_GRE);
developerd0a7d722023-10-19 11:24:25 +080043}
44
45static inline bool is_tcp(struct sk_buff *skb)
46{
47 return (ntohs(skb->protocol) == ETH_P_IP) && (ip_hdr(skb)->protocol == IPPROTO_TCP);
48}
49
50static inline bool is_hnat_rate_reach(struct sk_buff *skb)
51{
52 return is_magic_tag_valid(skb) && (skb_hnat_reason(skb) == HIT_UNBIND_RATE_REACH);
53}
developer49489b82024-03-28 15:18:08 +080054#endif // HNAT
developerd0a7d722023-10-19 11:24:25 +080055
developer1baf13c2024-07-05 15:43:05 +080056struct xfrm_params_list *mtk_xfrm_params_list_get(void)
57{
58 return &xfrm_params_list;
59}
60
developer94c513e2023-08-21 17:33:25 +080061static void mtk_xfrm_offload_cdrt_tear_down(struct mtk_xfrm_params *xfrm_params)
62{
developer9d17aad2024-08-01 17:36:15 +080063 if (!xfrm_params->cdrt)
64 return;
65
developer94c513e2023-08-21 17:33:25 +080066 memset(&xfrm_params->cdrt->desc, 0, sizeof(struct cdrt_desc));
67
68 mtk_pce_cdrt_entry_write(xfrm_params->cdrt);
69}
70
71static int mtk_xfrm_offload_cdrt_setup(struct mtk_xfrm_params *xfrm_params)
72{
73 struct cdrt_desc *cdesc = &xfrm_params->cdrt->desc;
74
75 cdesc->desc1.common.type = 3;
76 cdesc->desc1.token_len = 48;
77 cdesc->desc1.p_tr[0] = __pa(xfrm_params->p_tr) | 2;
78
developerc9adb362024-07-05 13:30:41 +080079 cdesc->desc2.hw_srv = 3;
developer94c513e2023-08-21 17:33:25 +080080 cdesc->desc2.allow_pad = 1;
81 cdesc->desc2.strip_pad = 1;
82
83 return mtk_pce_cdrt_entry_write(xfrm_params->cdrt);
84}
85
developer8ac909d2023-08-25 11:03:33 +080086static void mtk_xfrm_offload_cls_entry_tear_down(struct mtk_xfrm_params *xfrm_params)
87{
developer9d17aad2024-08-01 17:36:15 +080088 if (!xfrm_params->cdrt || !xfrm_params->cdrt->cls)
89 return;
90
developer8ac909d2023-08-25 11:03:33 +080091 memset(&xfrm_params->cdrt->cls->cdesc, 0, sizeof(struct cls_desc));
92
93 mtk_pce_cls_entry_write(xfrm_params->cdrt->cls);
94
95 mtk_pce_cls_entry_free(xfrm_params->cdrt->cls);
96}
97
developer94c513e2023-08-21 17:33:25 +080098static int mtk_xfrm_offload_cls_entry_setup(struct mtk_xfrm_params *xfrm_params)
99{
developer8ac909d2023-08-25 11:03:33 +0800100 struct cls_desc *cdesc;
101
102 xfrm_params->cdrt->cls = mtk_pce_cls_entry_alloc();
103 if (IS_ERR(xfrm_params->cdrt->cls))
104 return PTR_ERR(xfrm_params->cdrt->cls);
developer94c513e2023-08-21 17:33:25 +0800105
developer8ac909d2023-08-25 11:03:33 +0800106 cdesc = &xfrm_params->cdrt->cls->cdesc;
developer94c513e2023-08-21 17:33:25 +0800107
developer5922d4d2024-02-26 19:01:58 +0800108 if (mtk_crypto_ppe_get_num() == 1)
109 CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE0);
110 else
111 CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE1);
developer8ac909d2023-08-25 11:03:33 +0800112 CLS_DESC_DATA(cdesc, tport_idx, 0x2);
113 CLS_DESC_DATA(cdesc, cdrt_idx, xfrm_params->cdrt->idx);
114
115 CLS_DESC_MASK_DATA(cdesc, tag,
116 CLS_DESC_TAG_MASK, CLS_DESC_TAG_MATCH_L4_HDR);
117 CLS_DESC_MASK_DATA(cdesc, l4_udp_hdr_nez,
118 CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK,
119 CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK);
120 CLS_DESC_MASK_DATA(cdesc, l4_type,
121 CLS_DESC_L4_TYPE_MASK, IPPROTO_ESP);
122 CLS_DESC_MASK_DATA(cdesc, l4_valid,
123 0x3,
124 CLS_DESC_VALID_UPPER_HALF_WORD_BIT |
125 CLS_DESC_VALID_LOWER_HALF_WORD_BIT);
126 CLS_DESC_MASK_DATA(cdesc, l4_hdr_usr_data,
127 0xFFFFFFFF, be32_to_cpu(xfrm_params->xs->id.spi));
128
129 return mtk_pce_cls_entry_write(xfrm_params->cdrt->cls);
developer94c513e2023-08-21 17:33:25 +0800130}
131
132static void mtk_xfrm_offload_context_tear_down(struct mtk_xfrm_params *xfrm_params)
133{
134 mtk_xfrm_offload_cdrt_tear_down(xfrm_params);
135
136 /* TODO: free context */
137 devm_kfree(crypto_dev, xfrm_params->p_tr);
138
139 /* TODO: transform record tear down */
140}
141
142static int mtk_xfrm_offload_context_setup(struct mtk_xfrm_params *xfrm_params)
143{
144 u32 *tr;
145 int ret;
146
147 xfrm_params->p_tr = devm_kcalloc(crypto_dev, sizeof(u32),
148 TRANSFORM_RECORD_LEN, GFP_KERNEL);
149 if (unlikely(!xfrm_params->p_tr))
150 return -ENOMEM;
151
152 switch (xfrm_params->xs->outer_mode.encap) {
153 case XFRM_MODE_TUNNEL:
154 tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TUNNEL);
155 break;
156 case XFRM_MODE_TRANSPORT:
157 tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TRANSPORT);
158 break;
159 default:
160 ret = -ENOMEM;
161 goto err_out;
162 }
163
164 if (!tr) {
165 ret = -EINVAL;
166 goto err_out;
167 }
168
169 memcpy(xfrm_params->p_tr, tr, sizeof(u32) * TRANSFORM_RECORD_LEN);
170
171 /* TODO: free tr */
172
173 return mtk_xfrm_offload_cdrt_setup(xfrm_params);
174
175err_out:
176 devm_kfree(crypto_dev, xfrm_params->p_tr);
177
178 return ret;
179}
180
181static int mtk_xfrm_offload_state_add_outbound(struct xfrm_state *xs,
182 struct mtk_xfrm_params *xfrm_params)
183{
184 int ret;
185
186 xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_ENCRYPT);
187 if (IS_ERR(xfrm_params->cdrt))
188 return PTR_ERR(xfrm_params->cdrt);
189
190 xfrm_params->dir = SAB_DIRECTION_OUTBOUND;
191
192 ret = mtk_xfrm_offload_context_setup(xfrm_params);
193 if (ret)
194 goto free_cdrt;
195
196 return ret;
197
198free_cdrt:
199 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
200
201 return ret;
202}
203
204static int mtk_xfrm_offload_state_add_inbound(struct xfrm_state *xs,
205 struct mtk_xfrm_params *xfrm_params)
206{
207 int ret;
208
209 xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_DECRYPT);
210 if (IS_ERR(xfrm_params->cdrt))
211 return PTR_ERR(xfrm_params->cdrt);
212
213 xfrm_params->dir = SAB_DIRECTION_INBOUND;
214
215 ret = mtk_xfrm_offload_context_setup(xfrm_params);
216 if (ret)
217 goto free_cdrt;
218
219 ret = mtk_xfrm_offload_cls_entry_setup(xfrm_params);
220 if (ret)
221 goto tear_down_context;
222
223 return ret;
224
225tear_down_context:
226 mtk_xfrm_offload_context_tear_down(xfrm_params);
227
228free_cdrt:
229 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
230
231 return ret;
232}
233
234int mtk_xfrm_offload_state_add(struct xfrm_state *xs)
235{
236 struct mtk_xfrm_params *xfrm_params;
developer1baf13c2024-07-05 15:43:05 +0800237 unsigned long flags;
developer94c513e2023-08-21 17:33:25 +0800238 int ret = 0;
239
240 /* TODO: maybe support IPv6 in the future? */
241 if (xs->props.family != AF_INET) {
242 CRYPTO_NOTICE("Only IPv4 xfrm states may be offloaded\n");
243 return -EINVAL;
244 }
245
246 /* only support ESP right now */
247 if (xs->id.proto != IPPROTO_ESP) {
248 CRYPTO_NOTICE("Unsupported protocol 0x%04x\n", xs->id.proto);
249 return -EINVAL;
250 }
251
252 /* only support tunnel mode or transport mode */
253 if (!(xs->outer_mode.encap == XFRM_MODE_TUNNEL
254 || xs->outer_mode.encap == XFRM_MODE_TRANSPORT))
255 return -EINVAL;
256
257 xfrm_params = devm_kzalloc(crypto_dev,
258 sizeof(struct mtk_xfrm_params),
259 GFP_KERNEL);
260 if (!xfrm_params)
261 return -ENOMEM;
262
263 xfrm_params->xs = xs;
264 INIT_LIST_HEAD(&xfrm_params->node);
265
266 if (xs->xso.flags & XFRM_OFFLOAD_INBOUND)
267 /* rx path */
268 ret = mtk_xfrm_offload_state_add_inbound(xs, xfrm_params);
269 else
270 /* tx path */
271 ret = mtk_xfrm_offload_state_add_outbound(xs, xfrm_params);
272
273 if (ret) {
274 devm_kfree(crypto_dev, xfrm_params);
275 goto out;
276 }
277
278 xs->xso.offload_handle = (unsigned long)xfrm_params;
developer1baf13c2024-07-05 15:43:05 +0800279
280 spin_lock_irqsave(&xfrm_params_list.lock, flags);
281
282 list_add_tail(&xfrm_params->node, &xfrm_params_list.list);
283
284 spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
developer94c513e2023-08-21 17:33:25 +0800285out:
286 return ret;
287}
288
289void mtk_xfrm_offload_state_delete(struct xfrm_state *xs)
290{
291}
292
293void mtk_xfrm_offload_state_free(struct xfrm_state *xs)
294{
295 struct mtk_xfrm_params *xfrm_params;
296
297 if (!xs->xso.offload_handle)
298 return;
299
300 xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle;
301
302 list_del(&xfrm_params->node);
303
304 if (xs->xso.flags & XFRM_OFFLOAD_INBOUND)
developer8ac909d2023-08-25 11:03:33 +0800305 mtk_xfrm_offload_cls_entry_tear_down(xfrm_params);
developer94c513e2023-08-21 17:33:25 +0800306
307 mtk_xfrm_offload_context_tear_down(xfrm_params);
308
developer9d17aad2024-08-01 17:36:15 +0800309 if (xfrm_params->cdrt)
310 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
developer94c513e2023-08-21 17:33:25 +0800311
312 devm_kfree(crypto_dev, xfrm_params);
313}
314
315void mtk_xfrm_offload_state_tear_down(void)
316{
317 struct mtk_xfrm_params *xfrm_params, *tmp;
developer1baf13c2024-07-05 15:43:05 +0800318 unsigned long flags;
developer94c513e2023-08-21 17:33:25 +0800319
developer1baf13c2024-07-05 15:43:05 +0800320 spin_lock_irqsave(&xfrm_params_list.lock, flags);
321
322 list_for_each_entry_safe(xfrm_params, tmp, &xfrm_params_list.list, node)
developer94c513e2023-08-21 17:33:25 +0800323 mtk_xfrm_offload_state_free(xfrm_params->xs);
developer1baf13c2024-07-05 15:43:05 +0800324
325 spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
developer94c513e2023-08-21 17:33:25 +0800326}
327
328int mtk_xfrm_offload_policy_add(struct xfrm_policy *xp)
329{
330 return 0;
331}
332
developerd0a7d722023-10-19 11:24:25 +0800333static inline struct neighbour *mtk_crypto_find_dst_mac(struct sk_buff *skb, struct xfrm_state *xs)
334{
335 struct neighbour *neigh;
developerd0a7d722023-10-19 11:24:25 +0800336 struct dst_entry *dst = skb_dst(skb);
developerd0a7d722023-10-19 11:24:25 +0800337
developer8ac52a62024-07-01 16:11:43 +0800338 neigh = __ipv4_neigh_lookup_noref(dst->dev, xs->id.daddr.a4);
developerd0a7d722023-10-19 11:24:25 +0800339 if (unlikely(!neigh)) {
340 CRYPTO_INFO("%s: %s No neigh (daddr=%pI4)\n", __func__, dst->dev->name,
341 &xs->id.daddr.a4);
342 neigh = __neigh_create(&arp_tbl, &xs->id.daddr.a4, dst->dev, false);
developerfba064e2024-01-09 14:52:17 +0800343 neigh_output(neigh, skb, false);
developerd0a7d722023-10-19 11:24:25 +0800344 return NULL;
345 }
346
347 return neigh;
348}
349
developer94c513e2023-08-21 17:33:25 +0800350bool mtk_xfrm_offload_ok(struct sk_buff *skb,
351 struct xfrm_state *xs)
352{
353 struct mtk_xfrm_params *xfrm_params;
developerd0a7d722023-10-19 11:24:25 +0800354 struct neighbour *neigh;
355 struct dst_entry *dst = skb_dst(skb);
developer8ac52a62024-07-01 16:11:43 +0800356 int fill_inner_info = 0;
developerd0a7d722023-10-19 11:24:25 +0800357
358 rcu_read_lock_bh();
359
360 neigh = mtk_crypto_find_dst_mac(skb, xs);
361 if (!neigh) {
362 rcu_read_unlock_bh();
363 return true;
364 }
365
developer8ac52a62024-07-01 16:11:43 +0800366 /*
367 * For packet has pass through VTI (route-based VTI)
368 * The 'dev_queue_xmit' function called at network layer will cause both
369 * skb->mac_header and skb->network_header to point to the IP header
370 */
371 if (skb->mac_header == skb->network_header)
372 fill_inner_info = 1;
373
developerd0a7d722023-10-19 11:24:25 +0800374 skb_push(skb, sizeof(struct ethhdr));
375 skb_reset_mac_header(skb);
376
377 eth_hdr(skb)->h_proto = htons(ETH_P_IP);
378 memcpy(eth_hdr(skb)->h_dest, neigh->ha, ETH_ALEN);
379 memcpy(eth_hdr(skb)->h_source, dst->dev->dev_addr, ETH_ALEN);
380
381 rcu_read_unlock_bh();
382
383 xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle;
developer94c513e2023-08-21 17:33:25 +0800384
developer49489b82024-03-28 15:18:08 +0800385#if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
386 skb_hnat_cdrt(skb) = xfrm_params->cdrt->idx;
developer52e00b72023-09-04 11:13:18 +0800387 /*
388 * EIP197 does not support fragmentation. As a result, we can not bind UDP
389 * flow since it may cause network fail due to fragmentation
390 */
developer49489b82024-03-28 15:18:08 +0800391 if (ra_sw_nat_hook_tx &&
developerc9adb362024-07-05 13:30:41 +0800392 ((is_tops_tunnel(skb) || is_tcp(skb)) && is_hnat_rate_reach(skb)))
developer8ac52a62024-07-01 16:11:43 +0800393 hnat_bind_crypto_entry(skb, dst->dev, fill_inner_info);
developer52e00b72023-09-04 11:13:18 +0800394
developerd0a7d722023-10-19 11:24:25 +0800395 /* Set magic tag for tport setting, reset to 0 after tport is set */
396 skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG;
developer49489b82024-03-28 15:18:08 +0800397#else
398 skb_tnl_cdrt(skb) = xfrm_params->cdrt->idx;
399 skb_tnl_magic_tag(skb) = TNL_MAGIC_TAG;
400#endif // HNAT
401
402 /* Since we're going to tx directly, set skb->dev to dst->dev */
403 skb->dev = dst->dev;
developer05215d12024-01-04 19:14:19 +0800404
405 /*
406 * Since skb headroom may not be copy when segment, we cannot rely on
407 * headroom data (ex. cdrt) to decide packets should send to EIP197.
408 * Here is a workaround that only skb with inner_protocol = ESP will
409 * be sent to EIP197.
410 */
411 skb->inner_protocol = IPPROTO_ESP;
developerd0a7d722023-10-19 11:24:25 +0800412 /*
413 * Tx packet to EIP197.
414 * To avoid conflict of SW and HW sequence number
415 * All offloadable packets send to EIP197
416 */
417 dev_queue_xmit(skb);
developer94c513e2023-08-21 17:33:25 +0800418
developerd0a7d722023-10-19 11:24:25 +0800419 return true;
developer94c513e2023-08-21 17:33:25 +0800420}