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 360721 2020-05-06 23:23:22Z jhb $  */
    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, tdeswarn;
   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_EALG_3DESCBC:
  168                 if (ratecheck(&tdeswarn, &ipsec_warn_interval))
  169                         gone_in(13, "3DES cipher for IPsec");
  170                 break;
  171         case SADB_X_EALG_BLOWFISHCBC:
  172                 if (ratecheck(&blfwarn, &ipsec_warn_interval))
  173                         gone_in(13, "Blowfish cipher for IPsec");
  174                 break;
  175         case SADB_X_EALG_CAST128CBC:
  176                 if (ratecheck(&castwarn, &ipsec_warn_interval))
  177                         gone_in(13, "CAST cipher for IPsec");
  178                 break;
  179         case SADB_X_EALG_CAMELLIACBC:
  180                 if (ratecheck(&camelliawarn, &ipsec_warn_interval))
  181                         gone_in(13, "Camellia cipher for IPsec");
  182                 break;
  183         }
  184 
  185         /* subtract off the salt, RFC4106, 8.1 and RFC3686, 5.1 */
  186         keylen = _KEYLEN(sav->key_enc) - SAV_ISCTRORGCM(sav) * 4;
  187         if (txform->minkey > keylen || keylen > txform->maxkey) {
  188                 DPRINTF(("%s: invalid key length %u, must be in the range "
  189                         "[%u..%u] for algorithm %s\n", __func__,
  190                         keylen, txform->minkey, txform->maxkey,
  191                         txform->name));
  192                 return EINVAL;
  193         }
  194 
  195         if (SAV_ISCTRORGCM(sav))
  196                 sav->ivlen = 8; /* RFC4106 3.1 and RFC3686 3.1 */
  197         else
  198                 sav->ivlen = txform->ivsize;
  199 
  200         /*
  201          * Setup AH-related state.
  202          */
  203         if (sav->alg_auth != 0) {
  204                 error = ah_init0(sav, xsp, &cria);
  205                 if (error)
  206                         return error;
  207         }
  208 
  209         /* NB: override anything set in ah_init0 */
  210         sav->tdb_xform = xsp;
  211         sav->tdb_encalgxform = txform;
  212 
  213         /*
  214          * Whenever AES-GCM is used for encryption, one
  215          * of the AES authentication algorithms is chosen
  216          * as well, based on the key size.
  217          */
  218         if (sav->alg_enc == SADB_X_EALG_AESGCM16) {
  219                 switch (keylen) {
  220                 case AES_128_GMAC_KEY_LEN:
  221                         sav->alg_auth = SADB_X_AALG_AES128GMAC;
  222                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_128;
  223                         break;
  224                 case AES_192_GMAC_KEY_LEN:
  225                         sav->alg_auth = SADB_X_AALG_AES192GMAC;
  226                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_192;
  227                         break;
  228                 case AES_256_GMAC_KEY_LEN:
  229                         sav->alg_auth = SADB_X_AALG_AES256GMAC;
  230                         sav->tdb_authalgxform = &auth_hash_nist_gmac_aes_256;
  231                         break;
  232                 default:
  233                         DPRINTF(("%s: invalid key length %u"
  234                                  "for algorithm %s\n", __func__,
  235                                  keylen, txform->name));
  236                         return EINVAL;
  237                 }
  238                 bzero(&cria, sizeof(cria));
  239                 cria.cri_alg = sav->tdb_authalgxform->type;
  240                 cria.cri_key = sav->key_enc->key_data;
  241                 cria.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISGCM(sav) * 32;
  242         }
  243 
  244         /* Initialize crypto session. */
  245         bzero(&crie, sizeof(crie));
  246         crie.cri_alg = sav->tdb_encalgxform->type;
  247         crie.cri_key = sav->key_enc->key_data;
  248         crie.cri_klen = _KEYBITS(sav->key_enc) - SAV_ISCTRORGCM(sav) * 32;
  249 
  250         if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
  251                 /* init both auth & enc */
  252                 crie.cri_next = &cria;
  253                 error = crypto_newsession(&sav->tdb_cryptoid,
  254                                           &crie, V_crypto_support);
  255         } else if (sav->tdb_encalgxform) {
  256                 error = crypto_newsession(&sav->tdb_cryptoid,
  257                                           &crie, V_crypto_support);
  258         } else if (sav->tdb_authalgxform) {
  259                 error = crypto_newsession(&sav->tdb_cryptoid,
  260                                           &cria, V_crypto_support);
  261         } else {
  262                 /* XXX cannot happen? */
  263                 DPRINTF(("%s: no encoding OR authentication xform!\n",
  264                         __func__));
  265                 error = EINVAL;
  266         }
  267         return error;
  268 }
  269 
  270 /*
  271  * Paranoia.
  272  */
  273 static int
  274 esp_zeroize(struct secasvar *sav)
  275 {
  276         /* NB: ah_zerorize free's the crypto session state */
  277         int error = ah_zeroize(sav);
  278 
  279         if (sav->key_enc)
  280                 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
  281         sav->tdb_encalgxform = NULL;
  282         sav->tdb_xform = NULL;
  283         return error;
  284 }
  285 
  286 /*
  287  * ESP input processing, called (eventually) through the protocol switch.
  288  */
  289 static int
  290 esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  291 {
  292         IPSEC_DEBUG_DECLARE(char buf[128]);
  293         const struct auth_hash *esph;
  294         const struct enc_xform *espx;
  295         struct xform_data *xd;
  296         struct cryptodesc *crde;
  297         struct cryptop *crp;
  298         struct newesp *esp;
  299         uint8_t *ivp;
  300         uint64_t cryptoid;
  301         int alen, error, hlen, plen;
  302 
  303         IPSEC_ASSERT(sav != NULL, ("null SA"));
  304         IPSEC_ASSERT(sav->tdb_encalgxform != NULL, ("null encoding xform"));
  305 
  306         error = EINVAL;
  307         /* Valid IP Packet length ? */
  308         if ( (skip&3) || (m->m_pkthdr.len&3) ){
  309                 DPRINTF(("%s: misaligned packet, skip %u pkt len %u",
  310                                 __func__, skip, m->m_pkthdr.len));
  311                 ESPSTAT_INC(esps_badilen);
  312                 goto bad;
  313         }
  314         /* XXX don't pullup, just copy header */
  315         IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
  316 
  317         esph = sav->tdb_authalgxform;
  318         espx = sav->tdb_encalgxform;
  319 
  320         /* Determine the ESP header and auth length */
  321         if (sav->flags & SADB_X_EXT_OLD)
  322                 hlen = sizeof (struct esp) + sav->ivlen;
  323         else
  324                 hlen = sizeof (struct newesp) + sav->ivlen;
  325 
  326         alen = xform_ah_authsize(esph);
  327 
  328         /*
  329          * Verify payload length is multiple of encryption algorithm
  330          * block size.
  331          *
  332          * NB: This works for the null algorithm because the blocksize
  333          *     is 4 and all packets must be 4-byte aligned regardless
  334          *     of the algorithm.
  335          */
  336         plen = m->m_pkthdr.len - (skip + hlen + alen);
  337         if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
  338                 DPRINTF(("%s: payload of %d octets not a multiple of %d octets,"
  339                     "  SA %s/%08lx\n", __func__, plen, espx->blocksize,
  340                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  341                     (u_long)ntohl(sav->spi)));
  342                 ESPSTAT_INC(esps_badilen);
  343                 goto bad;
  344         }
  345 
  346         /*
  347          * Check sequence number.
  348          */
  349         SECASVAR_LOCK(sav);
  350         if (esph != NULL && sav->replay != NULL && sav->replay->wsize != 0) {
  351                 if (ipsec_chkreplay(ntohl(esp->esp_seq), sav) == 0) {
  352                         SECASVAR_UNLOCK(sav);
  353                         DPRINTF(("%s: packet replay check for %s\n", __func__,
  354                             ipsec_sa2str(sav, buf, sizeof(buf))));
  355                         ESPSTAT_INC(esps_replay);
  356                         error = EACCES;
  357                         goto bad;
  358                 }
  359         }
  360         cryptoid = sav->tdb_cryptoid;
  361         SECASVAR_UNLOCK(sav);
  362 
  363         /* Update the counters */
  364         ESPSTAT_ADD(esps_ibytes, m->m_pkthdr.len - (skip + hlen + alen));
  365 
  366         /* Get crypto descriptors */
  367         crp = crypto_getreq(esph && espx ? 2 : 1);
  368         if (crp == NULL) {
  369                 DPRINTF(("%s: failed to acquire crypto descriptors\n",
  370                         __func__));
  371                 ESPSTAT_INC(esps_crypto);
  372                 error = ENOBUFS;
  373                 goto bad;
  374         }
  375 
  376         /* Get IPsec-specific opaque pointer */
  377         xd = malloc(sizeof(*xd) + alen, M_XDATA, M_NOWAIT | M_ZERO);
  378         if (xd == NULL) {
  379                 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
  380                 ESPSTAT_INC(esps_crypto);
  381                 crypto_freereq(crp);
  382                 error = ENOBUFS;
  383                 goto bad;
  384         }
  385 
  386         if (esph != NULL) {
  387                 struct cryptodesc *crda = crp->crp_desc;
  388 
  389                 IPSEC_ASSERT(crda != NULL, ("null ah crypto descriptor"));
  390 
  391                 /* Authentication descriptor */
  392                 crda->crd_skip = skip;
  393                 if (SAV_ISGCM(sav))
  394                         crda->crd_len = 8;      /* RFC4106 5, SPI + SN */
  395                 else
  396                         crda->crd_len = m->m_pkthdr.len - (skip + alen);
  397                 crda->crd_inject = m->m_pkthdr.len - alen;
  398 
  399                 crda->crd_alg = esph->type;
  400 
  401                 /* Copy the authenticator */
  402                 m_copydata(m, m->m_pkthdr.len - alen, alen,
  403                     (caddr_t) (xd + 1));
  404 
  405                 /* Chain authentication request */
  406                 crde = crda->crd_next;
  407         } else {
  408                 crde = crp->crp_desc;
  409         }
  410 
  411         /* Crypto operation descriptor */
  412         crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
  413         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  414         crp->crp_buf = (caddr_t) m;
  415         crp->crp_callback = esp_input_cb;
  416         crp->crp_sid = cryptoid;
  417         crp->crp_opaque = (caddr_t) xd;
  418 
  419         /* These are passed as-is to the callback */
  420         xd->sav = sav;
  421         xd->protoff = protoff;
  422         xd->skip = skip;
  423         xd->cryptoid = cryptoid;
  424         xd->vnet = curvnet;
  425 
  426         /* Decryption descriptor */
  427         IPSEC_ASSERT(crde != NULL, ("null esp crypto descriptor"));
  428         crde->crd_skip = skip + hlen;
  429         crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  430         crde->crd_inject = skip + hlen - sav->ivlen;
  431 
  432         if (SAV_ISCTRORGCM(sav)) {
  433                 ivp = &crde->crd_iv[0];
  434 
  435                 /* GCM IV Format: RFC4106 4 */
  436                 /* CTR IV Format: RFC3686 4 */
  437                 /* Salt is last four bytes of key, RFC4106 8.1 */
  438                 /* Nonce is last four bytes of key, RFC3686 5.1 */
  439                 memcpy(ivp, sav->key_enc->key_data +
  440                     _KEYLEN(sav->key_enc) - 4, 4);
  441 
  442                 if (SAV_ISCTR(sav)) {
  443                         /* Initial block counter is 1, RFC3686 4 */
  444                         be32enc(&ivp[sav->ivlen + 4], 1);
  445                 }
  446 
  447                 m_copydata(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
  448                 crde->crd_flags |= CRD_F_IV_EXPLICIT;
  449         }
  450 
  451         crde->crd_alg = espx->type;
  452 
  453         return (crypto_dispatch(crp));
  454 bad:
  455         m_freem(m);
  456         key_freesav(&sav);
  457         return (error);
  458 }
  459 
  460 /*
  461  * ESP input callback from the crypto driver.
  462  */
  463 static int
  464 esp_input_cb(struct cryptop *crp)
  465 {
  466         IPSEC_DEBUG_DECLARE(char buf[128]);
  467         u_int8_t lastthree[3], aalg[AH_HMAC_MAXHASHLEN];
  468         const struct auth_hash *esph;
  469         const struct enc_xform *espx;
  470         struct mbuf *m;
  471         struct cryptodesc *crd;
  472         struct xform_data *xd;
  473         struct secasvar *sav;
  474         struct secasindex *saidx;
  475         caddr_t ptr;
  476         uint64_t cryptoid;
  477         int hlen, skip, protoff, error, alen;
  478 
  479         crd = crp->crp_desc;
  480         IPSEC_ASSERT(crd != NULL, ("null crypto descriptor!"));
  481 
  482         m = (struct mbuf *) crp->crp_buf;
  483         xd = (struct xform_data *) crp->crp_opaque;
  484         CURVNET_SET(xd->vnet);
  485         sav = xd->sav;
  486         skip = xd->skip;
  487         protoff = xd->protoff;
  488         cryptoid = xd->cryptoid;
  489         saidx = &sav->sah->saidx;
  490         esph = sav->tdb_authalgxform;
  491         espx = sav->tdb_encalgxform;
  492 
  493         /* Check for crypto errors */
  494         if (crp->crp_etype) {
  495                 if (crp->crp_etype == EAGAIN) {
  496                         /* Reset the session ID */
  497                         if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
  498                                 crypto_freesession(cryptoid);
  499                         xd->cryptoid = crp->crp_sid;
  500                         CURVNET_RESTORE();
  501                         return (crypto_dispatch(crp));
  502                 }
  503                 ESPSTAT_INC(esps_noxform);
  504                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  505                 error = crp->crp_etype;
  506                 goto bad;
  507         }
  508 
  509         /* Shouldn't happen... */
  510         if (m == NULL) {
  511                 ESPSTAT_INC(esps_crypto);
  512                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
  513                 error = EINVAL;
  514                 goto bad;
  515         }
  516         ESPSTAT_INC(esps_hist[sav->alg_enc]);
  517 
  518         /* If authentication was performed, check now. */
  519         if (esph != NULL) {
  520                 alen = xform_ah_authsize(esph);
  521                 AHSTAT_INC(ahs_hist[sav->alg_auth]);
  522                 /* Copy the authenticator from the packet */
  523                 m_copydata(m, m->m_pkthdr.len - alen, alen, aalg);
  524                 ptr = (caddr_t) (xd + 1);
  525 
  526                 /* Verify authenticator */
  527                 if (timingsafe_bcmp(ptr, aalg, alen) != 0) {
  528                         DPRINTF(("%s: authentication hash mismatch for "
  529                             "packet in SA %s/%08lx\n", __func__,
  530                             ipsec_address(&saidx->dst, buf, sizeof(buf)),
  531                             (u_long) ntohl(sav->spi)));
  532                         ESPSTAT_INC(esps_badauth);
  533                         error = EACCES;
  534                         goto bad;
  535                 }
  536                 m->m_flags |= M_AUTHIPDGM;
  537                 /* Remove trailing authenticator */
  538                 m_adj(m, -alen);
  539         }
  540 
  541         /* Release the crypto descriptors */
  542         free(xd, M_XDATA), xd = NULL;
  543         crypto_freereq(crp), crp = NULL;
  544 
  545         /*
  546          * Packet is now decrypted.
  547          */
  548         m->m_flags |= M_DECRYPTED;
  549 
  550         /*
  551          * Update replay sequence number, if appropriate.
  552          */
  553         if (sav->replay) {
  554                 u_int32_t seq;
  555 
  556                 m_copydata(m, skip + offsetof(struct newesp, esp_seq),
  557                            sizeof (seq), (caddr_t) &seq);
  558                 SECASVAR_LOCK(sav);
  559                 if (ipsec_updatereplay(ntohl(seq), sav)) {
  560                         SECASVAR_UNLOCK(sav);
  561                         DPRINTF(("%s: packet replay check for %s\n", __func__,
  562                             ipsec_sa2str(sav, buf, sizeof(buf))));
  563                         ESPSTAT_INC(esps_replay);
  564                         error = EACCES;
  565                         goto bad;
  566                 }
  567                 SECASVAR_UNLOCK(sav);
  568         }
  569 
  570         /* Determine the ESP header length */
  571         if (sav->flags & SADB_X_EXT_OLD)
  572                 hlen = sizeof (struct esp) + sav->ivlen;
  573         else
  574                 hlen = sizeof (struct newesp) + sav->ivlen;
  575 
  576         /* Remove the ESP header and IV from the mbuf. */
  577         error = m_striphdr(m, skip, hlen);
  578         if (error) {
  579                 ESPSTAT_INC(esps_hdrops);
  580                 DPRINTF(("%s: bad mbuf chain, SA %s/%08lx\n", __func__,
  581                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  582                     (u_long) ntohl(sav->spi)));
  583                 goto bad;
  584         }
  585 
  586         /* Save the last three bytes of decrypted data */
  587         m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
  588 
  589         /* Verify pad length */
  590         if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
  591                 ESPSTAT_INC(esps_badilen);
  592                 DPRINTF(("%s: invalid padding length %d for %u byte packet "
  593                     "in SA %s/%08lx\n", __func__, lastthree[1],
  594                     m->m_pkthdr.len - skip,
  595                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
  596                     (u_long) ntohl(sav->spi)));
  597                 error = EINVAL;
  598                 goto bad;
  599         }
  600 
  601         /* Verify correct decryption by checking the last padding bytes */
  602         if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
  603                 if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
  604                         ESPSTAT_INC(esps_badenc);
  605                         DPRINTF(("%s: decryption failed for packet in "
  606                             "SA %s/%08lx\n", __func__, ipsec_address(
  607                             &sav->sah->saidx.dst, buf, sizeof(buf)),
  608                             (u_long) ntohl(sav->spi)));
  609                         error = EINVAL;
  610                         goto bad;
  611                 }
  612         }
  613 
  614         /*
  615          * RFC4303 2.6:
  616          * Silently drop packet if next header field is IPPROTO_NONE.
  617          */
  618         if (lastthree[2] == IPPROTO_NONE)
  619                 goto bad;
  620 
  621         /* Trim the mbuf chain to remove trailing authenticator and padding */
  622         m_adj(m, -(lastthree[1] + 2));
  623 
  624         /* Restore the Next Protocol field */
  625         m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
  626 
  627         switch (saidx->dst.sa.sa_family) {
  628 #ifdef INET6
  629         case AF_INET6:
  630                 error = ipsec6_common_input_cb(m, sav, skip, protoff);
  631                 break;
  632 #endif
  633 #ifdef INET
  634         case AF_INET:
  635                 error = ipsec4_common_input_cb(m, sav, skip, protoff);
  636                 break;
  637 #endif
  638         default:
  639                 panic("%s: Unexpected address family: %d saidx=%p", __func__,
  640                     saidx->dst.sa.sa_family, saidx);
  641         }
  642         CURVNET_RESTORE();
  643         return error;
  644 bad:
  645         CURVNET_RESTORE();
  646         if (sav != NULL)
  647                 key_freesav(&sav);
  648         if (m != NULL)
  649                 m_freem(m);
  650         if (xd != NULL)
  651                 free(xd, M_XDATA);
  652         if (crp != NULL)
  653                 crypto_freereq(crp);
  654         return error;
  655 }
  656 /*
  657  * ESP output routine, called by ipsec[46]_perform_request().
  658  */
  659 static int
  660 esp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
  661     u_int idx, int skip, int protoff)
  662 {
  663         IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
  664         struct cryptodesc *crde = NULL, *crda = NULL;
  665         struct cryptop *crp;
  666         const struct auth_hash *esph;
  667         const struct enc_xform *espx;
  668         struct mbuf *mo = NULL;
  669         struct xform_data *xd;
  670         struct secasindex *saidx;
  671         unsigned char *pad;
  672         uint8_t *ivp;
  673         uint64_t cntr, cryptoid;
  674         int hlen, rlen, padding, blks, alen, i, roff;
  675         int error, maxpacketsize;
  676         uint8_t prot;
  677 
  678         IPSEC_ASSERT(sav != NULL, ("null SA"));
  679         esph = sav->tdb_authalgxform;
  680         espx = sav->tdb_encalgxform;
  681         IPSEC_ASSERT(espx != NULL, ("null encoding xform"));
  682 
  683         if (sav->flags & SADB_X_EXT_OLD)
  684                 hlen = sizeof (struct esp) + sav->ivlen;
  685         else
  686                 hlen = sizeof (struct newesp) + sav->ivlen;
  687 
  688         rlen = m->m_pkthdr.len - skip;  /* Raw payload length. */
  689         /*
  690          * RFC4303 2.4 Requires 4 byte alignment.
  691          */
  692         blks = MAX(4, espx->blocksize);         /* Cipher blocksize */
  693 
  694         /* XXX clamp padding length a la KAME??? */
  695         padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
  696 
  697         alen = xform_ah_authsize(esph);
  698 
  699         ESPSTAT_INC(esps_output);
  700 
  701         saidx = &sav->sah->saidx;
  702         /* Check for maximum packet size violations. */
  703         switch (saidx->dst.sa.sa_family) {
  704 #ifdef INET
  705         case AF_INET:
  706                 maxpacketsize = IP_MAXPACKET;
  707                 break;
  708 #endif /* INET */
  709 #ifdef INET6
  710         case AF_INET6:
  711                 maxpacketsize = IPV6_MAXPACKET;
  712                 break;
  713 #endif /* INET6 */
  714         default:
  715                 DPRINTF(("%s: unknown/unsupported protocol "
  716                     "family %d, SA %s/%08lx\n", __func__,
  717                     saidx->dst.sa.sa_family, ipsec_address(&saidx->dst,
  718                         buf, sizeof(buf)), (u_long) ntohl(sav->spi)));
  719                 ESPSTAT_INC(esps_nopf);
  720                 error = EPFNOSUPPORT;
  721                 goto bad;
  722         }
  723         /*
  724         DPRINTF(("%s: skip %d hlen %d rlen %d padding %d alen %d blksd %d\n",
  725                 __func__, skip, hlen, rlen, padding, alen, blks)); */
  726         if (skip + hlen + rlen + padding + alen > maxpacketsize) {
  727                 DPRINTF(("%s: packet in SA %s/%08lx got too big "
  728                     "(len %u, max len %u)\n", __func__,
  729                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  730                     (u_long) ntohl(sav->spi),
  731                     skip + hlen + rlen + padding + alen, maxpacketsize));
  732                 ESPSTAT_INC(esps_toobig);
  733                 error = EMSGSIZE;
  734                 goto bad;
  735         }
  736 
  737         /* Update the counters. */
  738         ESPSTAT_ADD(esps_obytes, m->m_pkthdr.len - skip);
  739 
  740         m = m_unshare(m, M_NOWAIT);
  741         if (m == NULL) {
  742                 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
  743                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  744                     (u_long) ntohl(sav->spi)));
  745                 ESPSTAT_INC(esps_hdrops);
  746                 error = ENOBUFS;
  747                 goto bad;
  748         }
  749 
  750         /* Inject ESP header. */
  751         mo = m_makespace(m, skip, hlen, &roff);
  752         if (mo == NULL) {
  753                 DPRINTF(("%s: %u byte ESP hdr inject failed for SA %s/%08lx\n",
  754                     __func__, hlen, ipsec_address(&saidx->dst, buf,
  755                     sizeof(buf)), (u_long) ntohl(sav->spi)));
  756                 ESPSTAT_INC(esps_hdrops);       /* XXX diffs from openbsd */
  757                 error = ENOBUFS;
  758                 goto bad;
  759         }
  760 
  761         /* Initialize ESP header. */
  762         bcopy((caddr_t) &sav->spi, mtod(mo, caddr_t) + roff,
  763             sizeof(uint32_t));
  764         SECASVAR_LOCK(sav);
  765         if (sav->replay) {
  766                 uint32_t replay;
  767 
  768 #ifdef REGRESSION
  769                 /* Emulate replay attack when ipsec_replay is TRUE. */
  770                 if (!V_ipsec_replay)
  771 #endif
  772                         sav->replay->count++;
  773                 replay = htonl(sav->replay->count);
  774 
  775                 bcopy((caddr_t) &replay, mtod(mo, caddr_t) + roff +
  776                     sizeof(uint32_t), sizeof(uint32_t));
  777         }
  778         cryptoid = sav->tdb_cryptoid;
  779         if (SAV_ISCTRORGCM(sav))
  780                 cntr = sav->cntr++;
  781         SECASVAR_UNLOCK(sav);
  782 
  783         /*
  784          * Add padding -- better to do it ourselves than use the crypto engine,
  785          * although if/when we support compression, we'd have to do that.
  786          */
  787         pad = (u_char *) m_pad(m, padding + alen);
  788         if (pad == NULL) {
  789                 DPRINTF(("%s: m_pad failed for SA %s/%08lx\n", __func__,
  790                     ipsec_address(&saidx->dst, buf, sizeof(buf)),
  791                     (u_long) ntohl(sav->spi)));
  792                 m = NULL;               /* NB: free'd by m_pad */
  793                 error = ENOBUFS;
  794                 goto bad;
  795         }
  796 
  797         /*
  798          * Add padding: random, zero, or self-describing.
  799          * XXX catch unexpected setting
  800          */
  801         switch (sav->flags & SADB_X_EXT_PMASK) {
  802         case SADB_X_EXT_PRAND:
  803                 (void) read_random(pad, padding - 2);
  804                 break;
  805         case SADB_X_EXT_PZERO:
  806                 bzero(pad, padding - 2);
  807                 break;
  808         case SADB_X_EXT_PSEQ:
  809                 for (i = 0; i < padding - 2; i++)
  810                         pad[i] = i+1;
  811                 break;
  812         }
  813 
  814         /* Fix padding length and Next Protocol in padding itself. */
  815         pad[padding - 2] = padding - 2;
  816         m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
  817 
  818         /* Fix Next Protocol in IPv4/IPv6 header. */
  819         prot = IPPROTO_ESP;
  820         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
  821 
  822         /* Get crypto descriptors. */
  823         crp = crypto_getreq(esph != NULL ? 2 : 1);
  824         if (crp == NULL) {
  825                 DPRINTF(("%s: failed to acquire crypto descriptors\n",
  826                         __func__));
  827                 ESPSTAT_INC(esps_crypto);
  828                 error = ENOBUFS;
  829                 goto bad;
  830         }
  831 
  832         /* IPsec-specific opaque crypto info. */
  833         xd =  malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
  834         if (xd == NULL) {
  835                 crypto_freereq(crp);
  836                 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
  837                 ESPSTAT_INC(esps_crypto);
  838                 error = ENOBUFS;
  839                 goto bad;
  840         }
  841 
  842         crde = crp->crp_desc;
  843         crda = crde->crd_next;
  844 
  845         /* Encryption descriptor. */
  846         crde->crd_skip = skip + hlen;
  847         crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  848         crde->crd_flags = CRD_F_ENCRYPT;
  849         crde->crd_inject = skip + hlen - sav->ivlen;
  850 
  851         /* Encryption operation. */
  852         crde->crd_alg = espx->type;
  853         if (SAV_ISCTRORGCM(sav)) {
  854                 ivp = &crde->crd_iv[0];
  855 
  856                 /* GCM IV Format: RFC4106 4 */
  857                 /* CTR IV Format: RFC3686 4 */
  858                 /* Salt is last four bytes of key, RFC4106 8.1 */
  859                 /* Nonce is last four bytes of key, RFC3686 5.1 */
  860                 memcpy(ivp, sav->key_enc->key_data +
  861                     _KEYLEN(sav->key_enc) - 4, 4);
  862                 be64enc(&ivp[4], cntr);
  863                 if (SAV_ISCTR(sav)) {
  864                         /* Initial block counter is 1, RFC3686 4 */
  865                         /* XXXAE: should we use this only for first packet? */
  866                         be32enc(&ivp[sav->ivlen + 4], 1);
  867                 }
  868 
  869                 m_copyback(m, skip + hlen - sav->ivlen, sav->ivlen, &ivp[4]);
  870                 crde->crd_flags |= CRD_F_IV_EXPLICIT|CRD_F_IV_PRESENT;
  871         }
  872 
  873         /* Callback parameters */
  874         xd->sp = sp;
  875         xd->sav = sav;
  876         xd->idx = idx;
  877         xd->cryptoid = cryptoid;
  878         xd->vnet = curvnet;
  879 
  880         /* Crypto operation descriptor. */
  881         crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
  882         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  883         crp->crp_buf = (caddr_t) m;
  884         crp->crp_callback = esp_output_cb;
  885         crp->crp_opaque = (caddr_t) xd;
  886         crp->crp_sid = cryptoid;
  887 
  888         if (esph) {
  889                 /* Authentication descriptor. */
  890                 crda->crd_alg = esph->type;
  891                 crda->crd_skip = skip;
  892                 if (SAV_ISGCM(sav))
  893                         crda->crd_len = 8;      /* RFC4106 5, SPI + SN */
  894                 else
  895                         crda->crd_len = m->m_pkthdr.len - (skip + alen);
  896                 crda->crd_inject = m->m_pkthdr.len - alen;
  897         }
  898 
  899         return crypto_dispatch(crp);
  900 bad:
  901         if (m)
  902                 m_freem(m);
  903         key_freesav(&sav);
  904         key_freesp(&sp);
  905         return (error);
  906 }
  907 /*
  908  * ESP output callback from the crypto driver.
  909  */
  910 static int
  911 esp_output_cb(struct cryptop *crp)
  912 {
  913         struct xform_data *xd;
  914         struct secpolicy *sp;
  915         struct secasvar *sav;
  916         struct mbuf *m;
  917         uint64_t cryptoid;
  918         u_int idx;
  919         int error;
  920 
  921         xd = (struct xform_data *) crp->crp_opaque;
  922         CURVNET_SET(xd->vnet);
  923         m = (struct mbuf *) crp->crp_buf;
  924         sp = xd->sp;
  925         sav = xd->sav;
  926         idx = xd->idx;
  927         cryptoid = xd->cryptoid;
  928 
  929         /* Check for crypto errors. */
  930         if (crp->crp_etype) {
  931                 if (crp->crp_etype == EAGAIN) {
  932                         /* Reset the session ID */
  933                         if (ipsec_updateid(sav, &crp->crp_sid, &cryptoid) != 0)
  934                                 crypto_freesession(cryptoid);
  935                         xd->cryptoid = crp->crp_sid;
  936                         CURVNET_RESTORE();
  937                         return (crypto_dispatch(crp));
  938                 }
  939                 ESPSTAT_INC(esps_noxform);
  940                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
  941                 error = crp->crp_etype;
  942                 m_freem(m);
  943                 goto bad;
  944         }
  945 
  946         /* Shouldn't happen... */
  947         if (m == NULL) {
  948                 ESPSTAT_INC(esps_crypto);
  949                 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
  950                 error = EINVAL;
  951                 goto bad;
  952         }
  953         free(xd, M_XDATA);
  954         crypto_freereq(crp);
  955         ESPSTAT_INC(esps_hist[sav->alg_enc]);
  956         if (sav->tdb_authalgxform != NULL)
  957                 AHSTAT_INC(ahs_hist[sav->alg_auth]);
  958 
  959 #ifdef REGRESSION
  960         /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
  961         if (V_ipsec_integrity) {
  962                 static unsigned char ipseczeroes[AH_HMAC_MAXHASHLEN];
  963                 const struct auth_hash *esph;
  964 
  965                 /*
  966                  * Corrupt HMAC if we want to test integrity verification of
  967                  * the other side.
  968                  */
  969                 esph = sav->tdb_authalgxform;
  970                 if (esph !=  NULL) {
  971                         int alen;
  972 
  973                         alen = xform_ah_authsize(esph);
  974                         m_copyback(m, m->m_pkthdr.len - alen,
  975                             alen, ipseczeroes);
  976                 }
  977         }
  978 #endif
  979 
  980         /* NB: m is reclaimed by ipsec_process_done. */
  981         error = ipsec_process_done(m, sp, sav, idx);
  982         CURVNET_RESTORE();
  983         return (error);
  984 bad:
  985         CURVNET_RESTORE();
  986         free(xd, M_XDATA);
  987         crypto_freereq(crp);
  988         key_freesav(&sav);
  989         key_freesp(&sp);
  990         return (error);
  991 }
  992 
  993 static struct xformsw esp_xformsw = {
  994         .xf_type =      XF_ESP,
  995         .xf_name =      "IPsec ESP",
  996         .xf_init =      esp_init,
  997         .xf_zeroize =   esp_zeroize,
  998         .xf_input =     esp_input,
  999         .xf_output =    esp_output,
 1000 };
 1001 
 1002 SYSINIT(esp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
 1003     xform_attach, &esp_xformsw);
 1004 SYSUNINIT(esp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
 1005     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.