common/cnxk: fix null access in IPv6 flow parsing
[dpdk.git] / drivers / common / cnxk / cnxk_security.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include <rte_udp.h>
6
7 #include "cnxk_security.h"
8
9 #include "roc_api.h"
10
11 static void
12 ipsec_hmac_opad_ipad_gen(struct rte_crypto_sym_xform *auth_xform,
13                          uint8_t *hmac_opad_ipad)
14 {
15         const uint8_t *key = auth_xform->auth.key.data;
16         uint32_t length = auth_xform->auth.key.length;
17         uint8_t opad[128] = {[0 ... 127] = 0x5c};
18         uint8_t ipad[128] = {[0 ... 127] = 0x36};
19         uint32_t i;
20
21         /* HMAC OPAD and IPAD */
22         for (i = 0; i < 127 && i < length; i++) {
23                 opad[i] = opad[i] ^ key[i];
24                 ipad[i] = ipad[i] ^ key[i];
25         }
26
27         /* Precompute hash of HMAC OPAD and IPAD to avoid
28          * per packet computation
29          */
30         switch (auth_xform->auth.algo) {
31         case RTE_CRYPTO_AUTH_SHA1_HMAC:
32                 roc_hash_sha1_gen(opad, (uint32_t *)&hmac_opad_ipad[0]);
33                 roc_hash_sha1_gen(ipad, (uint32_t *)&hmac_opad_ipad[24]);
34                 break;
35         case RTE_CRYPTO_AUTH_SHA256_HMAC:
36                 roc_hash_sha256_gen(opad, (uint32_t *)&hmac_opad_ipad[0]);
37                 roc_hash_sha256_gen(ipad, (uint32_t *)&hmac_opad_ipad[64]);
38                 break;
39         case RTE_CRYPTO_AUTH_SHA384_HMAC:
40                 roc_hash_sha512_gen(opad, (uint64_t *)&hmac_opad_ipad[0], 384);
41                 roc_hash_sha512_gen(ipad, (uint64_t *)&hmac_opad_ipad[64], 384);
42                 break;
43         case RTE_CRYPTO_AUTH_SHA512_HMAC:
44                 roc_hash_sha512_gen(opad, (uint64_t *)&hmac_opad_ipad[0], 512);
45                 roc_hash_sha512_gen(ipad, (uint64_t *)&hmac_opad_ipad[64], 512);
46                 break;
47         default:
48                 break;
49         }
50 }
51
52 static int
53 ot_ipsec_sa_common_param_fill(union roc_ot_ipsec_sa_word2 *w2,
54                               uint8_t *cipher_key, uint8_t *salt_key,
55                               uint8_t *hmac_opad_ipad,
56                               struct rte_security_ipsec_xform *ipsec_xfrm,
57                               struct rte_crypto_sym_xform *crypto_xfrm)
58 {
59         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
60         const uint8_t *key;
61         uint32_t *tmp_salt;
62         uint64_t *tmp_key;
63         int length, i;
64
65         /* Set direction */
66         switch (ipsec_xfrm->direction) {
67         case RTE_SECURITY_IPSEC_SA_DIR_INGRESS:
68                 w2->s.dir = ROC_IE_SA_DIR_INBOUND;
69                 auth_xfrm = crypto_xfrm;
70                 cipher_xfrm = crypto_xfrm->next;
71                 break;
72         case RTE_SECURITY_IPSEC_SA_DIR_EGRESS:
73                 w2->s.dir = ROC_IE_SA_DIR_OUTBOUND;
74                 cipher_xfrm = crypto_xfrm;
75                 auth_xfrm = crypto_xfrm->next;
76                 break;
77         default:
78                 return -EINVAL;
79         }
80
81         /* Set protocol - ESP vs AH */
82         switch (ipsec_xfrm->proto) {
83         case RTE_SECURITY_IPSEC_SA_PROTO_ESP:
84                 w2->s.protocol = ROC_IE_SA_PROTOCOL_ESP;
85                 break;
86         case RTE_SECURITY_IPSEC_SA_PROTO_AH:
87                 w2->s.protocol = ROC_IE_SA_PROTOCOL_AH;
88                 break;
89         default:
90                 return -EINVAL;
91         }
92
93         /* Set mode - transport vs tunnel */
94         switch (ipsec_xfrm->mode) {
95         case RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT:
96                 w2->s.mode = ROC_IE_SA_MODE_TRANSPORT;
97                 break;
98         case RTE_SECURITY_IPSEC_SA_MODE_TUNNEL:
99                 w2->s.mode = ROC_IE_SA_MODE_TUNNEL;
100                 break;
101         default:
102                 return -EINVAL;
103         }
104
105         /* Set encryption algorithm */
106         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
107                 key = crypto_xfrm->aead.key.data;
108                 length = crypto_xfrm->aead.key.length;
109
110                 switch (crypto_xfrm->aead.algo) {
111                 case RTE_CRYPTO_AEAD_AES_GCM:
112                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_GCM;
113                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_NULL;
114                         memcpy(salt_key, &ipsec_xfrm->salt, 4);
115                         tmp_salt = (uint32_t *)salt_key;
116                         *tmp_salt = rte_be_to_cpu_32(*tmp_salt);
117                         break;
118                 default:
119                         return -ENOTSUP;
120                 }
121         } else {
122                 switch (cipher_xfrm->cipher.algo) {
123                 case RTE_CRYPTO_CIPHER_NULL:
124                         w2->s.enc_type = ROC_IE_OT_SA_ENC_NULL;
125                         break;
126                 case RTE_CRYPTO_CIPHER_AES_CBC:
127                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_CBC;
128                         break;
129                 case RTE_CRYPTO_CIPHER_AES_CTR:
130                         w2->s.enc_type = ROC_IE_OT_SA_ENC_AES_CTR;
131                         break;
132                 default:
133                         return -ENOTSUP;
134                 }
135
136                 switch (auth_xfrm->auth.algo) {
137                 case RTE_CRYPTO_AUTH_NULL:
138                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_NULL;
139                         break;
140                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
141                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA1;
142                         break;
143                 case RTE_CRYPTO_AUTH_SHA256_HMAC:
144                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_256;
145                         break;
146                 case RTE_CRYPTO_AUTH_SHA384_HMAC:
147                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_384;
148                         break;
149                 case RTE_CRYPTO_AUTH_SHA512_HMAC:
150                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_SHA2_512;
151                         break;
152                 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
153                         w2->s.auth_type = ROC_IE_OT_SA_AUTH_AES_XCBC_128;
154                         break;
155                 default:
156                         return -ENOTSUP;
157                 }
158
159                 if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_AES_XCBC_MAC) {
160                         const uint8_t *auth_key = auth_xfrm->auth.key.data;
161                         roc_aes_xcbc_key_derive(auth_key, hmac_opad_ipad);
162                 } else {
163                         ipsec_hmac_opad_ipad_gen(auth_xfrm, hmac_opad_ipad);
164                 }
165
166                 tmp_key = (uint64_t *)hmac_opad_ipad;
167                 for (i = 0;
168                      i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t));
169                      i++)
170                         tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
171
172                 key = cipher_xfrm->cipher.key.data;
173                 length = cipher_xfrm->cipher.key.length;
174         }
175
176         /* Set encapsulation type */
177         if (ipsec_xfrm->options.udp_encap)
178                 w2->s.encap_type = ROC_IE_OT_SA_ENCAP_UDP;
179
180         w2->s.spi = ipsec_xfrm->spi;
181
182         /* Copy encryption key */
183         memcpy(cipher_key, key, length);
184         tmp_key = (uint64_t *)cipher_key;
185         for (i = 0; i < (int)(ROC_CTX_MAX_CKEY_LEN / sizeof(uint64_t)); i++)
186                 tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
187
188         /* Set AES key length */
189         if (w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CBC ||
190             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CCM ||
191             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_CTR ||
192             w2->s.enc_type == ROC_IE_OT_SA_ENC_AES_GCM ||
193             w2->s.auth_type == ROC_IE_OT_SA_AUTH_AES_GMAC) {
194                 switch (length) {
195                 case ROC_CPT_AES128_KEY_LEN:
196                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_128;
197                         break;
198                 case ROC_CPT_AES192_KEY_LEN:
199                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_192;
200                         break;
201                 case ROC_CPT_AES256_KEY_LEN:
202                         w2->s.aes_key_len = ROC_IE_SA_AES_KEY_LEN_256;
203                         break;
204                 default:
205                         plt_err("Invalid AES key length");
206                         return -EINVAL;
207                 }
208         }
209
210         if (ipsec_xfrm->life.packets_soft_limit != 0 ||
211             ipsec_xfrm->life.packets_hard_limit != 0) {
212                 if (ipsec_xfrm->life.bytes_soft_limit != 0 ||
213                     ipsec_xfrm->life.bytes_hard_limit != 0) {
214                         plt_err("Expiry tracking with both packets & bytes is not supported");
215                         return -EINVAL;
216                 }
217                 w2->s.life_unit = ROC_IE_OT_SA_LIFE_UNIT_PKTS;
218         }
219
220         if (ipsec_xfrm->life.bytes_soft_limit != 0 ||
221             ipsec_xfrm->life.bytes_hard_limit != 0) {
222                 if (ipsec_xfrm->life.packets_soft_limit != 0 ||
223                     ipsec_xfrm->life.packets_hard_limit != 0) {
224                         plt_err("Expiry tracking with both packets & bytes is not supported");
225                         return -EINVAL;
226                 }
227                 w2->s.life_unit = ROC_IE_OT_SA_LIFE_UNIT_OCTETS;
228         }
229
230         return 0;
231 }
232
233 static size_t
234 ot_ipsec_inb_ctx_size(struct roc_ot_ipsec_inb_sa *sa)
235 {
236         size_t size;
237
238         /* Variable based on Anti-replay Window */
239         size = offsetof(struct roc_ot_ipsec_inb_sa, ctx) +
240                offsetof(struct roc_ot_ipsec_inb_ctx_update_reg, ar_winbits);
241
242         if (sa->w0.s.ar_win)
243                 size += (1 << (sa->w0.s.ar_win - 1)) * sizeof(uint64_t);
244
245         return size;
246 }
247
248 static int
249 ot_ipsec_inb_tunnel_hdr_fill(struct roc_ot_ipsec_inb_sa *sa,
250                              struct rte_security_ipsec_xform *ipsec_xfrm)
251 {
252         struct rte_security_ipsec_tunnel_param *tunnel;
253
254         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
255                 return 0;
256
257         if (ipsec_xfrm->options.tunnel_hdr_verify == 0)
258                 return 0;
259
260         tunnel = &ipsec_xfrm->tunnel;
261
262         switch (tunnel->type) {
263         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
264                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
265                 memcpy(&sa->outer_hdr.ipv4.src_addr, &tunnel->ipv4.src_ip,
266                        sizeof(struct in_addr));
267                 memcpy(&sa->outer_hdr.ipv4.dst_addr, &tunnel->ipv4.dst_ip,
268                        sizeof(struct in_addr));
269
270                 /* IP Source and Dest are in LE/CPU endian */
271                 sa->outer_hdr.ipv4.src_addr =
272                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.src_addr);
273                 sa->outer_hdr.ipv4.dst_addr =
274                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.dst_addr);
275
276                 break;
277         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
278                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_6;
279                 memcpy(&sa->outer_hdr.ipv6.src_addr, &tunnel->ipv6.src_addr,
280                        sizeof(struct in6_addr));
281                 memcpy(&sa->outer_hdr.ipv6.dst_addr, &tunnel->ipv6.dst_addr,
282                        sizeof(struct in6_addr));
283
284                 break;
285         default:
286                 return -EINVAL;
287         }
288
289         switch (ipsec_xfrm->options.tunnel_hdr_verify) {
290         case RTE_SECURITY_IPSEC_TUNNEL_VERIFY_DST_ADDR:
291                 sa->w2.s.ip_hdr_verify = ROC_IE_OT_SA_IP_HDR_VERIFY_DST_ADDR;
292                 break;
293         case RTE_SECURITY_IPSEC_TUNNEL_VERIFY_SRC_DST_ADDR:
294                 sa->w2.s.ip_hdr_verify =
295                         ROC_IE_OT_SA_IP_HDR_VERIFY_SRC_DST_ADDR;
296                 break;
297         default:
298                 return -ENOTSUP;
299         }
300
301         return 0;
302 }
303
304 int
305 cnxk_ot_ipsec_inb_sa_fill(struct roc_ot_ipsec_inb_sa *sa,
306                           struct rte_security_ipsec_xform *ipsec_xfrm,
307                           struct rte_crypto_sym_xform *crypto_xfrm)
308 {
309         union roc_ot_ipsec_sa_word2 w2;
310         uint32_t replay_win_sz;
311         size_t offset;
312         int rc;
313
314         w2.u64 = 0;
315         rc = ot_ipsec_sa_common_param_fill(&w2, sa->cipher_key, sa->w8.s.salt,
316                                            sa->hmac_opad_ipad, ipsec_xfrm,
317                                            crypto_xfrm);
318         if (rc)
319                 return rc;
320
321         /* Updata common word2 data */
322         sa->w2.u64 = w2.u64;
323
324         /* Only support power-of-two window sizes supported */
325         replay_win_sz = ipsec_xfrm->replay_win_sz;
326         if (replay_win_sz) {
327                 if (!rte_is_power_of_2(replay_win_sz) ||
328                     replay_win_sz > ROC_AR_WIN_SIZE_MAX)
329                         return -ENOTSUP;
330
331                 sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
332         }
333
334         rc = ot_ipsec_inb_tunnel_hdr_fill(sa, ipsec_xfrm);
335         if (rc)
336                 return rc;
337
338         /* Default options for pkt_out and pkt_fmt are with
339          * second pass meta and no defrag.
340          */
341         sa->w0.s.pkt_format = ROC_IE_OT_SA_PKT_FMT_META;
342         sa->w0.s.pkt_output = ROC_IE_OT_SA_PKT_OUTPUT_HW_BASED_DEFRAG;
343         sa->w0.s.pkind = ROC_OT_CPT_META_PKIND;
344
345         /* ESN */
346         sa->w2.s.esn_en = !!ipsec_xfrm->options.esn;
347         if (ipsec_xfrm->options.udp_encap) {
348                 sa->w10.s.udp_src_port = 4500;
349                 sa->w10.s.udp_dst_port = 4500;
350         }
351
352         if (ipsec_xfrm->options.udp_ports_verify)
353                 sa->w2.s.udp_ports_verify = 1;
354
355         offset = offsetof(struct roc_ot_ipsec_inb_sa, ctx);
356         /* Word offset for HW managed SA field */
357         sa->w0.s.hw_ctx_off = offset / 8;
358         /* Context push size for inbound spans up to hw_ctx including
359          * ar_base field, in 8b units
360          */
361         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off + 1;
362         /* Entire context size in 128B units */
363         sa->w0.s.ctx_size =
364                 (PLT_ALIGN_CEIL(ot_ipsec_inb_ctx_size(sa), ROC_CTX_UNIT_128B) /
365                  ROC_CTX_UNIT_128B) -
366                 1;
367
368         /**
369          * CPT MC triggers expiry when counter value changes from 2 to 1. To
370          * mitigate this behaviour add 1 to the life counter values provided.
371          */
372
373         if (ipsec_xfrm->life.bytes_soft_limit) {
374                 sa->ctx.soft_life = ipsec_xfrm->life.bytes_soft_limit + 1;
375                 sa->w0.s.soft_life_dec = 1;
376         }
377
378         if (ipsec_xfrm->life.packets_soft_limit) {
379                 sa->ctx.soft_life = ipsec_xfrm->life.packets_soft_limit + 1;
380                 sa->w0.s.soft_life_dec = 1;
381         }
382
383         if (ipsec_xfrm->life.bytes_hard_limit) {
384                 sa->ctx.hard_life = ipsec_xfrm->life.bytes_hard_limit + 1;
385                 sa->w0.s.hard_life_dec = 1;
386         }
387
388         if (ipsec_xfrm->life.packets_hard_limit) {
389                 sa->ctx.hard_life = ipsec_xfrm->life.packets_hard_limit + 1;
390                 sa->w0.s.hard_life_dec = 1;
391         }
392
393         /* There are two words of CPT_CTX_HW_S for ucode to skip */
394         sa->w0.s.ctx_hdr_size = 1;
395         sa->w0.s.aop_valid = 1;
396         sa->w0.s.et_ovrwr = 1;
397
398         rte_wmb();
399
400         /* Enable SA */
401         sa->w2.s.valid = 1;
402         return 0;
403 }
404
405 int
406 cnxk_ot_ipsec_outb_sa_fill(struct roc_ot_ipsec_outb_sa *sa,
407                            struct rte_security_ipsec_xform *ipsec_xfrm,
408                            struct rte_crypto_sym_xform *crypto_xfrm)
409 {
410         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
411         union roc_ot_ipsec_sa_word2 w2;
412         size_t offset;
413         int rc;
414
415         w2.u64 = 0;
416         rc = ot_ipsec_sa_common_param_fill(&w2, sa->cipher_key, sa->iv.s.salt,
417                                            sa->hmac_opad_ipad, ipsec_xfrm,
418                                            crypto_xfrm);
419         if (rc)
420                 return rc;
421
422         /* Update common word2 data */
423         sa->w2.u64 = w2.u64;
424
425         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
426                 goto skip_tunnel_info;
427
428         /* Tunnel header info */
429         switch (tunnel->type) {
430         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
431                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_4;
432                 memcpy(&sa->outer_hdr.ipv4.src_addr, &tunnel->ipv4.src_ip,
433                        sizeof(struct in_addr));
434                 memcpy(&sa->outer_hdr.ipv4.dst_addr, &tunnel->ipv4.dst_ip,
435                        sizeof(struct in_addr));
436
437                 /* IP Source and Dest seems to be in LE/CPU endian */
438                 sa->outer_hdr.ipv4.src_addr =
439                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.src_addr);
440                 sa->outer_hdr.ipv4.dst_addr =
441                         rte_be_to_cpu_32(sa->outer_hdr.ipv4.dst_addr);
442
443                 /* Outer header DF bit source */
444                 if (!ipsec_xfrm->options.copy_df) {
445                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
446                                 ROC_IE_OT_SA_COPY_FROM_SA;
447                         sa->w10.s.ipv4_df_or_ipv6_flw_lbl = tunnel->ipv4.df;
448                 } else {
449                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
450                                 ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
451                 }
452
453                 /* Outer header DSCP source */
454                 if (!ipsec_xfrm->options.copy_dscp) {
455                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_SA;
456                         sa->w10.s.dscp = tunnel->ipv4.dscp;
457                 } else {
458                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
459                 }
460                 break;
461         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
462                 sa->w2.s.outer_ip_ver = ROC_IE_SA_IP_VERSION_6;
463                 memcpy(&sa->outer_hdr.ipv6.src_addr, &tunnel->ipv6.src_addr,
464                        sizeof(struct in6_addr));
465                 memcpy(&sa->outer_hdr.ipv6.dst_addr, &tunnel->ipv6.dst_addr,
466                        sizeof(struct in6_addr));
467
468                 /* Outer header flow label source */
469                 if (!ipsec_xfrm->options.copy_flabel) {
470                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
471                                 ROC_IE_OT_SA_COPY_FROM_SA;
472
473                         sa->w10.s.ipv4_df_or_ipv6_flw_lbl = tunnel->ipv6.flabel;
474                 } else {
475                         sa->w2.s.ipv4_df_src_or_ipv6_flw_lbl_src =
476                                 ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
477                 }
478
479                 /* Outer header DSCP source */
480                 if (!ipsec_xfrm->options.copy_dscp) {
481                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_SA;
482                         sa->w10.s.dscp = tunnel->ipv6.dscp;
483                 } else {
484                         sa->w2.s.dscp_src = ROC_IE_OT_SA_COPY_FROM_INNER_IP_HDR;
485                 }
486                 break;
487         default:
488                 return -EINVAL;
489         }
490
491 skip_tunnel_info:
492         /* ESN */
493         sa->w0.s.esn_en = !!ipsec_xfrm->options.esn;
494
495         if (ipsec_xfrm->esn.value)
496                 sa->ctx.esn_val = ipsec_xfrm->esn.value - 1;
497
498         if (ipsec_xfrm->options.udp_encap) {
499                 sa->w10.s.udp_src_port = 4500;
500                 sa->w10.s.udp_dst_port = 4500;
501         }
502
503         offset = offsetof(struct roc_ot_ipsec_outb_sa, ctx);
504         /* Word offset for HW managed SA field */
505         sa->w0.s.hw_ctx_off = offset / 8;
506
507         /* Context push size is up to err ctl in HW ctx */
508         sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off + 1;
509
510         /* Entire context size in 128B units */
511         offset = sizeof(struct roc_ot_ipsec_outb_sa);
512         sa->w0.s.ctx_size = (PLT_ALIGN_CEIL(offset, ROC_CTX_UNIT_128B) /
513                              ROC_CTX_UNIT_128B) -
514                             1;
515
516         /* IPID gen */
517         sa->w2.s.ipid_gen = 1;
518
519         /**
520          * CPT MC triggers expiry when counter value changes from 2 to 1. To
521          * mitigate this behaviour add 1 to the life counter values provided.
522          */
523
524         if (ipsec_xfrm->life.bytes_soft_limit) {
525                 sa->ctx.soft_life = ipsec_xfrm->life.bytes_soft_limit + 1;
526                 sa->w0.s.soft_life_dec = 1;
527         }
528
529         if (ipsec_xfrm->life.packets_soft_limit) {
530                 sa->ctx.soft_life = ipsec_xfrm->life.packets_soft_limit + 1;
531                 sa->w0.s.soft_life_dec = 1;
532         }
533
534         if (ipsec_xfrm->life.bytes_hard_limit) {
535                 sa->ctx.hard_life = ipsec_xfrm->life.bytes_hard_limit + 1;
536                 sa->w0.s.hard_life_dec = 1;
537         }
538
539         if (ipsec_xfrm->life.packets_hard_limit) {
540                 sa->ctx.hard_life = ipsec_xfrm->life.packets_hard_limit + 1;
541                 sa->w0.s.hard_life_dec = 1;
542         }
543
544         /* There are two words of CPT_CTX_HW_S for ucode to skip */
545         sa->w0.s.ctx_hdr_size = 1;
546         sa->w0.s.aop_valid = 1;
547
548         rte_wmb();
549
550         /* Enable SA */
551         sa->w2.s.valid = 1;
552         return 0;
553 }
554
555 bool
556 cnxk_ot_ipsec_inb_sa_valid(struct roc_ot_ipsec_inb_sa *sa)
557 {
558         return !!sa->w2.s.valid;
559 }
560
561 bool
562 cnxk_ot_ipsec_outb_sa_valid(struct roc_ot_ipsec_outb_sa *sa)
563 {
564         return !!sa->w2.s.valid;
565 }
566
567 static inline int
568 ipsec_xfrm_verify(struct rte_security_ipsec_xform *ipsec_xfrm,
569                   struct rte_crypto_sym_xform *crypto_xfrm)
570 {
571         if (crypto_xfrm->next == NULL)
572                 return -EINVAL;
573
574         if (ipsec_xfrm->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
575                 if (crypto_xfrm->type != RTE_CRYPTO_SYM_XFORM_AUTH ||
576                     crypto_xfrm->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER)
577                         return -EINVAL;
578         } else {
579                 if (crypto_xfrm->type != RTE_CRYPTO_SYM_XFORM_CIPHER ||
580                     crypto_xfrm->next->type != RTE_CRYPTO_SYM_XFORM_AUTH)
581                         return -EINVAL;
582         }
583
584         return 0;
585 }
586
587 static int
588 onf_ipsec_sa_common_param_fill(struct roc_ie_onf_sa_ctl *ctl, uint8_t *salt,
589                                uint8_t *cipher_key, uint8_t *hmac_opad_ipad,
590                                struct rte_security_ipsec_xform *ipsec_xfrm,
591                                struct rte_crypto_sym_xform *crypto_xfrm)
592 {
593         struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
594         int rc, length, auth_key_len;
595         const uint8_t *key = NULL;
596
597         /* Set direction */
598         switch (ipsec_xfrm->direction) {
599         case RTE_SECURITY_IPSEC_SA_DIR_INGRESS:
600                 ctl->direction = ROC_IE_SA_DIR_INBOUND;
601                 auth_xfrm = crypto_xfrm;
602                 cipher_xfrm = crypto_xfrm->next;
603                 break;
604         case RTE_SECURITY_IPSEC_SA_DIR_EGRESS:
605                 ctl->direction = ROC_IE_SA_DIR_OUTBOUND;
606                 cipher_xfrm = crypto_xfrm;
607                 auth_xfrm = crypto_xfrm->next;
608                 break;
609         default:
610                 return -EINVAL;
611         }
612
613         /* Set protocol - ESP vs AH */
614         switch (ipsec_xfrm->proto) {
615         case RTE_SECURITY_IPSEC_SA_PROTO_ESP:
616                 ctl->ipsec_proto = ROC_IE_SA_PROTOCOL_ESP;
617                 break;
618         case RTE_SECURITY_IPSEC_SA_PROTO_AH:
619                 return -ENOTSUP;
620         default:
621                 return -EINVAL;
622         }
623
624         /* Set mode - transport vs tunnel */
625         switch (ipsec_xfrm->mode) {
626         case RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT:
627                 ctl->ipsec_mode = ROC_IE_SA_MODE_TRANSPORT;
628                 break;
629         case RTE_SECURITY_IPSEC_SA_MODE_TUNNEL:
630                 ctl->ipsec_mode = ROC_IE_SA_MODE_TUNNEL;
631                 break;
632         default:
633                 return -EINVAL;
634         }
635
636         /* Set encryption algorithm */
637         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
638                 length = crypto_xfrm->aead.key.length;
639
640                 switch (crypto_xfrm->aead.algo) {
641                 case RTE_CRYPTO_AEAD_AES_GCM:
642                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_GCM;
643                         ctl->auth_type = ROC_IE_ON_SA_AUTH_NULL;
644                         memcpy(salt, &ipsec_xfrm->salt, 4);
645                         key = crypto_xfrm->aead.key.data;
646                         break;
647                 default:
648                         return -ENOTSUP;
649                 }
650
651         } else {
652                 rc = ipsec_xfrm_verify(ipsec_xfrm, crypto_xfrm);
653                 if (rc)
654                         return rc;
655
656                 switch (cipher_xfrm->cipher.algo) {
657                 case RTE_CRYPTO_CIPHER_AES_CBC:
658                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CBC;
659                         break;
660                 case RTE_CRYPTO_CIPHER_AES_CTR:
661                         ctl->enc_type = ROC_IE_ON_SA_ENC_AES_CTR;
662                         break;
663                 default:
664                         return -ENOTSUP;
665                 }
666
667                 switch (auth_xfrm->auth.algo) {
668                 case RTE_CRYPTO_AUTH_SHA1_HMAC:
669                         ctl->auth_type = ROC_IE_ON_SA_AUTH_SHA1;
670                         break;
671                 default:
672                         return -ENOTSUP;
673                 }
674                 auth_key_len = auth_xfrm->auth.key.length;
675                 if (auth_key_len < 20 || auth_key_len > 64)
676                         return -ENOTSUP;
677
678                 key = cipher_xfrm->cipher.key.data;
679                 length = cipher_xfrm->cipher.key.length;
680
681                 ipsec_hmac_opad_ipad_gen(auth_xfrm, hmac_opad_ipad);
682         }
683
684         switch (length) {
685         case ROC_CPT_AES128_KEY_LEN:
686                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_128;
687                 break;
688         case ROC_CPT_AES192_KEY_LEN:
689                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_192;
690                 break;
691         case ROC_CPT_AES256_KEY_LEN:
692                 ctl->aes_key_len = ROC_IE_SA_AES_KEY_LEN_256;
693                 break;
694         default:
695                 return -EINVAL;
696         }
697
698         memcpy(cipher_key, key, length);
699
700         if (ipsec_xfrm->options.esn)
701                 ctl->esn_en = 1;
702
703         ctl->spi = rte_cpu_to_be_32(ipsec_xfrm->spi);
704         return 0;
705 }
706
707 int
708 cnxk_onf_ipsec_inb_sa_fill(struct roc_onf_ipsec_inb_sa *sa,
709                            struct rte_security_ipsec_xform *ipsec_xfrm,
710                            struct rte_crypto_sym_xform *crypto_xfrm)
711 {
712         struct roc_ie_onf_sa_ctl *ctl = &sa->ctl;
713         int rc;
714
715         rc = onf_ipsec_sa_common_param_fill(ctl, sa->nonce, sa->cipher_key,
716                                             sa->hmac_key, ipsec_xfrm,
717                                             crypto_xfrm);
718         if (rc)
719                 return rc;
720
721         rte_wmb();
722
723         /* Enable SA */
724         ctl->valid = 1;
725         return 0;
726 }
727
728 int
729 cnxk_onf_ipsec_outb_sa_fill(struct roc_onf_ipsec_outb_sa *sa,
730                             struct rte_security_ipsec_xform *ipsec_xfrm,
731                             struct rte_crypto_sym_xform *crypto_xfrm)
732 {
733         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
734         struct roc_ie_onf_sa_ctl *ctl = &sa->ctl;
735         int rc;
736
737         /* Fill common params */
738         rc = onf_ipsec_sa_common_param_fill(ctl, sa->nonce, sa->cipher_key,
739                                             sa->hmac_key, ipsec_xfrm,
740                                             crypto_xfrm);
741         if (rc)
742                 return rc;
743
744         if (ipsec_xfrm->mode != RTE_SECURITY_IPSEC_SA_MODE_TUNNEL)
745                 goto skip_tunnel_info;
746
747         /* Tunnel header info */
748         switch (tunnel->type) {
749         case RTE_SECURITY_IPSEC_TUNNEL_IPV4:
750                 memcpy(&sa->ip_src, &tunnel->ipv4.src_ip,
751                        sizeof(struct in_addr));
752                 memcpy(&sa->ip_dst, &tunnel->ipv4.dst_ip,
753                        sizeof(struct in_addr));
754                 break;
755         case RTE_SECURITY_IPSEC_TUNNEL_IPV6:
756                 return -ENOTSUP;
757         default:
758                 return -EINVAL;
759         }
760
761         /* Update udp encap ports */
762         if (ipsec_xfrm->options.udp_encap == 1) {
763                 sa->udp_src = 4500;
764                 sa->udp_dst = 4500;
765         }
766
767 skip_tunnel_info:
768         rte_wmb();
769
770         /* Enable SA */
771         ctl->valid = 1;
772         return 0;
773 }
774
775 bool
776 cnxk_onf_ipsec_inb_sa_valid(struct roc_onf_ipsec_inb_sa *sa)
777 {
778         return !!sa->ctl.valid;
779 }
780
781 bool
782 cnxk_onf_ipsec_outb_sa_valid(struct roc_onf_ipsec_outb_sa *sa)
783 {
784         return !!sa->ctl.valid;
785 }
786
787 uint8_t
788 cnxk_ipsec_ivlen_get(enum rte_crypto_cipher_algorithm c_algo,
789                      enum rte_crypto_auth_algorithm a_algo,
790                      enum rte_crypto_aead_algorithm aead_algo)
791 {
792         uint8_t ivlen = 0;
793
794         if (aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
795                 ivlen = 8;
796
797         switch (c_algo) {
798         case RTE_CRYPTO_CIPHER_AES_CTR:
799                 ivlen = 8;
800                 break;
801         case RTE_CRYPTO_CIPHER_3DES_CBC:
802                 ivlen = ROC_CPT_DES_BLOCK_LENGTH;
803                 break;
804         case RTE_CRYPTO_CIPHER_AES_CBC:
805                 ivlen = ROC_CPT_AES_BLOCK_LENGTH;
806                 break;
807         default:
808                 break;
809         }
810
811         switch (a_algo) {
812         case RTE_CRYPTO_AUTH_AES_GMAC:
813                 ivlen = 8;
814                 break;
815         default:
816                 break;
817         }
818
819         return ivlen;
820 }
821
822 uint8_t
823 cnxk_ipsec_icvlen_get(enum rte_crypto_cipher_algorithm c_algo,
824                       enum rte_crypto_auth_algorithm a_algo,
825                       enum rte_crypto_aead_algorithm aead_algo)
826 {
827         uint8_t icv = 0;
828
829         (void)c_algo;
830
831         switch (a_algo) {
832         case RTE_CRYPTO_AUTH_NULL:
833                 icv = 0;
834                 break;
835         case RTE_CRYPTO_AUTH_SHA1_HMAC:
836                 icv = 12;
837                 break;
838         case RTE_CRYPTO_AUTH_SHA256_HMAC:
839         case RTE_CRYPTO_AUTH_AES_GMAC:
840                 icv = 16;
841                 break;
842         case RTE_CRYPTO_AUTH_SHA384_HMAC:
843                 icv = 24;
844                 break;
845         case RTE_CRYPTO_AUTH_SHA512_HMAC:
846                 icv = 32;
847                 break;
848         case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
849                 icv = 12;
850                 break;
851         default:
852                 break;
853         }
854
855         switch (aead_algo) {
856         case RTE_CRYPTO_AEAD_AES_GCM:
857                 icv = 16;
858                 break;
859         default:
860                 break;
861         }
862
863         return icv;
864 }
865
866 uint8_t
867 cnxk_ipsec_outb_roundup_byte(enum rte_crypto_cipher_algorithm c_algo,
868                              enum rte_crypto_aead_algorithm aead_algo)
869 {
870         uint8_t roundup_byte = 4;
871
872         if (aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
873                 return roundup_byte;
874
875         switch (c_algo) {
876         case RTE_CRYPTO_CIPHER_AES_CTR:
877                 roundup_byte = 4;
878                 break;
879         case RTE_CRYPTO_CIPHER_AES_CBC:
880                 roundup_byte = 16;
881                 break;
882         case RTE_CRYPTO_CIPHER_3DES_CBC:
883                 roundup_byte = 8;
884                 break;
885         case RTE_CRYPTO_CIPHER_NULL:
886                 roundup_byte = 4;
887                 break;
888         default:
889                 break;
890         }
891
892         return roundup_byte;
893 }
894
895 int
896 cnxk_ipsec_outb_rlens_get(struct cnxk_ipsec_outb_rlens *rlens,
897                           struct rte_security_ipsec_xform *ipsec_xfrm,
898                           struct rte_crypto_sym_xform *crypto_xfrm)
899 {
900         struct rte_security_ipsec_tunnel_param *tunnel = &ipsec_xfrm->tunnel;
901         enum rte_crypto_cipher_algorithm c_algo = RTE_CRYPTO_CIPHER_NULL;
902         enum rte_crypto_auth_algorithm a_algo = RTE_CRYPTO_AUTH_NULL;
903         enum rte_crypto_aead_algorithm aead_algo = 0;
904         uint16_t partial_len = 0;
905         uint8_t roundup_byte = 0;
906         int8_t roundup_len = 0;
907
908         memset(rlens, 0, sizeof(struct cnxk_ipsec_outb_rlens));
909
910         /* Get Cipher and Auth algo */
911         if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
912                 aead_algo = crypto_xfrm->aead.algo;
913         } else {
914                 if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
915                         c_algo = crypto_xfrm->cipher.algo;
916                 else
917                         a_algo = crypto_xfrm->auth.algo;
918
919                 if (crypto_xfrm->next) {
920                         if (crypto_xfrm->next->type ==
921                             RTE_CRYPTO_SYM_XFORM_CIPHER)
922                                 c_algo = crypto_xfrm->next->cipher.algo;
923                         else
924                                 a_algo = crypto_xfrm->next->auth.algo;
925                 }
926         }
927
928         if (ipsec_xfrm->proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP) {
929                 partial_len = ROC_CPT_ESP_HDR_LEN;
930                 roundup_len = ROC_CPT_ESP_TRL_LEN;
931         } else {
932                 partial_len = ROC_CPT_AH_HDR_LEN;
933         }
934
935         if (ipsec_xfrm->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
936                 if (tunnel->type == RTE_SECURITY_IPSEC_TUNNEL_IPV4)
937                         partial_len += ROC_CPT_TUNNEL_IPV4_HDR_LEN;
938                 else
939                         partial_len += ROC_CPT_TUNNEL_IPV6_HDR_LEN;
940         }
941
942         partial_len += cnxk_ipsec_ivlen_get(c_algo, a_algo, aead_algo);
943         partial_len += cnxk_ipsec_icvlen_get(c_algo, a_algo, aead_algo);
944         roundup_byte = cnxk_ipsec_outb_roundup_byte(c_algo, aead_algo);
945
946         if (ipsec_xfrm->options.udp_encap)
947                 partial_len += sizeof(struct rte_udp_hdr);
948
949         rlens->partial_len = partial_len;
950         rlens->roundup_len = roundup_len;
951         rlens->roundup_byte = roundup_byte;
952         rlens->max_extended_len = partial_len + roundup_len + roundup_byte;
953         return 0;
954 }