blob: 1075b771550b0ccca6ea868c96f2ed67b3965652 [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
23#include <crypto-eip/ddk/configs/cs_hwpal_ext.h>
24#include <crypto-eip/ddk/kit/iotoken/iotoken.h>
25#include <crypto-eip/ddk/kit/iotoken/iotoken_ext.h>
26
27#include "crypto-eip/crypto-eip.h"
28#include "crypto-eip/ddk-wrapper.h"
29#include "crypto-eip/internal.h"
30
developer1baf13c2024-07-05 15:43:05 +080031static struct xfrm_params_list xfrm_params_list = {
32 .list = LIST_HEAD_INIT(xfrm_params_list.list),
33 .lock = __SPIN_LOCK_UNLOCKED(xfrm_params_list.lock),
34};
developer94c513e2023-08-21 17:33:25 +080035
developer49489b82024-03-28 15:18:08 +080036#if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
37extern int (*ra_sw_nat_hook_tx)(struct sk_buff *skb, int gmac_no);
38
developerc9adb362024-07-05 13:30:41 +080039static inline bool is_tops_tunnel(struct sk_buff *skb)
developerd0a7d722023-10-19 11:24:25 +080040{
41 return skb_hnat_tops(skb) && (ntohs(skb->protocol) == ETH_P_IP) &&
developerc9adb362024-07-05 13:30:41 +080042 (ip_hdr(skb)->protocol == IPPROTO_UDP ||
43 ip_hdr(skb)->protocol == IPPROTO_GRE);
developerd0a7d722023-10-19 11:24:25 +080044}
45
46static inline bool is_tcp(struct sk_buff *skb)
47{
48 return (ntohs(skb->protocol) == ETH_P_IP) && (ip_hdr(skb)->protocol == IPPROTO_TCP);
49}
50
51static inline bool is_hnat_rate_reach(struct sk_buff *skb)
52{
53 return is_magic_tag_valid(skb) && (skb_hnat_reason(skb) == HIT_UNBIND_RATE_REACH);
54}
developer49489b82024-03-28 15:18:08 +080055#endif // HNAT
developerd0a7d722023-10-19 11:24:25 +080056
developer1baf13c2024-07-05 15:43:05 +080057struct xfrm_params_list *mtk_xfrm_params_list_get(void)
58{
59 return &xfrm_params_list;
60}
61
developer94c513e2023-08-21 17:33:25 +080062static void mtk_xfrm_offload_cdrt_tear_down(struct mtk_xfrm_params *xfrm_params)
63{
64 memset(&xfrm_params->cdrt->desc, 0, sizeof(struct cdrt_desc));
65
66 mtk_pce_cdrt_entry_write(xfrm_params->cdrt);
67}
68
69static int mtk_xfrm_offload_cdrt_setup(struct mtk_xfrm_params *xfrm_params)
70{
71 struct cdrt_desc *cdesc = &xfrm_params->cdrt->desc;
72
73 cdesc->desc1.common.type = 3;
74 cdesc->desc1.token_len = 48;
75 cdesc->desc1.p_tr[0] = __pa(xfrm_params->p_tr) | 2;
76
developerc9adb362024-07-05 13:30:41 +080077 cdesc->desc2.hw_srv = 3;
developer94c513e2023-08-21 17:33:25 +080078 cdesc->desc2.allow_pad = 1;
79 cdesc->desc2.strip_pad = 1;
80
81 return mtk_pce_cdrt_entry_write(xfrm_params->cdrt);
82}
83
developer8ac909d2023-08-25 11:03:33 +080084static void mtk_xfrm_offload_cls_entry_tear_down(struct mtk_xfrm_params *xfrm_params)
85{
86 memset(&xfrm_params->cdrt->cls->cdesc, 0, sizeof(struct cls_desc));
87
88 mtk_pce_cls_entry_write(xfrm_params->cdrt->cls);
89
90 mtk_pce_cls_entry_free(xfrm_params->cdrt->cls);
91}
92
developer94c513e2023-08-21 17:33:25 +080093static int mtk_xfrm_offload_cls_entry_setup(struct mtk_xfrm_params *xfrm_params)
94{
developer8ac909d2023-08-25 11:03:33 +080095 struct cls_desc *cdesc;
96
97 xfrm_params->cdrt->cls = mtk_pce_cls_entry_alloc();
98 if (IS_ERR(xfrm_params->cdrt->cls))
99 return PTR_ERR(xfrm_params->cdrt->cls);
developer94c513e2023-08-21 17:33:25 +0800100
developer8ac909d2023-08-25 11:03:33 +0800101 cdesc = &xfrm_params->cdrt->cls->cdesc;
developer94c513e2023-08-21 17:33:25 +0800102
developer5922d4d2024-02-26 19:01:58 +0800103 if (mtk_crypto_ppe_get_num() == 1)
104 CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE0);
105 else
106 CLS_DESC_DATA(cdesc, fport, PSE_PORT_PPE1);
developer8ac909d2023-08-25 11:03:33 +0800107 CLS_DESC_DATA(cdesc, tport_idx, 0x2);
108 CLS_DESC_DATA(cdesc, cdrt_idx, xfrm_params->cdrt->idx);
109
110 CLS_DESC_MASK_DATA(cdesc, tag,
111 CLS_DESC_TAG_MASK, CLS_DESC_TAG_MATCH_L4_HDR);
112 CLS_DESC_MASK_DATA(cdesc, l4_udp_hdr_nez,
113 CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK,
114 CLS_DESC_UDPLITE_L4_HDR_NEZ_MASK);
115 CLS_DESC_MASK_DATA(cdesc, l4_type,
116 CLS_DESC_L4_TYPE_MASK, IPPROTO_ESP);
117 CLS_DESC_MASK_DATA(cdesc, l4_valid,
118 0x3,
119 CLS_DESC_VALID_UPPER_HALF_WORD_BIT |
120 CLS_DESC_VALID_LOWER_HALF_WORD_BIT);
121 CLS_DESC_MASK_DATA(cdesc, l4_hdr_usr_data,
122 0xFFFFFFFF, be32_to_cpu(xfrm_params->xs->id.spi));
123
124 return mtk_pce_cls_entry_write(xfrm_params->cdrt->cls);
developer94c513e2023-08-21 17:33:25 +0800125}
126
127static void mtk_xfrm_offload_context_tear_down(struct mtk_xfrm_params *xfrm_params)
128{
129 mtk_xfrm_offload_cdrt_tear_down(xfrm_params);
130
131 /* TODO: free context */
132 devm_kfree(crypto_dev, xfrm_params->p_tr);
133
134 /* TODO: transform record tear down */
135}
136
137static int mtk_xfrm_offload_context_setup(struct mtk_xfrm_params *xfrm_params)
138{
139 u32 *tr;
140 int ret;
141
142 xfrm_params->p_tr = devm_kcalloc(crypto_dev, sizeof(u32),
143 TRANSFORM_RECORD_LEN, GFP_KERNEL);
144 if (unlikely(!xfrm_params->p_tr))
145 return -ENOMEM;
146
147 switch (xfrm_params->xs->outer_mode.encap) {
148 case XFRM_MODE_TUNNEL:
149 tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TUNNEL);
150 break;
151 case XFRM_MODE_TRANSPORT:
152 tr = mtk_ddk_tr_ipsec_build(xfrm_params, SAB_IPSEC_TRANSPORT);
153 break;
154 default:
155 ret = -ENOMEM;
156 goto err_out;
157 }
158
159 if (!tr) {
160 ret = -EINVAL;
161 goto err_out;
162 }
163
164 memcpy(xfrm_params->p_tr, tr, sizeof(u32) * TRANSFORM_RECORD_LEN);
165
166 /* TODO: free tr */
167
168 return mtk_xfrm_offload_cdrt_setup(xfrm_params);
169
170err_out:
171 devm_kfree(crypto_dev, xfrm_params->p_tr);
172
173 return ret;
174}
175
176static int mtk_xfrm_offload_state_add_outbound(struct xfrm_state *xs,
177 struct mtk_xfrm_params *xfrm_params)
178{
179 int ret;
180
181 xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_ENCRYPT);
182 if (IS_ERR(xfrm_params->cdrt))
183 return PTR_ERR(xfrm_params->cdrt);
184
185 xfrm_params->dir = SAB_DIRECTION_OUTBOUND;
186
187 ret = mtk_xfrm_offload_context_setup(xfrm_params);
188 if (ret)
189 goto free_cdrt;
190
191 return ret;
192
193free_cdrt:
194 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
195
196 return ret;
197}
198
199static int mtk_xfrm_offload_state_add_inbound(struct xfrm_state *xs,
200 struct mtk_xfrm_params *xfrm_params)
201{
202 int ret;
203
204 xfrm_params->cdrt = mtk_pce_cdrt_entry_alloc(CDRT_DECRYPT);
205 if (IS_ERR(xfrm_params->cdrt))
206 return PTR_ERR(xfrm_params->cdrt);
207
208 xfrm_params->dir = SAB_DIRECTION_INBOUND;
209
210 ret = mtk_xfrm_offload_context_setup(xfrm_params);
211 if (ret)
212 goto free_cdrt;
213
214 ret = mtk_xfrm_offload_cls_entry_setup(xfrm_params);
215 if (ret)
216 goto tear_down_context;
217
218 return ret;
219
220tear_down_context:
221 mtk_xfrm_offload_context_tear_down(xfrm_params);
222
223free_cdrt:
224 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
225
226 return ret;
227}
228
229int mtk_xfrm_offload_state_add(struct xfrm_state *xs)
230{
231 struct mtk_xfrm_params *xfrm_params;
developer1baf13c2024-07-05 15:43:05 +0800232 unsigned long flags;
developer94c513e2023-08-21 17:33:25 +0800233 int ret = 0;
234
235 /* TODO: maybe support IPv6 in the future? */
236 if (xs->props.family != AF_INET) {
237 CRYPTO_NOTICE("Only IPv4 xfrm states may be offloaded\n");
238 return -EINVAL;
239 }
240
241 /* only support ESP right now */
242 if (xs->id.proto != IPPROTO_ESP) {
243 CRYPTO_NOTICE("Unsupported protocol 0x%04x\n", xs->id.proto);
244 return -EINVAL;
245 }
246
247 /* only support tunnel mode or transport mode */
248 if (!(xs->outer_mode.encap == XFRM_MODE_TUNNEL
249 || xs->outer_mode.encap == XFRM_MODE_TRANSPORT))
250 return -EINVAL;
251
252 xfrm_params = devm_kzalloc(crypto_dev,
253 sizeof(struct mtk_xfrm_params),
254 GFP_KERNEL);
255 if (!xfrm_params)
256 return -ENOMEM;
257
258 xfrm_params->xs = xs;
259 INIT_LIST_HEAD(&xfrm_params->node);
260
261 if (xs->xso.flags & XFRM_OFFLOAD_INBOUND)
262 /* rx path */
263 ret = mtk_xfrm_offload_state_add_inbound(xs, xfrm_params);
264 else
265 /* tx path */
266 ret = mtk_xfrm_offload_state_add_outbound(xs, xfrm_params);
267
268 if (ret) {
269 devm_kfree(crypto_dev, xfrm_params);
270 goto out;
271 }
272
273 xs->xso.offload_handle = (unsigned long)xfrm_params;
developer1baf13c2024-07-05 15:43:05 +0800274
275 spin_lock_irqsave(&xfrm_params_list.lock, flags);
276
277 list_add_tail(&xfrm_params->node, &xfrm_params_list.list);
278
279 spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
developer94c513e2023-08-21 17:33:25 +0800280out:
281 return ret;
282}
283
284void mtk_xfrm_offload_state_delete(struct xfrm_state *xs)
285{
286}
287
288void mtk_xfrm_offload_state_free(struct xfrm_state *xs)
289{
290 struct mtk_xfrm_params *xfrm_params;
291
292 if (!xs->xso.offload_handle)
293 return;
294
295 xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle;
296
297 list_del(&xfrm_params->node);
298
299 if (xs->xso.flags & XFRM_OFFLOAD_INBOUND)
developer8ac909d2023-08-25 11:03:33 +0800300 mtk_xfrm_offload_cls_entry_tear_down(xfrm_params);
developer94c513e2023-08-21 17:33:25 +0800301
302 mtk_xfrm_offload_context_tear_down(xfrm_params);
303
304 mtk_pce_cdrt_entry_free(xfrm_params->cdrt);
305
306 devm_kfree(crypto_dev, xfrm_params);
307}
308
309void mtk_xfrm_offload_state_tear_down(void)
310{
311 struct mtk_xfrm_params *xfrm_params, *tmp;
developer1baf13c2024-07-05 15:43:05 +0800312 unsigned long flags;
developer94c513e2023-08-21 17:33:25 +0800313
developer1baf13c2024-07-05 15:43:05 +0800314 spin_lock_irqsave(&xfrm_params_list.lock, flags);
315
316 list_for_each_entry_safe(xfrm_params, tmp, &xfrm_params_list.list, node)
developer94c513e2023-08-21 17:33:25 +0800317 mtk_xfrm_offload_state_free(xfrm_params->xs);
developer1baf13c2024-07-05 15:43:05 +0800318
319 spin_unlock_irqrestore(&xfrm_params_list.lock, flags);
developer94c513e2023-08-21 17:33:25 +0800320}
321
322int mtk_xfrm_offload_policy_add(struct xfrm_policy *xp)
323{
324 return 0;
325}
326
developerd0a7d722023-10-19 11:24:25 +0800327static inline struct neighbour *mtk_crypto_find_dst_mac(struct sk_buff *skb, struct xfrm_state *xs)
328{
329 struct neighbour *neigh;
developerd0a7d722023-10-19 11:24:25 +0800330 struct dst_entry *dst = skb_dst(skb);
developerd0a7d722023-10-19 11:24:25 +0800331
developer8ac52a62024-07-01 16:11:43 +0800332 neigh = __ipv4_neigh_lookup_noref(dst->dev, xs->id.daddr.a4);
developerd0a7d722023-10-19 11:24:25 +0800333 if (unlikely(!neigh)) {
334 CRYPTO_INFO("%s: %s No neigh (daddr=%pI4)\n", __func__, dst->dev->name,
335 &xs->id.daddr.a4);
336 neigh = __neigh_create(&arp_tbl, &xs->id.daddr.a4, dst->dev, false);
developerfba064e2024-01-09 14:52:17 +0800337 neigh_output(neigh, skb, false);
developerd0a7d722023-10-19 11:24:25 +0800338 return NULL;
339 }
340
341 return neigh;
342}
343
developer94c513e2023-08-21 17:33:25 +0800344bool mtk_xfrm_offload_ok(struct sk_buff *skb,
345 struct xfrm_state *xs)
346{
347 struct mtk_xfrm_params *xfrm_params;
developerd0a7d722023-10-19 11:24:25 +0800348 struct neighbour *neigh;
349 struct dst_entry *dst = skb_dst(skb);
developer8ac52a62024-07-01 16:11:43 +0800350 int fill_inner_info = 0;
developerd0a7d722023-10-19 11:24:25 +0800351
352 rcu_read_lock_bh();
353
354 neigh = mtk_crypto_find_dst_mac(skb, xs);
355 if (!neigh) {
356 rcu_read_unlock_bh();
357 return true;
358 }
359
developer8ac52a62024-07-01 16:11:43 +0800360 /*
361 * For packet has pass through VTI (route-based VTI)
362 * The 'dev_queue_xmit' function called at network layer will cause both
363 * skb->mac_header and skb->network_header to point to the IP header
364 */
365 if (skb->mac_header == skb->network_header)
366 fill_inner_info = 1;
367
developerd0a7d722023-10-19 11:24:25 +0800368 skb_push(skb, sizeof(struct ethhdr));
369 skb_reset_mac_header(skb);
370
371 eth_hdr(skb)->h_proto = htons(ETH_P_IP);
372 memcpy(eth_hdr(skb)->h_dest, neigh->ha, ETH_ALEN);
373 memcpy(eth_hdr(skb)->h_source, dst->dev->dev_addr, ETH_ALEN);
374
375 rcu_read_unlock_bh();
376
377 xfrm_params = (struct mtk_xfrm_params *)xs->xso.offload_handle;
developer94c513e2023-08-21 17:33:25 +0800378
developer49489b82024-03-28 15:18:08 +0800379#if IS_ENABLED(CONFIG_NET_MEDIATEK_HNAT)
380 skb_hnat_cdrt(skb) = xfrm_params->cdrt->idx;
developer52e00b72023-09-04 11:13:18 +0800381 /*
382 * EIP197 does not support fragmentation. As a result, we can not bind UDP
383 * flow since it may cause network fail due to fragmentation
384 */
developer49489b82024-03-28 15:18:08 +0800385 if (ra_sw_nat_hook_tx &&
developerc9adb362024-07-05 13:30:41 +0800386 ((is_tops_tunnel(skb) || is_tcp(skb)) && is_hnat_rate_reach(skb)))
developer8ac52a62024-07-01 16:11:43 +0800387 hnat_bind_crypto_entry(skb, dst->dev, fill_inner_info);
developer52e00b72023-09-04 11:13:18 +0800388
developerd0a7d722023-10-19 11:24:25 +0800389 /* Set magic tag for tport setting, reset to 0 after tport is set */
390 skb_hnat_magic_tag(skb) = HNAT_MAGIC_TAG;
developer49489b82024-03-28 15:18:08 +0800391#else
392 skb_tnl_cdrt(skb) = xfrm_params->cdrt->idx;
393 skb_tnl_magic_tag(skb) = TNL_MAGIC_TAG;
394#endif // HNAT
395
396 /* Since we're going to tx directly, set skb->dev to dst->dev */
397 skb->dev = dst->dev;
developer05215d12024-01-04 19:14:19 +0800398
399 /*
400 * Since skb headroom may not be copy when segment, we cannot rely on
401 * headroom data (ex. cdrt) to decide packets should send to EIP197.
402 * Here is a workaround that only skb with inner_protocol = ESP will
403 * be sent to EIP197.
404 */
405 skb->inner_protocol = IPPROTO_ESP;
developerd0a7d722023-10-19 11:24:25 +0800406 /*
407 * Tx packet to EIP197.
408 * To avoid conflict of SW and HW sequence number
409 * All offloadable packets send to EIP197
410 */
411 dev_queue_xmit(skb);
developer94c513e2023-08-21 17:33:25 +0800412
developerd0a7d722023-10-19 11:24:25 +0800413 return true;
developer94c513e2023-08-21 17:33:25 +0800414}