1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
9 #include <rte_cryptodev.h>
12 #include "ipsec_sqn.h"
18 typedef uint16_t (*esp_inb_process_t)(const struct rte_ipsec_sa *sa,
19 struct rte_mbuf *mb[], uint32_t sqn[], uint32_t dr[], uint16_t num,
23 * helper function to fill crypto_sym op for cipher+auth algorithms.
24 * used by inb_cop_prepare(), see below.
27 sop_ciph_auth_prepare(struct rte_crypto_sym_op *sop,
28 const struct rte_ipsec_sa *sa, const union sym_op_data *icv,
29 uint32_t pofs, uint32_t plen)
31 sop->cipher.data.offset = pofs + sa->ctp.cipher.offset;
32 sop->cipher.data.length = plen - sa->ctp.cipher.length;
33 sop->auth.data.offset = pofs + sa->ctp.auth.offset;
34 sop->auth.data.length = plen - sa->ctp.auth.length;
35 sop->auth.digest.data = icv->va;
36 sop->auth.digest.phys_addr = icv->pa;
40 * helper function to fill crypto_sym op for aead algorithms
41 * used by inb_cop_prepare(), see below.
44 sop_aead_prepare(struct rte_crypto_sym_op *sop,
45 const struct rte_ipsec_sa *sa, const union sym_op_data *icv,
46 uint32_t pofs, uint32_t plen)
48 sop->aead.data.offset = pofs + sa->ctp.cipher.offset;
49 sop->aead.data.length = plen - sa->ctp.cipher.length;
50 sop->aead.digest.data = icv->va;
51 sop->aead.digest.phys_addr = icv->pa;
52 sop->aead.aad.data = icv->va + sa->icv_len;
53 sop->aead.aad.phys_addr = icv->pa + sa->icv_len;
57 * setup crypto op and crypto sym op for ESP inbound packet.
60 inb_cop_prepare(struct rte_crypto_op *cop,
61 const struct rte_ipsec_sa *sa, struct rte_mbuf *mb,
62 const union sym_op_data *icv, uint32_t pofs, uint32_t plen)
64 struct rte_crypto_sym_op *sop;
65 struct aead_gcm_iv *gcm;
66 struct aesctr_cnt_blk *ctr;
71 ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
72 pofs + sizeof(struct rte_esp_hdr));
74 /* fill sym op fields */
78 case ALGO_TYPE_AES_GCM:
79 sop_aead_prepare(sop, sa, icv, pofs, plen);
81 /* fill AAD IV (located inside crypto op) */
82 gcm = rte_crypto_op_ctod_offset(cop, struct aead_gcm_iv *,
84 aead_gcm_iv_fill(gcm, ivp[0], sa->salt);
86 case ALGO_TYPE_AES_CBC:
87 case ALGO_TYPE_3DES_CBC:
88 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
90 /* copy iv from the input packet to the cop */
91 ivc = rte_crypto_op_ctod_offset(cop, uint64_t *, sa->iv_ofs);
92 copy_iv(ivc, ivp, sa->iv_len);
94 case ALGO_TYPE_AES_CTR:
95 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
97 /* fill CTR block (located inside crypto op) */
98 ctr = rte_crypto_op_ctod_offset(cop, struct aesctr_cnt_blk *,
100 aes_ctr_cnt_blk_fill(ctr, ivp[0], sa->salt);
103 sop_ciph_auth_prepare(sop, sa, icv, pofs, plen);
109 * Helper function for prepare() to deal with situation when
110 * ICV is spread by two segments. Tries to move ICV completely into the
113 static struct rte_mbuf *
114 move_icv(struct rte_mbuf *ml, uint32_t ofs)
122 n = ml->data_len - ofs;
124 prev = rte_pktmbuf_mtod_offset(ml, const void *, ofs);
125 new = rte_pktmbuf_prepend(ms, n);
129 /* move n ICV bytes from ml into ms */
130 rte_memcpy(new, prev, n);
137 * for pure cryptodev (lookaside none) depending on SA settings,
138 * we might have to write some extra data to the packet.
141 inb_pkt_xprepare(const struct rte_ipsec_sa *sa, rte_be64_t sqc,
142 const union sym_op_data *icv)
144 struct aead_gcm_aad *aad;
146 /* insert SQN.hi between ESP trailer and ICV */
147 if (sa->sqh_len != 0)
148 insert_sqh(sqn_hi32(sqc), icv->va, sa->icv_len);
151 * fill AAD fields, if any (aad fields are placed after icv),
152 * right now we support only one AEAD algorithm: AES-GCM.
154 if (sa->aad_len != 0) {
155 aad = (struct aead_gcm_aad *)(icv->va + sa->icv_len);
156 aead_gcm_aad_fill(aad, sa->spi, sqc, IS_ESN(sa));
161 * setup/update packet data and metadata for ESP inbound tunnel case.
163 static inline int32_t
164 inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
165 struct rte_mbuf *mb, uint32_t hlen, union sym_op_data *icv)
169 uint32_t clen, icv_len, icv_ofs, plen;
171 struct rte_esp_hdr *esph;
173 esph = rte_pktmbuf_mtod_offset(mb, struct rte_esp_hdr *, hlen);
176 * retrieve and reconstruct SQN, then check it, then
177 * convert it back into network byte order.
179 sqn = rte_be_to_cpu_32(esph->seq);
181 sqn = reconstruct_esn(rsn->sqn, sqn, sa->replay.win_sz);
183 rc = esn_inb_check_sqn(rsn, sa, sqn);
187 sqn = rte_cpu_to_be_64(sqn);
189 /* start packet manipulation */
193 /* check that packet has a valid length */
194 clen = plen - sa->ctp.cipher.length;
195 if ((int32_t)clen < 0 || (clen & (sa->pad_align - 1)) != 0)
198 /* find ICV location */
199 icv_len = sa->icv_len;
200 icv_ofs = mb->pkt_len - icv_len;
202 ml = mbuf_get_seg_ofs(mb, &icv_ofs);
205 * if ICV is spread by two segments, then try to
206 * move ICV completely into the last segment.
208 if (ml->data_len < icv_ofs + icv_len) {
210 ml = move_icv(ml, icv_ofs);
214 /* new ICV location */
218 icv_ofs += sa->sqh_len;
220 /* we have to allocate space for AAD somewhere,
221 * right now - just use free trailing space at the last segment.
222 * Would probably be more convenient to reserve space for AAD
223 * inside rte_crypto_op itself
224 * (again for IV space is already reserved inside cop).
226 if (sa->aad_len + sa->sqh_len > rte_pktmbuf_tailroom(ml))
229 icv->va = rte_pktmbuf_mtod_offset(ml, void *, icv_ofs);
230 icv->pa = rte_pktmbuf_iova_offset(ml, icv_ofs);
233 * if esn is used then high-order 32 bits are also used in ICV
234 * calculation but are not transmitted, update packet length
235 * to be consistent with auth data length and offset, this will
236 * be subtracted from packet length in post crypto processing
238 mb->pkt_len += sa->sqh_len;
239 ml->data_len += sa->sqh_len;
241 inb_pkt_xprepare(sa, sqn, icv);
246 * setup/update packets and crypto ops for ESP inbound case.
249 esp_inb_pkt_prepare(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
250 struct rte_crypto_op *cop[], uint16_t num)
254 struct rte_ipsec_sa *sa;
255 struct rte_cryptodev_sym_session *cs;
256 struct replay_sqn *rsn;
257 union sym_op_data icv;
262 rsn = rsn_acquire(sa);
265 for (i = 0; i != num; i++) {
267 hl = mb[i]->l2_len + mb[i]->l3_len;
268 rc = inb_pkt_prepare(sa, rsn, mb[i], hl, &icv);
270 lksd_none_cop_prepare(cop[k], cs, mb[i]);
271 inb_cop_prepare(cop[k], sa, mb[i], &icv, hl, rc);
277 rsn_release(sa, rsn);
279 /* copy not prepared mbufs beyond good ones */
280 if (k != num && k != 0) {
281 move_bad_mbufs(mb, dr, num, num - k);
289 * Start with processing inbound packet.
290 * This is common part for both tunnel and transport mode.
291 * Extract information that will be needed later from mbuf metadata and
292 * actual packet data:
293 * - mbuf for packet's last segment
294 * - length of the L2/L3 headers
295 * - esp tail structure
298 process_step1(struct rte_mbuf *mb, uint32_t tlen, struct rte_mbuf **ml,
299 struct esp_tail *espt, uint32_t *hlen, uint32_t *tofs)
301 const struct esp_tail *pt;
304 ofs = mb->pkt_len - tlen;
305 hlen[0] = mb->l2_len + mb->l3_len;
306 ml[0] = mbuf_get_seg_ofs(mb, &ofs);
307 pt = rte_pktmbuf_mtod_offset(ml[0], const struct esp_tail *, ofs);
313 * Helper function to check pad bytes values.
314 * Note that pad bytes can be spread across multiple segments.
317 check_pad_bytes(struct rte_mbuf *mb, uint32_t ofs, uint32_t len)
322 for (n = 0; n != len; n += k, mb = mb->next) {
323 k = mb->data_len - ofs;
324 k = RTE_MIN(k, len - n);
325 pd = rte_pktmbuf_mtod_offset(mb, const uint8_t *, ofs);
326 if (memcmp(pd, esp_pad_bytes + n, k) != 0)
335 * packet checks for transport mode:
336 * - no reported IPsec related failures in ol_flags
337 * - tail and header lengths are valid
338 * - padding bytes are valid
339 * apart from checks, function also updates tail offset (and segment)
340 * by taking into account pad length.
342 static inline int32_t
343 trs_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
344 uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen)
346 if ((mb->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED) != 0 ||
347 tlen + hlen > mb->pkt_len)
350 /* padding bytes are spread over multiple segments */
351 if (tofs[0] < espt.pad_len) {
352 tofs[0] = mb->pkt_len - tlen;
353 ml[0] = mbuf_get_seg_ofs(mb, tofs);
355 tofs[0] -= espt.pad_len;
357 return check_pad_bytes(ml[0], tofs[0], espt.pad_len);
361 * packet checks for tunnel mode:
362 * - same as for trasnport mode
363 * - esp tail next proto contains expected for that SA value
365 static inline int32_t
366 tun_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
367 uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen,
370 return (trs_process_check(mb, ml, tofs, espt, hlen, tlen) ||
371 espt.next_proto != proto);
375 * step two for tunnel mode:
376 * - read SQN value (for future use)
377 * - cut of ICV, ESP tail and padding bytes
378 * - cut of ESP header and IV, also if needed - L2/L3 headers
379 * (controlled by *adj* value)
382 tun_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
383 uint32_t adj, uint32_t tofs, uint32_t tlen, uint32_t *sqn)
385 const struct rte_esp_hdr *ph;
388 ph = rte_pktmbuf_mtod_offset(mb, const struct rte_esp_hdr *, hlen);
391 /* cut of ICV, ESP tail and padding bytes */
392 mbuf_cut_seg_ofs(mb, ml, tofs, tlen);
394 /* cut of L2/L3 headers, ESP header and IV */
395 return rte_pktmbuf_adj(mb, adj);
399 * step two for transport mode:
400 * - read SQN value (for future use)
401 * - cut of ICV, ESP tail and padding bytes
402 * - cut of ESP header and IV
403 * - move L2/L3 header to fill the gap after ESP header removal
406 trs_process_step2(struct rte_mbuf *mb, struct rte_mbuf *ml, uint32_t hlen,
407 uint32_t adj, uint32_t tofs, uint32_t tlen, uint32_t *sqn)
411 /* get start of the packet before modifications */
412 op = rte_pktmbuf_mtod(mb, char *);
414 /* cut off ESP header and IV */
415 np = tun_process_step2(mb, ml, hlen, adj, tofs, tlen, sqn);
417 /* move header bytes to fill the gap after ESP header removal */
418 remove_esph(np, op, hlen);
423 * step three for transport mode:
424 * update mbuf metadata:
429 trs_process_step3(struct rte_mbuf *mb)
431 /* reset mbuf packet type */
432 mb->packet_type &= (RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
434 /* clear the PKT_RX_SEC_OFFLOAD flag if set */
435 mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
439 * step three for tunnel mode:
440 * update mbuf metadata:
446 tun_process_step3(struct rte_mbuf *mb, uint64_t txof_msk, uint64_t txof_val)
448 /* reset mbuf metatdata: L2/L3 len, packet type */
449 mb->packet_type = RTE_PTYPE_UNKNOWN;
450 mb->tx_offload = (mb->tx_offload & txof_msk) | txof_val;
452 /* clear the PKT_RX_SEC_OFFLOAD flag if set */
453 mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
457 * *process* function for tunnel packets
459 static inline uint16_t
460 tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
461 uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
463 uint32_t adj, i, k, tl;
464 uint32_t hl[num], to[num];
465 struct esp_tail espt[num];
466 struct rte_mbuf *ml[num];
471 * remove icv, esp trailer and high-order
472 * 32 bits of esn from packet length
474 const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
475 const uint32_t cofs = sa->ctp.cipher.offset;
478 * to minimize stalls due to load latency,
479 * read mbufs metadata and esp tail first.
481 for (i = 0; i != num; i++)
482 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i], &to[i]);
485 for (i = 0; i != num; i++) {
488 tl = tlen + espt[i].pad_len;
490 /* check that packet is valid */
491 if (tun_process_check(mb[i], &ml[i], &to[i], espt[i], adj, tl,
494 outh = rte_pktmbuf_mtod_offset(mb[i], uint8_t *,
497 /* modify packet's layout */
498 inh = tun_process_step2(mb[i], ml[i], hl[i], adj,
501 /* update inner ip header */
502 update_tun_inb_l3hdr(sa, outh, inh);
504 /* update mbuf's metadata */
505 tun_process_step3(mb[i], sa->tx_offload.msk,
517 * *process* function for tunnel packets
519 static inline uint16_t
520 trs_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
521 uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
524 uint32_t i, k, l2, tl;
525 uint32_t hl[num], to[num];
526 struct esp_tail espt[num];
527 struct rte_mbuf *ml[num];
530 * remove icv, esp trailer and high-order
531 * 32 bits of esn from packet length
533 const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
534 const uint32_t cofs = sa->ctp.cipher.offset;
537 * to minimize stalls due to load latency,
538 * read mbufs metadata and esp tail first.
540 for (i = 0; i != num; i++)
541 process_step1(mb[i], tlen, &ml[i], &espt[i], &hl[i], &to[i]);
544 for (i = 0; i != num; i++) {
546 tl = tlen + espt[i].pad_len;
549 /* check that packet is valid */
550 if (trs_process_check(mb[i], &ml[i], &to[i], espt[i],
551 hl[i] + cofs, tl) == 0) {
553 /* modify packet's layout */
554 np = trs_process_step2(mb[i], ml[i], hl[i], cofs,
556 update_trs_l3hdr(sa, np + l2, mb[i]->pkt_len,
557 l2, hl[i] - l2, espt[i].next_proto);
559 /* update mbuf's metadata */
560 trs_process_step3(mb[i]);
570 * for group of ESP inbound packets perform SQN check and update.
572 static inline uint16_t
573 esp_inb_rsn_update(struct rte_ipsec_sa *sa, const uint32_t sqn[],
574 uint32_t dr[], uint16_t num)
577 struct replay_sqn *rsn;
579 /* replay not enabled */
580 if (sa->replay.win_sz == 0)
583 rsn = rsn_update_start(sa);
586 for (i = 0; i != num; i++) {
587 if (esn_inb_update_sqn(rsn, sa, rte_be_to_cpu_32(sqn[i])) == 0)
593 rsn_update_finish(sa, rsn);
598 * process group of ESP inbound packets.
600 static inline uint16_t
601 esp_inb_pkt_process(struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
602 uint16_t num, uint8_t sqh_len, esp_inb_process_t process)
608 /* process packets, extract seq numbers */
609 k = process(sa, mb, sqn, dr, num, sqh_len);
611 /* handle unprocessed mbufs */
612 if (k != num && k != 0)
613 move_bad_mbufs(mb, dr, num, num - k);
615 /* update SQN and replay winow */
616 n = esp_inb_rsn_update(sa, sqn, dr, k);
618 /* handle mbufs with wrong SQN */
619 if (n != k && n != 0)
620 move_bad_mbufs(mb, dr, k, k - n);
629 * process group of ESP inbound tunnel packets.
632 esp_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
633 struct rte_mbuf *mb[], uint16_t num)
635 struct rte_ipsec_sa *sa = ss->sa;
637 return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, tun_process);
641 inline_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
642 struct rte_mbuf *mb[], uint16_t num)
644 return esp_inb_pkt_process(ss->sa, mb, num, 0, tun_process);
648 * process group of ESP inbound transport packets.
651 esp_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
652 struct rte_mbuf *mb[], uint16_t num)
654 struct rte_ipsec_sa *sa = ss->sa;
656 return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, trs_process);
660 inline_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
661 struct rte_mbuf *mb[], uint16_t num)
663 return esp_inb_pkt_process(ss->sa, mb, num, 0, trs_process);