+ struct mbuf *s;
+ struct mbuf *d, *d0, *dp;
+ int soff; /* offset from the head of chain, to head of this mbuf */
+ int sn, dn; /* offset from the head of the mbuf, to meat */
+ size_t ivoff, bodyoff;
+ u_int8_t iv[AES_BLOCKLEN], *dptr;
+ u_int8_t sbuf[AES_BLOCKLEN], *sp;
+ struct mbuf *scut;
+ int scutoff;
+ int i, len;
+
+
+ if (ivlen != AES_BLOCKLEN) {
+ ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
+ "unsupported ivlen %d\n", algo->name, ivlen));
+ m_freem(m);
+ return EINVAL;
+ }
+
+ if (sav->flags & SADB_X_EXT_OLD) {
+ /* RFC 1827 */
+ ivoff = off + sizeof(struct esp);
+ bodyoff = off + sizeof(struct esp) + ivlen;
+ } else {
+ ivoff = off + sizeof(struct newesp);
+ bodyoff = off + sizeof(struct newesp) + ivlen;
+ }
+
+ if (m->m_pkthdr.len < bodyoff) {
+ ipseclog((LOG_ERR, "esp_cbc_decrypt %s: bad len %d/%lu\n",
+ algo->name, m->m_pkthdr.len, (u_int32_t)bodyoff));
+ m_freem(m);
+ return EINVAL;
+ }
+ if ((m->m_pkthdr.len - bodyoff) % AES_BLOCKLEN) {
+ ipseclog((LOG_ERR, "esp_cbc_decrypt %s: "
+ "payload length must be multiple of %d\n",
+ algo->name, AES_BLOCKLEN));
+ m_freem(m);
+ return EINVAL;
+ }
+
+ /* grab iv */
+ m_copydata(m, ivoff, ivlen, (caddr_t) iv);
+
+ s = m;
+ soff = sn = dn = 0;
+ d = d0 = dp = NULL;
+ sp = dptr = NULL;
+
+ /* skip header/IV offset */
+ while (soff < bodyoff) {
+ if (soff + s->m_len > bodyoff) {
+ sn = bodyoff - soff;
+ break;
+ }
+
+ soff += s->m_len;
+ s = s->m_next;
+ }
+ scut = s;
+ scutoff = sn;
+
+ /* skip over empty mbuf */
+ while (s && s->m_len == 0)
+ s = s->m_next;
+
+ while (soff < m->m_pkthdr.len) {
+ /* source */
+ if (sn + AES_BLOCKLEN <= s->m_len) {
+ /* body is continuous */
+ sp = mtod(s, u_int8_t *) + sn;
+ len = s->m_len - sn;
+ len -= len % AES_BLOCKLEN; // full blocks only
+ } else {
+ /* body is non-continuous */
+ m_copydata(s, sn, AES_BLOCKLEN, (caddr_t) sbuf);
+ sp = sbuf;
+ len = AES_BLOCKLEN; // 1 block only in sbuf
+ }
+
+ /* destination */
+ if (!d || dn + AES_BLOCKLEN > d->m_len) {
+ if (d)
+ dp = d;
+ MGET(d, M_DONTWAIT, MT_DATA);
+ i = m->m_pkthdr.len - (soff + sn);
+ if (d && i > MLEN) {
+ MCLGET(d, M_DONTWAIT);
+ if ((d->m_flags & M_EXT) == 0) {
+ d = m_mbigget(d, M_DONTWAIT);
+ if ((d->m_flags & M_EXT) == 0) {
+ m_free(d);
+ d = NULL;
+ }
+ }
+ }
+ if (!d) {
+ m_freem(m);
+ if (d0)
+ m_freem(d0);
+ return ENOBUFS;
+ }
+ if (!d0)
+ d0 = d;
+ if (dp)
+ dp->m_next = d;
+ d->m_len = M_TRAILINGSPACE(d);
+ d->m_len -= d->m_len % AES_BLOCKLEN;
+ if (d->m_len > i)
+ d->m_len = i;
+ dptr = mtod(d, u_int8_t *);
+ dn = 0;
+ }
+
+ /* adjust len if greater than space available in dest */
+ if (len > d->m_len - dn)
+ len = d->m_len - dn;
+
+ /* decrypt */
+ aes_decrypt_cbc(sp, iv, len >> 4, dptr + dn,
+ (aes_decrypt_ctx*)(&(((aes_ctx*)sav->sched)->decrypt)));
+
+ /* udpate offsets */
+ sn += len;
+ dn += len;
+
+ // next iv
+ bcopy(sp + len - AES_BLOCKLEN, iv, AES_BLOCKLEN);
+
+ /* find the next source block */
+ while (s && sn >= s->m_len) {
+ sn -= s->m_len;
+ soff += s->m_len;
+ s = s->m_next;
+ }
+
+ }
+
+ /* free un-needed source mbufs and add dest mbufs to chain */
+ m_freem(scut->m_next);
+ scut->m_len = scutoff;
+ scut->m_next = d0;
+
+ /* just in case */
+ bzero(iv, sizeof(iv));
+ bzero(sbuf, sizeof(sbuf));
+