1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_cryptodev.h>
12 #include "ipsec_sqn.h"
18 #define MBUF_MAX_L2_LEN RTE_LEN2MASK(RTE_MBUF_L2_LEN_BITS, uint64_t)
19 #define MBUF_MAX_L3_LEN RTE_LEN2MASK(RTE_MBUF_L3_LEN_BITS, uint64_t)
21 /* some helper structures */
23 struct rte_crypto_auth_xform *auth;
24 struct rte_crypto_cipher_xform *cipher;
25 struct rte_crypto_aead_xform *aead;
29 * helper routine, fills internal crypto_xform structure.
32 fill_crypto_xform(struct crypto_xform *xform, uint64_t type,
33 const struct rte_ipsec_sa_prm *prm)
35 struct rte_crypto_sym_xform *xf, *xfn;
37 memset(xform, 0, sizeof(*xform));
39 xf = prm->crypto_xform;
45 /* for AEAD just one xform required */
46 if (xf->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
49 xform->aead = &xf->aead;
51 * CIPHER+AUTH xforms are expected in strict order,
52 * depending on SA direction:
53 * inbound: AUTH+CIPHER
54 * outbound: CIPHER+AUTH
56 } else if ((type & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) {
58 /* wrong order or no cipher */
59 if (xfn == NULL || xf->type != RTE_CRYPTO_SYM_XFORM_AUTH ||
60 xfn->type != RTE_CRYPTO_SYM_XFORM_CIPHER)
63 xform->auth = &xf->auth;
64 xform->cipher = &xfn->cipher;
68 /* wrong order or no auth */
69 if (xfn == NULL || xf->type != RTE_CRYPTO_SYM_XFORM_CIPHER ||
70 xfn->type != RTE_CRYPTO_SYM_XFORM_AUTH)
73 xform->cipher = &xf->cipher;
74 xform->auth = &xfn->auth;
80 uint64_t __rte_experimental
81 rte_ipsec_sa_type(const struct rte_ipsec_sa *sa)
87 * Based on number of buckets calculated required size for the
88 * structure that holds replay window and sequence number (RSN) information.
91 rsn_size(uint32_t nb_bucket)
94 struct replay_sqn *rsn;
96 sz = sizeof(*rsn) + nb_bucket * sizeof(rsn->window[0]);
97 sz = RTE_ALIGN_CEIL(sz, RTE_CACHE_LINE_SIZE);
102 * for given size, calculate required number of buckets.
105 replay_num_bucket(uint32_t wsz)
109 nb = rte_align32pow2(RTE_ALIGN_MUL_CEIL(wsz, WINDOW_BUCKET_SIZE) /
111 nb = RTE_MAX(nb, (uint32_t)WINDOW_BUCKET_MIN);
117 ipsec_sa_size(uint64_t type, uint32_t *wnd_sz, uint32_t *nb_bucket)
124 if ((type & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) {
127 * RFC 4303 recommends 64 as minimum window size.
128 * there is no point to use ESN mode without SQN window,
129 * so make sure we have at least 64 window when ESN is enalbed.
131 wsz = ((type & RTE_IPSEC_SATP_ESN_MASK) ==
132 RTE_IPSEC_SATP_ESN_DISABLE) ?
133 wsz : RTE_MAX(wsz, (uint32_t)WINDOW_BUCKET_SIZE);
135 n = replay_num_bucket(wsz);
138 if (n > WINDOW_BUCKET_MAX)
145 if ((type & RTE_IPSEC_SATP_SQN_MASK) == RTE_IPSEC_SATP_SQN_ATOM)
146 sz *= REPLAY_SQN_NUM;
148 sz += sizeof(struct rte_ipsec_sa);
152 void __rte_experimental
153 rte_ipsec_sa_fini(struct rte_ipsec_sa *sa)
155 memset(sa, 0, sa->size);
159 * Determine expected SA type based on input parameters.
162 fill_sa_type(const struct rte_ipsec_sa_prm *prm, uint64_t *type)
168 if (prm->ipsec_xform.proto == RTE_SECURITY_IPSEC_SA_PROTO_AH)
169 tp |= RTE_IPSEC_SATP_PROTO_AH;
170 else if (prm->ipsec_xform.proto == RTE_SECURITY_IPSEC_SA_PROTO_ESP)
171 tp |= RTE_IPSEC_SATP_PROTO_ESP;
175 if (prm->ipsec_xform.direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS)
176 tp |= RTE_IPSEC_SATP_DIR_OB;
177 else if (prm->ipsec_xform.direction ==
178 RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
179 tp |= RTE_IPSEC_SATP_DIR_IB;
183 if (prm->ipsec_xform.mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
184 if (prm->ipsec_xform.tunnel.type ==
185 RTE_SECURITY_IPSEC_TUNNEL_IPV4)
186 tp |= RTE_IPSEC_SATP_MODE_TUNLV4;
187 else if (prm->ipsec_xform.tunnel.type ==
188 RTE_SECURITY_IPSEC_TUNNEL_IPV6)
189 tp |= RTE_IPSEC_SATP_MODE_TUNLV6;
193 if (prm->tun.next_proto == IPPROTO_IPIP)
194 tp |= RTE_IPSEC_SATP_IPV4;
195 else if (prm->tun.next_proto == IPPROTO_IPV6)
196 tp |= RTE_IPSEC_SATP_IPV6;
199 } else if (prm->ipsec_xform.mode ==
200 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT) {
201 tp |= RTE_IPSEC_SATP_MODE_TRANS;
202 if (prm->trs.proto == IPPROTO_IPIP)
203 tp |= RTE_IPSEC_SATP_IPV4;
204 else if (prm->trs.proto == IPPROTO_IPV6)
205 tp |= RTE_IPSEC_SATP_IPV6;
211 /* check for ESN flag */
212 if (prm->ipsec_xform.options.esn == 0)
213 tp |= RTE_IPSEC_SATP_ESN_DISABLE;
215 tp |= RTE_IPSEC_SATP_ESN_ENABLE;
217 /* interpret flags */
218 if (prm->flags & RTE_IPSEC_SAFLAG_SQN_ATOM)
219 tp |= RTE_IPSEC_SATP_SQN_ATOM;
221 tp |= RTE_IPSEC_SATP_SQN_RAW;
228 * Init ESP inbound specific things.
231 esp_inb_init(struct rte_ipsec_sa *sa)
233 /* these params may differ with new algorithms support */
234 sa->ctp.auth.offset = 0;
235 sa->ctp.auth.length = sa->icv_len - sa->sqh_len;
236 sa->ctp.cipher.offset = sizeof(struct rte_esp_hdr) + sa->iv_len;
237 sa->ctp.cipher.length = sa->icv_len + sa->ctp.cipher.offset;
241 * Init ESP inbound tunnel specific things.
244 esp_inb_tun_init(struct rte_ipsec_sa *sa, const struct rte_ipsec_sa_prm *prm)
246 sa->proto = prm->tun.next_proto;
251 * Init ESP outbound specific things.
254 esp_outb_init(struct rte_ipsec_sa *sa, uint32_t hlen)
258 sa->sqn.outb.raw = 1;
260 /* these params may differ with new algorithms support */
261 sa->ctp.auth.offset = hlen;
262 sa->ctp.auth.length = sizeof(struct rte_esp_hdr) +
263 sa->iv_len + sa->sqh_len;
265 algo_type = sa->algo_type;
268 case ALGO_TYPE_AES_GCM:
269 case ALGO_TYPE_AES_CTR:
271 sa->ctp.cipher.offset = hlen + sizeof(struct rte_esp_hdr) +
273 sa->ctp.cipher.length = 0;
275 case ALGO_TYPE_AES_CBC:
276 case ALGO_TYPE_3DES_CBC:
277 sa->ctp.cipher.offset = sa->hdr_len +
278 sizeof(struct rte_esp_hdr);
279 sa->ctp.cipher.length = sa->iv_len;
285 * Init ESP outbound tunnel specific things.
288 esp_outb_tun_init(struct rte_ipsec_sa *sa, const struct rte_ipsec_sa_prm *prm)
290 sa->proto = prm->tun.next_proto;
291 sa->hdr_len = prm->tun.hdr_len;
292 sa->hdr_l3_off = prm->tun.hdr_l3_off;
294 /* update l2_len and l3_len fields for outbound mbuf */
295 sa->tx_offload.val = rte_mbuf_tx_offload(sa->hdr_l3_off,
296 sa->hdr_len - sa->hdr_l3_off, 0, 0, 0, 0, 0);
298 memcpy(sa->hdr, prm->tun.hdr, sa->hdr_len);
300 esp_outb_init(sa, sa->hdr_len);
304 * helper function, init SA structure.
307 esp_sa_init(struct rte_ipsec_sa *sa, const struct rte_ipsec_sa_prm *prm,
308 const struct crypto_xform *cxf)
310 static const uint64_t msk = RTE_IPSEC_SATP_DIR_MASK |
311 RTE_IPSEC_SATP_MODE_MASK;
313 if (cxf->aead != NULL) {
314 switch (cxf->aead->algo) {
315 case RTE_CRYPTO_AEAD_AES_GCM:
317 sa->aad_len = sizeof(struct aead_gcm_aad);
318 sa->icv_len = cxf->aead->digest_length;
319 sa->iv_ofs = cxf->aead->iv.offset;
320 sa->iv_len = sizeof(uint64_t);
321 sa->pad_align = IPSEC_PAD_AES_GCM;
322 sa->algo_type = ALGO_TYPE_AES_GCM;
328 sa->icv_len = cxf->auth->digest_length;
329 sa->iv_ofs = cxf->cipher->iv.offset;
330 sa->sqh_len = IS_ESN(sa) ? sizeof(uint32_t) : 0;
332 switch (cxf->cipher->algo) {
333 case RTE_CRYPTO_CIPHER_NULL:
334 sa->pad_align = IPSEC_PAD_NULL;
336 sa->algo_type = ALGO_TYPE_NULL;
339 case RTE_CRYPTO_CIPHER_AES_CBC:
340 sa->pad_align = IPSEC_PAD_AES_CBC;
341 sa->iv_len = IPSEC_MAX_IV_SIZE;
342 sa->algo_type = ALGO_TYPE_AES_CBC;
345 case RTE_CRYPTO_CIPHER_AES_CTR:
347 sa->pad_align = IPSEC_PAD_AES_CTR;
348 sa->iv_len = IPSEC_AES_CTR_IV_SIZE;
349 sa->algo_type = ALGO_TYPE_AES_CTR;
352 case RTE_CRYPTO_CIPHER_3DES_CBC:
354 sa->pad_align = IPSEC_PAD_3DES_CBC;
355 sa->iv_len = IPSEC_3DES_IV_SIZE;
356 sa->algo_type = ALGO_TYPE_3DES_CBC;
364 sa->udata = prm->userdata;
365 sa->spi = rte_cpu_to_be_32(prm->ipsec_xform.spi);
366 sa->salt = prm->ipsec_xform.salt;
368 /* preserve all values except l2_len and l3_len */
370 ~rte_mbuf_tx_offload(MBUF_MAX_L2_LEN, MBUF_MAX_L3_LEN,
373 switch (sa->type & msk) {
374 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV4):
375 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV6):
376 esp_inb_tun_init(sa, prm);
378 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TRANS):
381 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV4):
382 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV6):
383 esp_outb_tun_init(sa, prm);
385 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TRANS):
386 esp_outb_init(sa, 0);
394 * helper function, init SA replay structure.
397 fill_sa_replay(struct rte_ipsec_sa *sa, uint32_t wnd_sz, uint32_t nb_bucket)
399 sa->replay.win_sz = wnd_sz;
400 sa->replay.nb_bucket = nb_bucket;
401 sa->replay.bucket_index_mask = nb_bucket - 1;
402 sa->sqn.inb.rsn[0] = (struct replay_sqn *)(sa + 1);
403 if ((sa->type & RTE_IPSEC_SATP_SQN_MASK) == RTE_IPSEC_SATP_SQN_ATOM)
404 sa->sqn.inb.rsn[1] = (struct replay_sqn *)
405 ((uintptr_t)sa->sqn.inb.rsn[0] + rsn_size(nb_bucket));
408 int __rte_experimental
409 rte_ipsec_sa_size(const struct rte_ipsec_sa_prm *prm)
418 /* determine SA type */
419 rc = fill_sa_type(prm, &type);
423 /* determine required size */
424 wsz = prm->replay_win_sz;
425 return ipsec_sa_size(type, &wsz, &nb);
428 int __rte_experimental
429 rte_ipsec_sa_init(struct rte_ipsec_sa *sa, const struct rte_ipsec_sa_prm *prm,
435 struct crypto_xform cxf;
437 if (sa == NULL || prm == NULL)
440 /* determine SA type */
441 rc = fill_sa_type(prm, &type);
445 /* determine required size */
446 wsz = prm->replay_win_sz;
447 sz = ipsec_sa_size(type, &wsz, &nb);
450 else if (size < (uint32_t)sz)
453 /* only esp is supported right now */
454 if (prm->ipsec_xform.proto != RTE_SECURITY_IPSEC_SA_PROTO_ESP)
457 if (prm->ipsec_xform.mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
458 prm->tun.hdr_len > sizeof(sa->hdr))
461 rc = fill_crypto_xform(&cxf, type, prm);
471 /* check for ESN flag */
472 sa->sqn_mask = (prm->ipsec_xform.options.esn == 0) ?
473 UINT32_MAX : UINT64_MAX;
475 rc = esp_sa_init(sa, prm, &cxf);
477 rte_ipsec_sa_fini(sa);
479 /* fill replay window related fields */
481 fill_sa_replay(sa, wsz, nb);
487 * setup crypto ops for LOOKASIDE_PROTO type of devices.
490 lksd_proto_cop_prepare(const struct rte_ipsec_session *ss,
491 struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num)
494 struct rte_crypto_sym_op *sop;
496 for (i = 0; i != num; i++) {
498 cop[i]->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
499 cop[i]->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
500 cop[i]->sess_type = RTE_CRYPTO_OP_SECURITY_SESSION;
502 __rte_security_attach_session(sop, ss->security.ses);
507 * setup packets and crypto ops for LOOKASIDE_PROTO type of devices.
508 * Note that for LOOKASIDE_PROTO all packet modifications will be
509 * performed by PMD/HW.
510 * SW has only to prepare crypto op.
513 lksd_proto_prepare(const struct rte_ipsec_session *ss,
514 struct rte_mbuf *mb[], struct rte_crypto_op *cop[], uint16_t num)
516 lksd_proto_cop_prepare(ss, mb, cop, num);
521 * simplest pkt process routine:
522 * all actual processing is already done by HW/PMD,
523 * just check mbuf ol_flags.
525 * - inbound for RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL
526 * - inbound/outbound for RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL
527 * - outbound for RTE_SECURITY_ACTION_TYPE_NONE when ESN is disabled
530 pkt_flag_process(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
539 for (i = 0; i != num; i++) {
540 if ((mb[i]->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED) == 0)
546 /* handle unprocessed mbufs */
550 move_bad_mbufs(mb, dr, num, num - k);
557 * Select packet processing function for session on LOOKASIDE_NONE
561 lksd_none_pkt_func_select(const struct rte_ipsec_sa *sa,
562 struct rte_ipsec_sa_pkt_func *pf)
566 static const uint64_t msk = RTE_IPSEC_SATP_DIR_MASK |
567 RTE_IPSEC_SATP_MODE_MASK;
570 switch (sa->type & msk) {
571 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV4):
572 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV6):
573 pf->prepare = esp_inb_pkt_prepare;
574 pf->process = esp_inb_tun_pkt_process;
576 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TRANS):
577 pf->prepare = esp_inb_pkt_prepare;
578 pf->process = esp_inb_trs_pkt_process;
580 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV4):
581 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV6):
582 pf->prepare = esp_outb_tun_prepare;
583 pf->process = (sa->sqh_len != 0) ?
584 esp_outb_sqh_process : pkt_flag_process;
586 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TRANS):
587 pf->prepare = esp_outb_trs_prepare;
588 pf->process = (sa->sqh_len != 0) ?
589 esp_outb_sqh_process : pkt_flag_process;
599 * Select packet processing function for session on INLINE_CRYPTO
603 inline_crypto_pkt_func_select(const struct rte_ipsec_sa *sa,
604 struct rte_ipsec_sa_pkt_func *pf)
608 static const uint64_t msk = RTE_IPSEC_SATP_DIR_MASK |
609 RTE_IPSEC_SATP_MODE_MASK;
612 switch (sa->type & msk) {
613 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV4):
614 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TUNLV6):
615 pf->process = esp_inb_tun_pkt_process;
617 case (RTE_IPSEC_SATP_DIR_IB | RTE_IPSEC_SATP_MODE_TRANS):
618 pf->process = esp_inb_trs_pkt_process;
620 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV4):
621 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TUNLV6):
622 pf->process = inline_outb_tun_pkt_process;
624 case (RTE_IPSEC_SATP_DIR_OB | RTE_IPSEC_SATP_MODE_TRANS):
625 pf->process = inline_outb_trs_pkt_process;
635 * Select packet processing function for given session based on SA parameters
636 * and type of associated with the session device.
639 ipsec_sa_pkt_func_select(const struct rte_ipsec_session *ss,
640 const struct rte_ipsec_sa *sa, struct rte_ipsec_sa_pkt_func *pf)
645 pf[0] = (struct rte_ipsec_sa_pkt_func) { 0 };
648 case RTE_SECURITY_ACTION_TYPE_NONE:
649 rc = lksd_none_pkt_func_select(sa, pf);
651 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
652 rc = inline_crypto_pkt_func_select(sa, pf);
654 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
655 if ((sa->type & RTE_IPSEC_SATP_DIR_MASK) ==
656 RTE_IPSEC_SATP_DIR_IB)
657 pf->process = pkt_flag_process;
659 pf->process = inline_proto_outb_pkt_process;
661 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
662 pf->prepare = lksd_proto_prepare;
663 pf->process = pkt_flag_process;