mbuf: extend meaning of QinQ stripped bit
[dpdk.git] / lib / librte_ipsec / esp_inb.c
index 819d2bf..96eec01 100644 (file)
@@ -1,5 +1,5 @@
 /* SPDX-License-Identifier: BSD-3-Clause
- * Copyright(c) 2018 Intel Corporation
+ * Copyright(c) 2018-2020 Intel Corporation
  */
 
 #include <rte_ipsec.h>
@@ -16,7 +16,8 @@
 #include "pad.h"
 
 typedef uint16_t (*esp_inb_process_t)(const struct rte_ipsec_sa *sa,
-       struct rte_mbuf *mb[], uint32_t sqn[], uint32_t dr[], uint16_t num);
+       struct rte_mbuf *mb[], uint32_t sqn[], uint32_t dr[], uint16_t num,
+       uint8_t sqh_len);
 
 /*
  * helper function to fill crypto_sym op for cipher+auth algorithms.
@@ -104,6 +105,39 @@ inb_cop_prepare(struct rte_crypto_op *cop,
        }
 }
 
+static inline uint32_t
+inb_cpu_crypto_prepare(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb,
+       uint32_t *pofs, uint32_t plen, void *iv)
+{
+       struct aead_gcm_iv *gcm;
+       struct aesctr_cnt_blk *ctr;
+       uint64_t *ivp;
+       uint32_t clen;
+
+       ivp = rte_pktmbuf_mtod_offset(mb, uint64_t *,
+               *pofs + sizeof(struct rte_esp_hdr));
+       clen = 0;
+
+       switch (sa->algo_type) {
+       case ALGO_TYPE_AES_GCM:
+               gcm = (struct aead_gcm_iv *)iv;
+               aead_gcm_iv_fill(gcm, ivp[0], sa->salt);
+               break;
+       case ALGO_TYPE_AES_CBC:
+       case ALGO_TYPE_3DES_CBC:
+               copy_iv(iv, ivp, sa->iv_len);
+               break;
+       case ALGO_TYPE_AES_CTR:
+               ctr = (struct aesctr_cnt_blk *)iv;
+               aes_ctr_cnt_blk_fill(ctr, ivp[0], sa->salt);
+               break;
+       }
+
+       *pofs += sa->ctp.auth.offset;
+       clen = plen - sa->ctp.auth.length;
+       return clen;
+}
+
 /*
  * Helper function for prepare() to deal with situation when
  * ICV is spread by two segments. Tries to move ICV completely into the
@@ -156,17 +190,12 @@ inb_pkt_xprepare(const struct rte_ipsec_sa *sa, rte_be64_t sqc,
        }
 }
 
-/*
- * setup/update packet data and metadata for ESP inbound tunnel case.
- */
-static inline int32_t
-inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
-       struct rte_mbuf *mb, uint32_t hlen, union sym_op_data *icv)
+static inline int
+inb_get_sqn(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
+       struct rte_mbuf *mb, uint32_t hlen, rte_be64_t *sqc)
 {
        int32_t rc;
        uint64_t sqn;
-       uint32_t clen, icv_len, icv_ofs, plen;
-       struct rte_mbuf *ml;
        struct rte_esp_hdr *esph;
 
        esph = rte_pktmbuf_mtod_offset(mb, struct rte_esp_hdr *, hlen);
@@ -178,12 +207,21 @@ inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
        sqn = rte_be_to_cpu_32(esph->seq);
        if (IS_ESN(sa))
                sqn = reconstruct_esn(rsn->sqn, sqn, sa->replay.win_sz);
+       *sqc = rte_cpu_to_be_64(sqn);
 
+       /* check IPsec window */
        rc = esn_inb_check_sqn(rsn, sa, sqn);
-       if (rc != 0)
-               return rc;
 
-       sqn = rte_cpu_to_be_64(sqn);
+       return rc;
+}
+
+/* prepare packet for upcoming processing */
+static inline int32_t
+inb_prepare(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb,
+       uint32_t hlen, union sym_op_data *icv)
+{
+       uint32_t clen, icv_len, icv_ofs, plen;
+       struct rte_mbuf *ml;
 
        /* start packet manipulation */
        plen = mb->pkt_len;
@@ -216,7 +254,8 @@ inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
 
        icv_ofs += sa->sqh_len;
 
-       /* we have to allocate space for AAD somewhere,
+       /*
+        * we have to allocate space for AAD somewhere,
         * right now - just use free trailing space at the last segment.
         * Would probably be more convenient to reserve space for AAD
         * inside rte_crypto_op itself
@@ -228,10 +267,37 @@ inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
        icv->va = rte_pktmbuf_mtod_offset(ml, void *, icv_ofs);
        icv->pa = rte_pktmbuf_iova_offset(ml, icv_ofs);
 
-       inb_pkt_xprepare(sa, sqn, icv);
+       /*
+        * if esn is used then high-order 32 bits are also used in ICV
+        * calculation but are not transmitted, update packet length
+        * to be consistent with auth data length and offset, this will
+        * be subtracted from packet length in post crypto processing
+        */
+       mb->pkt_len += sa->sqh_len;
+       ml->data_len += sa->sqh_len;
+
        return plen;
 }
 
