The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netipsec/xform_esp.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $FreeBSD: stable/11/sys/netipsec/xform_esp.c 355529 2019-12-08 15:24:03Z ae $   */
    2 /*      $OpenBSD: ip_esp.c,v 1.69 2001/06/26 06:18:59 angelos Exp $ */
    3 /*-
    4  * The authors of this code are John Ioannidis (ji@tla.org),
    5  * Angelos D. Keromytis (kermit@csd.uch.gr) and
    6  * Niels Provos (provos@physnet.uni-hamburg.de).
    7  *
    8  * The original version of this code was written by John Ioannidis
    9  * for BSD/OS in Athens, Greece, in November 1995.
   10  *
   11  * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
   12  * by Angelos D. Keromytis.
   13  *
   14  * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
   15  * and Niels Provos.
   16  *
   17  * Additional features in 1999 by Angelos D. Keromytis.
   18  *
   19  * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
   20  * Angelos D. Keromytis and Niels Provos.
   21  * Copyright (c) 2001 Angelos D. Keromytis.
   22  *
   23  * Permission to use, copy, and modify this software with or without fee
   24  * is hereby granted, provided that this entire notice is included in
   25  * all copies of any software which is or includes a copy or
   26  * modification of this software.
   27  * You may use this code under the GNU public license if you so wish. Please
   28  * contribute changes back to the authors under this freer than GPL license
   29  * so that we may further the use of strong encryption without limitations to
   30  * all.
   31  *
   32  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
   33  * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
   34  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
   35  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
   36  * PURPOSE.
   37  */
   38 #include "opt_inet.h"
   39 #include "opt_inet6.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/mbuf.h>
   44 #include <sys/socket.h>
   45 #include <sys/syslog.h>
   46 #include <sys/kernel.h>
   47 #include <sys/lock.h>
   48 #include <sys/random.h>
   49 #include <sys/mutex.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/mutex.h>
   52 #include <machine/atomic.h>
   53 
   54 #include <net/if.h>
   55 #include <net/vnet.h>
   56 
   57 #include <netinet/in.h>
   58 #include <netinet/in_systm.h>
   59 #include <netinet/ip.h>
   60 #include <netinet/ip_ecn.h>
   61 #include <netinet/ip6.h>
   62 
   63 #include <netipsec/ipsec.h>
   64 #include <netipsec/ah.h>
   65 #include <netipsec/ah_var.h>
   66 #include <netipsec/esp.h>
   67 #include <netipsec/esp_var.h>
   68 #include <netipsec/xform.h>
   69 
   70 #ifdef INET6
   71 #include <netinet6/ip6_var.h>
   72 #include <netipsec/ipsec6.h>
   73 #include <netinet6/ip6_ecn.h>
   74 #endif
   75 
   76 #include <netipsec/key.h>
   77 #include <netipsec/key_debug.h>
   78 
   79 #include <opencrypto/cryptodev.h>
   80 #include <opencrypto/xform.h>
   81 
   82 VNET_DEFINE(int, esp_enable) = 1;
   83 VNET_PCPUSTAT_DEFINE(struct espstat, espstat);
   84 VNET_PCPUSTAT_SYSINIT(espstat);
   85 
   86 #ifdef VIMAGE
   87 VNET_PCPUSTAT_SYSUNINIT(espstat);
   88 #endif /* VIMAGE */
   89 
   90 SYSCTL_DECL(_net_inet_esp);
   91 SYSCTL_INT(_net_inet_esp, OID_AUTO, esp_enable,
   92         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(esp_enable), 0, "");
   93 SYSCTL_VNET_PCPUSTAT(_net_inet_esp, IPSECCTL_STATS, stats,
   94     struct espstat, espstat,
   95     "ESP statistics (struct espstat, netipsec/esp_var.h");
   96 
   97 static struct timeval deswarn, blfwarn, castwarn, camelliawarn;
   98 
   99 static int esp_input_cb(struct cryptop *op);
  100 static int esp_output_cb(struct cryptop *crp);
  101 
  102 size_t
  103 esp_hdrsiz(struct secasvar *sav)
  104 {
  105         size_t size;
  106 
  107         if (sav != NULL) {
  108                 /*XXX not right for null algorithm--does it matter??*/
  109                 IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
  110                         ("SA with null xform"));
  111                 if (sav->flags & SADB_X_EXT_OLD)
  112                         size = sizeof (struct esp);
  113                 else
  114                         size = sizeof (struct newesp);
  115                 size += sav->tdb_encalgxform->blocksize + 9;
  116                 /*XXX need alg check???*/
  117                 if (sav->tdb_authalgxform != NULL && sav->replay)
  118                         size += ah_hdrsiz(sav);
  119         } else {
  120                 /*
  121                  *   base header size
  122                  * + max iv length for CBC mode
  123                  * + max pad length
  124                  * + sizeof (pad length field)
  125                  * + sizeof (next header field)
  126                  * + max icv supported.
  127                  */
  128                 size = sizeof (struct newesp) + EALG_MAX_BLOCK_LEN + 9 + 16;
  129         }
  130         return size;
  131 }
  132 
  133 /*
  134  * esp_init() is called when an SPI is being set up.
  135  */
  136 static int
  137 esp_init(struct secasvar *sav, struct xformsw *xsp)
  138 {
  139         const struct enc_xform *txform;
  140         struct cryptoini cria, crie;
  141         int keylen;
  142         int error;
  143 
  144         txform = enc_algorithm_lookup(sav->alg_enc);
  145         if (txform == NULL) {
  146                 DPRINTF(("%s: unsupported encryption algorithm %d\n",
  147                         __func__, sav->alg_enc));
  148                 return EINVAL;
  149         }
  150         if (sav->key_enc == NULL) {
  151                 DPRINTF(("%s: no encoding key for %s algorithm\n",
  152                          __func__, txform->name));
  153                 return EINVAL;
  154         }
  155         if ((sav->flags & (SADB_X_EXT_OLD | SADB_X_EXT_IV4B)) ==
  156             SADB_X_EXT_IV4B) {
  157                 DPRINTF(("%s: 4-byte IV not supported with protocol\n",
  158                         __func__));
  159                 return EINVAL;
  160         }
  161 
  162         switch (sav->alg_enc) {
  163         case SADB_EALG_DESCBC:
  164                 if (ratecheck(&deswarn, &ipsec_warn_interval))
  165                         gone_in(13, "DES cipher for IPsec");
  166                 break;
  167         case SADB_X_EALG_BLOWFISHCBC:
  168                 if (ratecheck(&blfwarn, &ipsec_warn_interval))
  169                         gone_in(13, "Blowfish cipher for IPsec");
  170                 break;
  171         case SADB_X_EALG_CAST128CBC:
  172                 if (ratecheck(&castwarn, &ipsec_warn_interval))
  173                         gone_in(13, "CAST cipher for IPsec");
  174                 break;
  175         case SADB_X_EALG_CAMELLIACBC:
  176                 if (ratecheck(&camelliawarn, &ipsec_warn_interval))
  177                         gone_in(13, "Camellia cipher for IPsec");
  178                 break;
  179         }
  180 
  181         /* subtract off the salt, RFC4106, 8.1 and RFC3686, 5.1 */
  182         keylen = _KEYLEN(sav->key_enc) - SAV_ISCTRORGCM(sav) * 4;
  183         if (txform->minkey > keylen || keylen > txform->maxkey) {
  184                 DPRINTF(("%s: invalid key length %u, must be in the range "
  185                         "[%u..%u] for algorithm %s\n", __func__,
  186                         keylen, txform->minkey, txform->maxkey,
  187                         txform->name));
  188                 return EINVAL;
  189         }
  190 
  191         if (SAV_ISCTRORGCM(sav))
  192                 sav->ivlen = 8; /* RFC4106 3.1 and RFC3686 3.1 */
  193         else
  194                 sav->ivlen = txform->ivsize;
  195 
  196         /*
  197          * Setup AH-related state.
  198          */
  199         if (sav->alg_auth != 0) {
  200                 error = ah_init0(sav, xsp, &cria);
  201                 if (error)
  202                         return error;
  203         }
  204 
  205         /* NB: override anything set in ah_init0 */
  206         sav->tdb_xform = xsp;
  207         sav->tdb_encalgxform = txform;
  208 
  209         /*
  210          * Whenever AES-GCM is used for encryption, one
  211          * of the AES authentication algorithms is chosen
  212          * as well, based on the key size.
  213          */
  214         if (sav->alg_enc == SADB_X_EALG_AESGCM16) {
  215                 switch (keylen) {
  216                 case AES_128_GMAC_KEY_LEN:
  217                         sav->alg_auth = SADB_X_AALG_AES128GMAC;
  218                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_128;
  219                         break;
  220                 case AES_192_GMAC_KEY_LEN:
  221                         sav->alg_auth = SADB_X_AALG_AES192GMAC;
  222                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_192;
  223                         break;
  224                 case AES_256_GMAC_KEY_LEN:
  225                         sav->alg_auth = SADB_X_AALG_AES256GMAC;
  226                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_256;
  227                         break;
  228                 default:
  229                         DPRINTF(("%s: invalid key length %u"
  230                                  "for algorithm %s\n", __func__,
  231                                  keylen, txform->name));
  232                         return EINVAL;
  233                 }
  234                 bzero(&cria, sizeof(cria));
  235                 cria.cri_alg = sav->tdb_authalgxform->type;
  236                 cria.cri_key = sav->key_enc->key_data;
  237                 cria.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISGCM(sav) * 32;
  238         }
  239 
  240         /* Initialize crypto session. */
  241         bzero(&crie, sizeof(crie));
  242         crie.cri_alg = sav->tdb_encalgxform->type;
  243         crie.cri_key = sav->key_enc->key_data;
  244         crie.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISCTRORGCM(sav) * 32;
  245 
  246         if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
  247                 /* init both auth & enc */
  248                 crie.cri_next = &cria;
  249                 error = crypto_newsession(&sav->tdb_cryptoid,
  250                                           &crie, V_crypto_support);
  251         } else if (sav->tdb_encalgxform) {
  252                 error = crypto_newsession(&sav->tdb_cryptoid,
  253                                           &crie, V_crypto_support);
  254         } else if (sav->tdb_authalgxform) {
  255                 error = crypto_newsession(&sav->tdb_cryptoid,
  256                                           &cria, V_crypto_support);
  257         } else {
  258                 /* XXX cannot happen? */
  259                 DPRINTF(("%s: no encoding OR authentication xform!\n",
  260                         __func__));
  261                 error = EINVAL;
  262         }
  263         return error;
  264 }
  265 
  266 /*
  267  * Paranoia.
  268  */
  269 static int
  270 esp_zeroize(struct secasvar *sav)
  271 {
  272         /* NB: ah_zerorize free's the crypto session state */
  273         int error = ah_zeroize(sav);
  274 
  275         if (sav->key_enc)
  276                 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
  277         sav->tdb_encalgxform = NULL;
  278         sav->tdb_xform = NULL;
  279         return error;
  280 }
  281 
  282 /*
  283  * ESP input processing, called (eventually) through the protocol switch.
  284  */
  285 static int
  286 esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  287 {
  288         IPSEC_DEBUG_DECLARE(char buf[128]);
  289         const struct auth_hash *esph;
  290         const struct enc_xform *espx;
  291         struct xform_data *xd;
  292         struct cryptodesc *crde;
  293         struct cryptop *crp;
  294         struct newesp *esp;
  295         uint8_t *ivp;
  296         uint64_t cryptoid;
  297         int alen, error, hlen, plen;
  298 
  299         IPSEC_ASSERT(sav != NULL, ("null SA"));
  300         IPSEC_ASSERT(sav->tdb_encalgxform != NULL, ("null encoding xform"));
  301 
  302         error = EINVAL;
  303         /* Valid IP Packet length ? */
  304         if ( (skip&3) || (m->m_pkthdr.len&3) ){
  305                 DPRINTF(("%s: misaligned packet, skip %u pkt len %u",
  306                                 __func__, skip, m->m_pkthdr.len));
  307                 ESPSTAT_INC(esps_badilen);
  308                 goto bad;
  309         }
  310         /* XXX don't pullup, just copy header */
  311         IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
  312 
  313         esph = sav->tdb_authalgxform;
  314         espx = sav->tdb_encalgxform;
  315 
  316         /* Determine the ESP header and auth length */
  317         if (sav->flags & SADB_X_EXT_OLD)
  318                 hlen = sizeof (struct esp) + sav->ivlen;
  319         else
  320                 hlen = sizeof (struct newesp) + sav->ivlen;
  321 
  322         alen = xform_ah_authsize(esph);
  323 
  324         /*
  325          * Verify payload length is multiple of encryption algorithm
  326          * block size.
  327          *
  328          * NB: This works for the null algorithm because the blocksize
  329          *     is 4 and all packets must be 4-byte aligned regardless
  330          *     of the algorithm.
  331          */
  332         plen = m->m_pkthdr.len - (skip + hlen + alen);
  333         if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
  334                 DPRINTF(("%s: payload of %d octets not a multiple of %d octets,"
  335                     "  SA %s/%08lx\n", __func__, plen, espx->blocksize,
  336                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  337                     (u_long)ntohl(sav->spi)));
  338                 ESPSTAT_INC(esps_badilen);
  339                 goto bad;
  340         }
  341 
  342         /*
  343          * Check sequence number.
  344          */
  345         SECASVAR_LOCK(sav);
  346         if (esph != NULL && sav->replay != NULL && sav->replay->wsize != 0) {
  347                 if (ipsec_chkreplay(ntohl(esp->esp_seq), sav) == 0) {
  348                         SECASVAR_UNLOCK(sav);
  349                         DPRINTF(("%s: packet replay check for %s\n", __func__,
  350                             ipsec_sa2str(sav, buf, sizeof(buf))));
  351                         ESPSTAT_INC(esps_replay);
  352                         error = EACCES;
  353                         goto bad;
  354                 }
  355         }
  356         cryptoid = sav->tdb_cryptoid;
  357         SECASVAR_UNLOCK(sav);
  358 
  359         /* Update the counters */
  360         ESPSTAT_ADD(esps_ibytes, m->m_pkthdr.len - (skip + hlen + alen));
  361 
  362         /* Get crypto descriptors */
  363         crp = crypto_getreq(esph && espx ? 2 : 1);
  364         if (crp == NULL) {
  365                 DPRINTF(("%s: failed to acquire crypto descriptors\n",
  366                         __func__));
  367                 ESPSTAT_INC(esps_crypto);
  368                 error = ENOBUFS;
  369                 goto bad;
  370         }
  371 
  372         /* Get IPsec-specific opaque pointer */
  373         xd = malloc(sizeof(*xd) + alen, M_XDATA, M_NOWAIT | M_ZERO);
  374         if (xd == NULL) {
  375                 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
  376                 ESPSTAT_INC(esps_crypto);
  377                 crypto_freereq(crp);
  378                 error = ENOBUFS;
  379                 goto bad;
  380         }
  381 
  382         if (esph != NULL) {
  383                 struct cryptodesc *crda = crp->crp_desc;
  384 
  385                 IPSEC_ASSERT(crda != NULL, ("null ah crypto descriptor"));
  386 
  387                 /* Authentication descriptor */
  388                 crda->crd_skip = skip;
  389                 if (SAV_ISGCM(sav))
  390                         crda->crd_len = 8;      /* RFC4106 5, SPI + SN */
  391                 else
  392                         crda->crd_len = m->m_pkthdr.len - (skip + alen);
  393                 crda->crd_inject = m->m_pkthdr.len - alen;
  394 
  395                 crda->crd_alg = esph->type;
  396 
  397                 /* Copy the authenticator */
  398                 m_copydata(m, m->m_pkthdr.len - alen, alen,
  399                     (caddr_t) (xd + 1));
  400 
  401                 /* Chain authentication request */
  402                 crde = crda->crd_next;
  403         } else {
  404                 crde = crp->crp_desc;
  405         }
  406 
  407         /* Crypto operation descriptor */
  408         crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
  409         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  410         crp->crp_buf = (caddr_t) m;
  411         crp->crp_callback = esp_input_cb;
  412         crp->crp_sid = cryptoid;
  413         crp->crp_opaque = (caddr_t) xd;
  414 
  415         /* These are passed as-is to the callback */
  416         xd->sav = sav;
  417         xd->protoff = protoff;
  418         xd->skip = skip;
  419         xd->cryptoid = cryptoid;
  420         xd->vnet = curvnet;
  421 
  422         /* Decryption descriptor */
  423         IPSEC_ASSERT(crde != NULL, ("null esp crypto descriptor"));
  424         crde->crd_skip = skip + hlen;
  425         crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  426         crde->crd_inject = skip + hlen - sav->ivlen;
  427 
  428         if (SAV_ISCTRORGCM(sav)) {
  429                 ivp = &crde->crd_iv[0];
  430 
  431                 /* GCM IV Format: RFC4106 4 */
  432                 /* CTR IV Format: RFC3686 4 */
  433                 /* Salt is last four bytes of key, RFC4106 8.1 */
  434                 /* Nonce is last four bytes of key, RFC3686 5.1 */
  435                 memcpy(ivp, sav->key_enc->key_data +
  436                     _KEYLEN(sav->key_enc) - 4, 4);
  437 
  438                 if (SAV_ISCTR(sav)) {
  439                         /* Initial block counter is 1, RFC3686 4 */
  440                         be32enc(&ivp[sav->ivlen + 4], 1);
  441                 }
  442 
  443                 m_copydata(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
  444                 crde->crd_flags |= CRD_F_IV_EXPLICIT;
  445         }
  446 
  447         crde->crd_alg = espx->type;
  448 
  449         return (crypto_dispatch(crp));
  450 bad:
  451         m_freem(m);
  452         key_freesav(&sav);
  453         return (error);
  454 }
  455 
  456 /*
  457  * ESP input callback from the crypto driver.
  458  */
  459 static int
  460 esp_input_cb(struct cryptop *crp)
  461 {
  462         IPSEC_DEBUG_DECLARE(char buf[128]);
  463         u_int8_t lastthree[3], aalg[AH_HMAC_MAXHASHLEN];
  464         const struct auth_hash *esph;
  465         const struct enc_xform *espx;
  466         struct mbuf *m;
  467         struct cryptodesc *crd;
  468         struct xform_data *xd;
  469         struct secasvar *sav;
  470         struct secasindex *saidx;
  471         caddr_t ptr;
  472         uint64_t cryptoid;
  473         int hlen, skip, protoff, error, alen;
  474 
  475         crd = crp->crp_desc;
  476         IPSEC_ASSERT(crd != NULL, ("null crypto descriptor!"));
  477 
  478         m = (struct mbuf *) crp->crp_buf;
  479         xd = (struct xform_data *) crp->crp_opaque;
  480         CURVNET_SET(xd->vnet);
  481         sav = xd->sav;
  482         skip = xd->skip;
  483         protoff = xd->protoff;
  484         cryptoid = xd->cryptoid;
  485         saidx = &sav->sah->saidx;
  486         esph = sav->tdb_authalgxform;
  487         espx = sav->tdb_encalgxform;
  488 
  489         /* Check for crypto errors */
  490         if (crp->crp_etype) {
  491                 if (crp->crp_etype == EAGAIN) {
  492                         /* Reset the session ID */
  493                         if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
  494                                 crypto_freesession(cryptoid);
  495                         xd->cryptoid = crp->crp_sid;
  496                         CURVNET_RESTORE();
  497                         return (crypto_dispatch(crp));
  498                 }
  499                 ESPSTAT_INC(esps_noxform);
  500                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  501                 error = crp->crp_etype;
  502                 goto bad;
  503         }
  504 
  505         /* Shouldn't happen... */
  506         if (m == NULL) {
  507                 ESPSTAT_INC(esps_crypto);
  508                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
  509                 error = EINVAL;
  510                 goto bad;
  511         }
  512         ESPSTAT_INC(esps_hist[sav->alg_enc]);
  513 
  514         /* If authentication was performed, check now. */
  515         if (esph != NULL) {
  516                 alen = xform_ah_authsize(esph);
  517                 AHSTAT_INC(ahs_hist[sav->alg_auth]);
  518                 /* Copy the authenticator from the packet */
  519                 m_copydata(m, m->m_pkthdr.len - alen, alen, aalg);
  520                 ptr = (caddr_t) (xd + 1);
  521 
  522                 /* Verify authenticator */
  523                 if (timingsafe_bcmp(ptr, aalg, alen) != 0) {
  524                         DPRINTF(("%s: authentication hash mismatch for "
  525                             "packet in SA %s/%08lx\n", __func__,
  526                             ipsec_address(&saidx->dst, buf, sizeof(buf)),
  527                             (u_long) ntohl(sav->spi)));
  528                         ESPSTAT_INC(esps_badauth);
  529                         error = EACCES;
  530                         goto bad;
  531                 }
  532                 m->m_flags |= M_AUTHIPDGM;
  533                 /* Remove trailing authenticator */
  534                 m_adj(m, -alen);
  535         }
  536 
  537         /* Release the crypto descriptors */
  538         free(xd, M_XDATA), xd = NULL;
  539         crypto_freereq(crp), crp = NULL;
  540 
  541         /*
  542          * Packet is now decrypted.
  543          */
  544         m->m_flags |= M_DECRYPTED;
  545 
  546         /*
  547          * Update replay sequence number, if appropriate.
  548          */
  549         if (sav->replay) {
  550                 u_int32_t seq;
  551 
  552                 m_copydata(m, skip + offsetof(struct newesp, esp_seq),
  553                            sizeof (seq), (caddr_t) &seq);
  554                 SECASVAR_LOCK(sav);
  555                 if (ipsec_updatereplay(ntohl(seq), sav)) {
  556                         SECASVAR_UNLOCK(sav);
  557                         DPRINTF(("%s: packet replay check for %s\n", __func__,
  558                             ipsec_sa2str(sav, buf, sizeof(buf))));
  559                         ESPSTAT_INC(esps_replay);
  560                         error = EACCES;
  561                         goto bad;
  562                 }
  563                 SECASVAR_UNLOCK(sav);
  564         }
  565 
  566         /* Determine the ESP header length */
  567         if (sav->flags & SADB_X_EXT_OLD)
  568                 hlen = sizeof (struct esp) + sav->ivlen;
  569         else
  570                 hlen = sizeof (struct newesp) + sav->ivlen;
  571 
  572         /* Remove the ESP header and IV from the mbuf. */
  573         error = m_striphdr(m, skip, hlen);
  574         if (error) {
  575                 ESPSTAT_INC(esps_hdrops);
  576                 DPRINTF(("%s: bad mbuf chain, SA %s/%08lx\n", __func__,
  577                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  578                     (u_long) ntohl(sav->spi)));
  579                 goto bad;
  580         }
  581 
  582         /* Save the last three bytes of decrypted data */
  583         m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
  584 
  585         /* Verify pad length */
  586         if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
  587                 ESPSTAT_INC(esps_badilen);
  588                 DPRINTF(("%s: invalid padding length %d for %u byte packet "
  589                     "in SA %s/%08lx\n", __func__, lastthree[1],
  590                     m->m_pkthdr.len - skip,
  591                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  592                     (u_long) ntohl(sav->spi)));
  593                 error = EINVAL;
  594                 goto bad;
  595         }
  596 
  597         /* Verify correct decryption by checking the last padding bytes */
  598         if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
  599                 if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
  600                         ESPSTAT_INC(esps_badenc);
  601                         DPRINTF(("%s: decryption failed for packet in "
  602                             "SA %s/%08lx\n", __func__, ipsec_address(
  603                             &sav->sah->saidx.dst, buf, sizeof(buf)),
  604                             (u_long) ntohl(sav->spi)));
  605                         error = EINVAL;
  606                         goto bad;
  607                 }
  608         }
  609 
  610         /*
  611          * RFC4303 2.6:
  612          * Silently drop packet if next header field is IPPROTO_NONE.
  613          */
  614         if (lastthree[2] == IPPROTO_NONE)
  615                 goto bad;
  616 
  617         /* Trim the mbuf chain to remove trailing authenticator and padding */
  618         m_adj(m, -(lastthree[1] + 2));
  619 
  620         /* Restore the Next Protocol field */
  621         m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
  622 
  623         switch (saidx->dst.sa.sa_family) {
  624 #ifdef INET6
  625         case AF_INET6:
  626                 error = ipsec6_common_input_cb(m, sav, skip, protoff);
  627                 break;
  628 #endif
  629 #ifdef INET
  630         case AF_INET:
  631                 error = ipsec4_common_input_cb(m, sav, skip, protoff);
  632                 break;
  633 #endif
  634         default:
  635                 panic("%s: Unexpected address family: %d saidx=%p", __func__,
  636                     saidx->dst.sa.sa_family, saidx);
  637         }
  638         CURVNET_RESTORE();
  639         return error;
  640 bad:
  641         CURVNET_RESTORE();
  642         if (sav != NULL)
  643                 key_freesav(&sav);
  644         if (m != NULL)
  645                 m_freem(m);
  646         if (xd != NULL)
  647                 free(xd, M_XDATA);
  648         if (crp != NULL)
  649                 crypto_freereq(crp);
  650         return error;
  651 }
  652 /*
  653  * ESP output routine, called by ipsec[46]_perform_request().
  654  */
  655 static int
  656 esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
  657     u_int idx, int skip, int protoff)
  658 {
  659         IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  660         struct cryptodesc *crde = NULL, *crda = NULL;
  661         struct cryptop *crp;
  662         const struct auth_hash *esph;
  663         const struct enc_xform *espx;
  664         struct mbuf *mo = NULL;
  665         struct xform_data *xd;
  666         struct secasindex *saidx;
  667         unsigned char *pad;
  668         uint8_t *ivp;
  669         uint64_t cntr, cryptoid;
  670         int hlen, rlen, padding, blks, alen, i, roff;
  671         int error, maxpacketsize;
  672         uint8_t prot;
  673 
  674         IPSEC_ASSERT(sav != NULL, ("null SA"));
  675         esph = sav->tdb_authalgxform;
  676         espx = sav->tdb_encalgxform;
  677         IPSEC_ASSERT(espx != NULL, ("null encoding xform"));
  678 
  679         if (sav->flags & SADB_X_EXT_OLD)
  680                 hlen = sizeof (struct esp) + sav->ivlen;
  681         else
  682                 hlen = sizeof (struct newesp) + sav->ivlen;
  683 
  684         rlen = m->m_pkthdr.len - skip;  /* Raw payload length. */
  685         /*
  686          * RFC4303 2.4 Requires 4 byte alignment.
  687          */
  688         blks = MAX(4, espx->blocksize);         /* Cipher blocksize */
  689 
  690         /* XXX clamp padding length a la KAME??? */
  691         padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
  692 
  693         alen = xform_ah_authsize(esph);
  694 
  695         ESPSTAT_INC(esps_output);
  696 
  697         saidx = &sav->sah->saidx;
  698         /* Check for maximum packet size violations. */
  699         switch (saidx->dst.sa.sa_family) {
  700 #ifdef INET
  701         case AF_INET:
  702                 maxpacketsize = IP_MAXPACKET;
  703                 break;
  704 #endif /* INET */
  705 #ifdef INET6
  706         case AF_INET6:
  707                 maxpacketsize = IPV6_MAXPACKET;
  708                 break;
  709 #endif /* INET6 */
  710         default:
  711                 DPRINTF(("%s: unknown/unsupported protocol "
  712                     "family %d, SA %s/%08lx\n", __func__,
  713                     saidx->dst.sa.sa_family, ipsec_address(&saidx->dst,
  714                         buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
  715                 ESPSTAT_INC(esps_nopf);
  716                 error = EPFNOSUPPORT;
  717                 goto bad;
  718         }
  719         /*
  720         DPRINTF(("%s: skip %d hlen %d rlen %d padding %d alen %d blksd %d\n",
  721                 __func__, skip, hlen, rlen, padding, alen, blks)); */
  722         if (skip + hlen + rlen + padding + alen > maxpacketsize) {
  723                 DPRINTF(("%s: packet in SA %s/%08lx got too big "
  724                     "(len %u, max len %u)\n", __func__,
  725                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  726                     (u_long) ntohl(sav->spi),
  727                     skip + hlen + rlen + padding + alen, maxpacketsize));
  728                 ESPSTAT_INC(esps_toobig);
  729                 error = EMSGSIZE;
  730                 goto bad;
  731         }
  732 
  733         /* Update the counters. */
  734         ESPSTAT_ADD(esps_obytes, m->m_pkthdr.len - skip);
  735 
  736         m = m_unshare(m, M_NOWAIT);
  737         if (m == NULL) {
  738                 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
  739                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  740                     (u_long) ntohl(sav->spi)));
  741                 ESPSTAT_INC(esps_hdrops);
  742                 error = ENOBUFS;
  743                 goto bad;
  744         }
  745 
  746         /* Inject ESP header. */
  747         mo = m_makespace(m, skip, hlen, &roff);
  748         if (mo == NULL) {
  749                 DPRINTF(("%s: %u byte ESP hdr inject failed for SA %s/%08lx\n",
  750                     __func__, hlen, ipsec_address(&saidx->dst, buf,
  751                     sizeof(buf)), (u_long) ntohl(sav->spi)));
  752                 ESPSTAT_INC(esps_hdrops);       /* XXX diffs from openbsd */
  753                 error = ENOBUFS;
  754                 goto bad;
  755         }
  756 
  757         /* Initialize ESP header. */
  758         bcopy((caddr_t) &sav->spi, mtod(mo, caddr_t) + roff,
  759             sizeof(uint32_t));
  760         SECASVAR_LOCK(sav);
  761         if (sav->replay) {
  762                 uint32_t replay;
  763 
  764 #ifdef REGRESSION
  765                 /* Emulate replay attack when ipsec_replay is TRUE. */
  766                 if (!V_ipsec_replay)
  767 #endif
  768                         sav->replay->count++;
  769                 replay = htonl(sav->replay->count);
  770 
  771                 bcopy((caddr_t) &replay, mtod(mo, caddr_t) + roff +
  772                     sizeof(uint32_t), sizeof(uint32_t));
  773         }
  774         cryptoid = sav->tdb_cryptoid;
  775         if (SAV_ISCTRORGCM(sav))
  776                 cntr = sav->cntr++;
  777         SECASVAR_UNLOCK(sav);
  778 
  779         /*
  780          * Add padding -- better to do it ourselves than use the crypto engine,
  781          * although if/when we support compression, we'd have to do that.
  782          */
  783         pad = (u_char *) m_pad(m, padding + alen);
  784         if (pad == NULL) {
  785                 DPRINTF(("%s: m_pad failed for SA %s/%08lx\n", __func__,
  786                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  787                     (u_long) ntohl(sav->spi)));
  788                 m = NULL;               /* NB: free'd by m_pad */
  789                 error = ENOBUFS;
  790                 goto bad;
  791         }
  792 
  793         /*
  794          * Add padding: random, zero, or self-describing.
  795          * XXX catch unexpected setting
  796          */
  797         switch (sav->flags & SADB_X_EXT_PMASK) {
  798         case SADB_X_EXT_PRAND:
  799                 (void) read_random(pad, padding - 2);
  800                 break;
  801         case SADB_X_EXT_PZERO:
  802                 bzero(pad, padding - 2);
  803                 break;
  804         case SADB_X_EXT_PSEQ:
  805                 for (i = 0; i < padding - 2; i++)
  806                         pad[i] = i+1;
  807                 break;
  808         }
  809 
  810         /* Fix padding length and Next Protocol in padding itself. */
  811         pad[padding - 2] = padding - 2;
  812         m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
  813 
  814         /* Fix Next Protocol in IPv4/IPv6 header. */
  815         prot = IPPROTO_ESP;
  816         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
  817 
  818         /* Get crypto descriptors. */
  819         crp = crypto_getreq(esph != NULL ? 2 : 1);
  820         if (crp == NULL) {
  821                 DPRINTF(("%s: failed to acquire crypto descriptors\n",
  822                         __func__));
  823                 ESPSTAT_INC(esps_crypto);
  824                 error = ENOBUFS;
  825                 goto bad;
  826         }
  827 
  828         /* IPsec-specific opaque crypto info. */
  829         xd =  malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
  830         if (xd == NULL) {
  831                 crypto_freereq(crp);
  832                 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
  833                 ESPSTAT_INC(esps_crypto);
  834                 error = ENOBUFS;
  835                 goto bad;
  836         }
  837 
  838         crde = crp->crp_desc;
  839         crda = crde->crd_next;
  840 
  841         /* Encryption descriptor. */
  842         crde->crd_skip = skip + hlen;
  843         crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  844         crde->crd_flags = CRD_F_ENCRYPT;
  845         crde->crd_inject = skip + hlen - sav->ivlen;
  846 
  847         /* Encryption operation. */
  848         crde->crd_alg = espx->type;
  849         if (SAV_ISCTRORGCM(sav)) {
  850                 ivp = &crde->crd_iv[0];
  851 
  852                 /* GCM IV Format: RFC4106 4 */
  853                 /* CTR IV Format: RFC3686 4 */
  854                 /* Salt is last four bytes of key, RFC4106 8.1 */
  855                 /* Nonce is last four bytes of key, RFC3686 5.1 */
  856                 memcpy(ivp, sav->key_enc->key_data +
  857                     _KEYLEN(sav->key_enc) - 4, 4);
  858                 be64enc(&ivp[4], cntr);
  859                 if (SAV_ISCTR(sav)) {
  860                         /* Initial block counter is 1, RFC3686 4 */
  861                         /* XXXAE: should we use this only for first packet? */
  862                         be32enc(&ivp[sav->ivlen + 4], 1);
  863                 }
  864 
  865                 m_copyback(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
  866                 crde->crd_flags |= CRD_F_IV_EXPLICIT|CRD_F_IV_PRESENT;
  867         }
  868 
  869         /* Callback parameters */
  870         xd->sp = sp;
  871         xd->sav = sav;
  872         xd->idx = idx;
  873         xd->cryptoid = cryptoid;
  874         xd->vnet = curvnet;
  875 
  876         /* Crypto operation descriptor. */
  877         crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
  878         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  879         crp->crp_buf = (caddr_t) m;
  880         crp->crp_callback = esp_output_cb;
  881         crp->crp_opaque = (caddr_t) xd;
  882         crp->crp_sid = cryptoid;
  883 
  884         if (esph) {
  885                 /* Authentication descriptor. */
  886                 crda->crd_alg = esph->type;
  887                 crda->crd_skip = skip;
  888                 if (SAV_ISGCM(sav))
  889                         crda->crd_len = 8;      /* RFC4106 5, SPI + SN */
  890                 else
  891                         crda->crd_len = m->m_pkthdr.len - (skip + alen);
  892                 crda->crd_inject = m->m_pkthdr.len - alen;
  893         }
  894 
  895         return crypto_dispatch(crp);
  896 bad:
  897         if (m)
  898                 m_freem(m);
  899         key_freesav(&sav);
  900         key_freesp(&sp);
  901         return (error);
  902 }
  903 /*
  904  * ESP output callback from the crypto driver.
  905  */
  906 static int
  907 esp_output_cb(struct cryptop *crp)
  908 {
  909         struct xform_data *xd;
  910         struct secpolicy *sp;
  911         struct secasvar *sav;
  912         struct mbuf *m;
  913         uint64_t cryptoid;
  914         u_int idx;
  915         int error;
  916 
  917         xd = (struct xform_data *) crp->crp_opaque;
  918         CURVNET_SET(xd->vnet);
  919         m = (struct mbuf *) crp->crp_buf;
  920         sp = xd->sp;
  921         sav = xd->sav;
  922         idx = xd->idx;
  923         cryptoid = xd->cryptoid;
  924 
  925         /* Check for crypto errors. */
  926         if (crp->crp_etype) {
  927                 if (crp->crp_etype == EAGAIN) {
  928                         /* Reset the session ID */
  929                         if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
  930                                 crypto_freesession(cryptoid);
  931                         xd->cryptoid = crp->crp_sid;
  932                         CURVNET_RESTORE();
  933                         return (crypto_dispatch(crp));
  934                 }
  935                 ESPSTAT_INC(esps_noxform);
  936                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  937                 error = crp->crp_etype;
  938                 m_freem(m);
  939                 goto bad;
  940         }
  941 
  942         /* Shouldn't happen... */
  943         if (m == NULL) {
  944                 ESPSTAT_INC(esps_crypto);
  945                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
  946                 error = EINVAL;
  947                 goto bad;
  948         }
  949         free(xd, M_XDATA);
  950         crypto_freereq(crp);
  951         ESPSTAT_INC(esps_hist[sav->alg_enc]);
  952         if (sav->tdb_authalgxform != NULL)
  953                 AHSTAT_INC(ahs_hist[sav->alg_auth]);
  954 
  955 #ifdef REGRESSION
  956         /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
  957         if (V_ipsec_integrity) {
  958                 static unsigned char ipseczeroes[AH_HMAC_MAXHASHLEN];
  959                 const struct auth_hash *esph;
  960 
  961                 /*
  962                  * Corrupt HMAC if we want to test integrity verification of
  963                  * the other side.
  964                  */
  965                 esph = sav->tdb_authalgxform;
  966                 if (esph !=  NULL) {
  967                         int alen;
  968 
  969                         alen = xform_ah_authsize(esph);
  970                         m_copyback(m, m->m_pkthdr.len - alen,
  971                             alen, ipseczeroes);
  972                 }
  973         }
  974 #endif
  975 
  976         /* NB: m is reclaimed by ipsec_process_done. */
  977         error = ipsec_process_done(m, sp, sav, idx);
  978         CURVNET_RESTORE();
  979         return (error);
  980 bad:
  981         CURVNET_RESTORE();
  982         free(xd, M_XDATA);
  983         crypto_freereq(crp);
  984         key_freesav(&sav);
  985         key_freesp(&sp);
  986         return (error);
  987 }
  988 
  989 static struct xformsw esp_xformsw = {
  990         .xf_type =      XF_ESP,
  991         .xf_name =      "IPsec ESP",
  992         .xf_init =      esp_init,
  993         .xf_zeroize =   esp_zeroize,
  994         .xf_input =     esp_input,
  995         .xf_output =    esp_output,
  996 };
  997 
  998 SYSINIT(esp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
  999     xform_attach, &esp_xformsw);
 1000 SYSUNINIT(esp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
 1001     xform_detach, &esp_xformsw);

Cache object: 1b0a7bdaed0d6c69f9d95bf4f18a7591


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.