+static inline int32_t
+inb_pkt_prepare(const struct rte_ipsec_sa *sa, const struct replay_sqn *rsn,
+       struct rte_mbuf *mb, uint32_t hlen, union sym_op_data *icv)
+{
+       int rc;
+       rte_be64_t sqn;
+
+       rc = inb_get_sqn(sa, rsn, mb, hlen, &sqn);
+       if (rc != 0)
+               return rc;
+
+       rc = inb_prepare(sa, mb, hlen, icv);
+       if (rc < 0)
+               return rc;
+
+       inb_pkt_xprepare(sa, sqn, icv);
+       return rc;
+}
+
 /*
  * setup/update packets and crypto ops for ESP inbound case.
  */
@@ -260,17 +326,17 @@ esp_inb_pkt_prepare(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
                        lksd_none_cop_prepare(cop[k], cs, mb[i]);
                        inb_cop_prepare(cop[k], sa, mb[i], &icv, hl, rc);
                        k++;
-               } else
+               } else {
                        dr[i - k] = i;
+                       rte_errno = -rc;
+               }
        }
 
        rsn_release(sa, rsn);
 
        /* copy not prepared mbufs beyond good ones */
-       if (k != num && k != 0) {
+       if (k != num && k != 0)
                move_bad_mbufs(mb, dr, num, num - k);
-               rte_errno = EBADMSG;
-       }
 
        return k;
 }
@@ -286,15 +352,15 @@ esp_inb_pkt_prepare(const struct rte_ipsec_session *ss, struct rte_mbuf *mb[],
  */
 static inline void
 process_step1(struct rte_mbuf *mb, uint32_t tlen, struct rte_mbuf **ml,
-       struct esp_tail *espt, uint32_t *hlen, uint32_t *tofs)
+       struct rte_esp_tail *espt, uint32_t *hlen, uint32_t *tofs)
 {
-       const struct esp_tail *pt;
+       const struct rte_esp_tail *pt;
        uint32_t ofs;
 
        ofs = mb->pkt_len - tlen;
        hlen[0] = mb->l2_len + mb->l3_len;
        ml[0] = mbuf_get_seg_ofs(mb, &ofs);
-       pt = rte_pktmbuf_mtod_offset(ml[0], const struct esp_tail *, ofs);
+       pt = rte_pktmbuf_mtod_offset(ml[0], const struct rte_esp_tail *, ofs);
        tofs[0] = ofs;
        espt[0] = pt[0];
 }
@@ -331,7 +397,7 @@ check_pad_bytes(struct rte_mbuf *mb, uint32_t ofs, uint32_t len)
  */
 static inline int32_t
 trs_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
-       uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen)
+       uint32_t *tofs, struct rte_esp_tail espt, uint32_t hlen, uint32_t tlen)
 {
        if ((mb->ol_flags & PKT_RX_SEC_OFFLOAD_FAILED) != 0 ||
                        tlen + hlen > mb->pkt_len)
@@ -354,7 +420,7 @@ trs_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
  */
 static inline int32_t
 tun_process_check(struct rte_mbuf *mb, struct rte_mbuf **ml,
-       uint32_t *tofs, struct esp_tail espt, uint32_t hlen, uint32_t tlen,
+       uint32_t *tofs, struct rte_esp_tail espt, uint32_t hlen, uint32_t tlen,
        uint8_t proto)
 {
        return (trs_process_check(mb, ml, tofs, espt, hlen, tlen) ||
@@ -443,20 +509,25 @@ tun_process_step3(struct rte_mbuf *mb, uint64_t txof_msk, uint64_t txof_val)
        mb->ol_flags &= ~PKT_RX_SEC_OFFLOAD;
 }
 
-
 /*
  * *process* function for tunnel packets
  */
 static inline uint16_t
 tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
-       uint32_t sqn[], uint32_t dr[], uint16_t num)
+           uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
 {
        uint32_t adj, i, k, tl;
        uint32_t hl[num], to[num];
-       struct esp_tail espt[num];
+       struct rte_esp_tail espt[num];
        struct rte_mbuf *ml[num];
+       const void *outh;
+       void *inh;
 
-       const uint32_t tlen = sa->icv_len + sizeof(espt[0]);
+       /*
+        * remove icv, esp trailer and high-order
+        * 32 bits of esn from packet length
+        */
+       const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
        const uint32_t cofs = sa->ctp.cipher.offset;
 
        /*
@@ -476,9 +547,16 @@ tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
                if (tun_process_check(mb[i], &ml[i], &to[i], espt[i], adj, tl,
                                        sa->proto) == 0) {
 
+                       outh = rte_pktmbuf_mtod_offset(mb[i], uint8_t *,
+                                       mb[i]->l2_len);
+
                        /* modify packet's layout */
-                       tun_process_step2(mb[i], ml[i], hl[i], adj, to[i],
-                               tl, sqn + k);
+                       inh = tun_process_step2(mb[i], ml[i], hl[i], adj,
+                                       to[i], tl, sqn + k);
+
+                       /* update inner ip header */
+                       update_tun_inb_l3hdr(sa, outh, inh);
+
                        /* update mbuf's metadata */
                        tun_process_step3(mb[i], sa->tx_offload.msk,
                                sa->tx_offload.val);
@@ -490,21 +568,24 @@ tun_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
        return k;
 }
 
-
 /*
  * *process* function for tunnel packets
  */
 static inline uint16_t
 trs_process(const struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
-       uint32_t sqn[], uint32_t dr[], uint16_t num)
+       uint32_t sqn[], uint32_t dr[], uint16_t num, uint8_t sqh_len)
 {
        char *np;
        uint32_t i, k, l2, tl;
        uint32_t hl[num], to[num];
-       struct esp_tail espt[num];
+       struct rte_esp_tail espt[num];
        struct rte_mbuf *ml[num];
 
-       const uint32_t tlen = sa->icv_len + sizeof(espt[0]);
+       /*
+        * remove icv, esp trailer and high-order
+        * 32 bits of esn from packet length
+        */
+       const uint32_t tlen = sa->icv_len + sizeof(espt[0]) + sqh_len;
        const uint32_t cofs = sa->ctp.cipher.offset;
 
        /*
@@ -572,24 +653,21 @@ esp_inb_rsn_update(struct rte_ipsec_sa *sa, const uint32_t sqn[],
  * process group of ESP inbound packets.
  */
 static inline uint16_t
-esp_inb_pkt_process(const struct rte_ipsec_session *ss,
-       struct rte_mbuf *mb[], uint16_t num, esp_inb_process_t process)
+esp_inb_pkt_process(struct rte_ipsec_sa *sa, struct rte_mbuf *mb[],
+       uint16_t num, uint8_t sqh_len, esp_inb_process_t process)
 {
        uint32_t k, n;
-       struct rte_ipsec_sa *sa;
        uint32_t sqn[num];
        uint32_t dr[num];
 
-       sa = ss->sa;
-
        /* process packets, extract seq numbers */
-       k = process(sa, mb, sqn, dr, num);
+       k = process(sa, mb, sqn, dr, num, sqh_len);
 
        /* handle unprocessed mbufs */
        if (k != num && k != 0)
                move_bad_mbufs(mb, dr, num, num - k);
 
-       /* update SQN and replay winow */
+       /* update SQN and replay window */
        n = esp_inb_rsn_update(sa, sqn, dr, k);
 
        /* handle mbufs with wrong SQN */
@@ -602,6 +680,69 @@ esp_inb_pkt_process(const struct rte_ipsec_session *ss,
        return n;
 }
 
+/*
+ * Prepare (plus actual crypto/auth) routine for inbound CPU-CRYPTO
+ * (synchronous mode).
+ */
+uint16_t
+cpu_inb_pkt_prepare(const struct rte_ipsec_session *ss,
+       struct rte_mbuf *mb[], uint16_t num)
+{
+       int32_t rc;
+       uint32_t i, k;
+       struct rte_ipsec_sa *sa;
+       struct replay_sqn *rsn;
+       union sym_op_data icv;
+       void *iv[num];
+       void *aad[num];
+       void *dgst[num];
+       uint32_t dr[num];
+       uint32_t l4ofs[num];
+       uint32_t clen[num];
+       uint64_t ivbuf[num][IPSEC_MAX_IV_QWORD];
+
+       sa = ss->sa;
+
+       /* grab rsn lock */
+       rsn = rsn_acquire(sa);
+
+       /* do preparation for all packets */
+       for (i = 0, k = 0; i != num; i++) {
+
+               /* calculate ESP header offset */
+               l4ofs[k] = mb[i]->l2_len + mb[i]->l3_len;
+
+               /* prepare ESP packet for processing */
+               rc = inb_pkt_prepare(sa, rsn, mb[i], l4ofs[k], &icv);
+               if (rc >= 0) {
+                       /* get encrypted data offset and length */
+                       clen[k] = inb_cpu_crypto_prepare(sa, mb[i],
+                               l4ofs + k, rc, ivbuf[k]);
+
+                       /* fill iv, digest and aad */
+                       iv[k] = ivbuf[k];
+                       aad[k] = icv.va + sa->icv_len;
+                       dgst[k++] = icv.va;
+               } else {
+                       dr[i - k] = i;
+                       rte_errno = -rc;
+               }
+       }
+
+       /* release rsn lock */
+       rsn_release(sa, rsn);
+
+       /* copy not prepared mbufs beyond good ones */
+       if (k != num && k != 0)
+               move_bad_mbufs(mb, dr, num, num - k);
+
+       /* convert mbufs to iovecs and do actual crypto/auth processing */
+       if (k != 0)
+               cpu_crypto_bulk(ss, sa->cofs, mb, iv, aad, dgst,
+                       l4ofs, clen, k);
+       return k;
+}
+
 /*
  * process group of ESP inbound tunnel packets.
  */
@@ -609,7 +750,16 @@ uint16_t
 esp_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
        struct rte_mbuf *mb[], uint16_t num)
 {
-       return esp_inb_pkt_process(ss, mb, num, tun_process);
+       struct rte_ipsec_sa *sa = ss->sa;
+
+       return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, tun_process);
+}
+
+uint16_t
+inline_inb_tun_pkt_process(const struct rte_ipsec_session *ss,
+       struct rte_mbuf *mb[], uint16_t num)
+{
+       return esp_inb_pkt_process(ss->sa, mb, num, 0, tun_process);
 }
 
 /*
@@ -619,5 +769,14 @@ uint16_t
 esp_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
        struct rte_mbuf *mb[], uint16_t num)
 {
-       return esp_inb_pkt_process(ss, mb, num, trs_process);
+       struct rte_ipsec_sa *sa = ss->sa;
+
+       return esp_inb_pkt_process(sa, mb, num, sa->sqh_len, trs_process);
+}
+
+uint16_t
+inline_inb_trs_pkt_process(const struct rte_ipsec_session *ss,
+       struct rte_mbuf *mb[], uint16_t num)
+{
+       return esp_inb_pkt_process(ss->sa, mb, num, 0, trs_process);
 }