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/4/sys/netipsec/xform_esp.c 156999 2006-03-22 16:02:49Z cperciva $      */
    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/random.h>
   48 #include <sys/sysctl.h>
   49 
   50 #include <net/if.h>
   51 
   52 #include <netinet/in.h>
   53 #include <netinet/in_systm.h>
   54 #include <netinet/ip.h>
   55 #include <netinet/ip_ecn.h>
   56 #include <netinet/ip6.h>
   57 
   58 #include <net/route.h>
   59 #include <netipsec/ipsec.h>
   60 #include <netipsec/ah.h>
   61 #include <netipsec/ah_var.h>
   62 #include <netipsec/esp.h>
   63 #include <netipsec/esp_var.h>
   64 #include <netipsec/xform.h>
   65 
   66 #ifdef INET6
   67 #include <netinet6/ip6_var.h>
   68 #include <netipsec/ipsec6.h>
   69 #include <netinet6/ip6_ecn.h>
   70 #endif
   71 
   72 #include <netipsec/key.h>
   73 #include <netipsec/key_debug.h>
   74 
   75 #include <opencrypto/cryptodev.h>
   76 #include <opencrypto/xform.h>
   77 
   78 int     esp_enable = 1;
   79 struct  espstat espstat;
   80 
   81 SYSCTL_DECL(_net_inet_esp);
   82 SYSCTL_INT(_net_inet_esp, OID_AUTO,
   83         esp_enable,     CTLFLAG_RW,     &esp_enable,    0, "");
   84 SYSCTL_STRUCT(_net_inet_esp, IPSECCTL_STATS,
   85         stats,          CTLFLAG_RD,     &espstat,       espstat, "");
   86 
   87 static  int esp_max_ivlen;              /* max iv length over all algorithms */
   88 
   89 static int esp_input_cb(struct cryptop *op);
   90 static int esp_output_cb(struct cryptop *crp);
   91 
   92 /*
   93  * NB: this is public for use by the PF_KEY support.
   94  * NB: if you add support here; be sure to add code to esp_attach below!
   95  */
   96 struct enc_xform *
   97 esp_algorithm_lookup(int alg)
   98 {
   99         if (alg >= ESP_ALG_MAX)
  100                 return NULL;
  101         switch (alg) {
  102         case SADB_EALG_DESCBC:
  103                 return &enc_xform_des;
  104         case SADB_EALG_3DESCBC:
  105                 return &enc_xform_3des;
  106         case SADB_X_EALG_AES:
  107                 return &enc_xform_rijndael128;
  108         case SADB_X_EALG_BLOWFISHCBC:
  109                 return &enc_xform_blf;
  110         case SADB_X_EALG_CAST128CBC:
  111                 return &enc_xform_cast5;
  112         case SADB_X_EALG_SKIPJACK:
  113                 return &enc_xform_skipjack;
  114         case SADB_EALG_NULL:
  115                 return &enc_xform_null;
  116         }
  117         return NULL;
  118 }
  119 
  120 size_t
  121 esp_hdrsiz(struct secasvar *sav)
  122 {
  123         size_t size;
  124 
  125         if (sav != NULL) {
  126                 /*XXX not right for null algorithm--does it matter??*/
  127                 KASSERT(sav->tdb_encalgxform != NULL,
  128                         ("esp_hdrsiz: SA with null xform"));
  129                 if (sav->flags & SADB_X_EXT_OLD)
  130                         size = sizeof (struct esp);
  131                 else
  132                         size = sizeof (struct newesp);
  133                 size += sav->tdb_encalgxform->blocksize + 9;
  134                 /*XXX need alg check???*/
  135                 if (sav->tdb_authalgxform != NULL && sav->replay)
  136                         size += ah_hdrsiz(sav);
  137         } else {
  138                 /*
  139                  *   base header size
  140                  * + max iv length for CBC mode
  141                  * + max pad length
  142                  * + sizeof (pad length field)
  143                  * + sizeof (next header field)
  144                  * + max icv supported.
  145                  */
  146                 size = sizeof (struct newesp) + esp_max_ivlen + 9 + 16;
  147         }
  148         return size;
  149 }
  150 
  151 /*
  152  * esp_init() is called when an SPI is being set up.
  153  */
  154 static int
  155 esp_init(struct secasvar *sav, struct xformsw *xsp)
  156 {
  157         struct enc_xform *txform;
  158         struct cryptoini cria, crie;
  159         int keylen;
  160         int error;
  161 
  162         txform = esp_algorithm_lookup(sav->alg_enc);
  163         if (txform == NULL) {
  164                 DPRINTF(("esp_init: unsupported encryption algorithm %d\n",
  165                         sav->alg_enc));
  166                 return EINVAL;
  167         }
  168         if (sav->key_enc == NULL) {
  169                 DPRINTF(("esp_init: no encoding key for %s algorithm\n",
  170                          txform->name));
  171                 return EINVAL;
  172         }
  173         if ((sav->flags&(SADB_X_EXT_OLD|SADB_X_EXT_IV4B)) == SADB_X_EXT_IV4B) {
  174                 DPRINTF(("esp_init: 4-byte IV not supported with protocol\n"));
  175                 return EINVAL;
  176         }
  177         keylen = _KEYLEN(sav->key_enc);
  178         if (txform->minkey > keylen || keylen > txform->maxkey) {
  179                 DPRINTF(("esp_init: invalid key length %u, must be in "
  180                         "the range [%u..%u] for algorithm %s\n",
  181                         keylen, txform->minkey, txform->maxkey,
  182                         txform->name));
  183                 return EINVAL;
  184         }
  185 
  186         /*
  187          * NB: The null xform needs a non-zero blocksize to keep the
  188          *      crypto code happy but if we use it to set ivlen then
  189          *      the ESP header will be processed incorrectly.  The
  190          *      compromise is to force it to zero here.
  191          */
  192         sav->ivlen = (txform == &enc_xform_null ? 0 : txform->blocksize);
  193         sav->iv = (caddr_t) malloc(sav->ivlen, M_XDATA, M_WAITOK);
  194         if (sav->iv == NULL) {
  195                 DPRINTF(("esp_init: no memory for IV\n"));
  196                 return EINVAL;
  197         }
  198         key_randomfill(sav->iv, sav->ivlen);    /*XXX*/
  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         /* Initialize crypto session. */
  214         bzero(&crie, sizeof (crie));
  215         crie.cri_alg = sav->tdb_encalgxform->type;
  216         crie.cri_klen = _KEYBITS(sav->key_enc);
  217         crie.cri_key = _KEYBUF(sav->key_enc);
  218         /* XXX Rounds ? */
  219 
  220         if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
  221                 /* init both auth & enc */
  222                 crie.cri_next = &cria;
  223                 error = crypto_newsession(&sav->tdb_cryptoid,
  224                                           &crie, crypto_support);
  225         } else if (sav->tdb_encalgxform) {
  226                 error = crypto_newsession(&sav->tdb_cryptoid,
  227                                           &crie, crypto_support);
  228         } else if (sav->tdb_authalgxform) {
  229                 error = crypto_newsession(&sav->tdb_cryptoid,
  230                                           &cria, crypto_support);
  231         } else {
  232                 /* XXX cannot happen? */
  233                 DPRINTF(("esp_init: no encoding OR authentication xform!\n"));
  234                 error = EINVAL;
  235         }
  236         return error;
  237 }
  238 
  239 /*
  240  * Paranoia.
  241  */
  242 static int
  243 esp_zeroize(struct secasvar *sav)
  244 {
  245         /* NB: ah_zerorize free's the crypto session state */
  246         int error = ah_zeroize(sav);
  247 
  248         if (sav->key_enc)
  249                 bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
  250         /* NB: sav->iv is freed elsewhere, even though we malloc it! */
  251         sav->tdb_encalgxform = NULL;
  252         sav->tdb_xform = NULL;
  253         return error;
  254 }
  255 
  256 /*
  257  * ESP input processing, called (eventually) through the protocol switch.
  258  */
  259 static int
  260 esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
  261 {
  262         struct auth_hash *esph;
  263         struct enc_xform *espx;
  264         struct tdb_ident *tdbi;
  265         struct tdb_crypto *tc;
  266         int plen, alen, hlen;
  267         struct m_tag *mtag;
  268         struct newesp *esp;
  269 
  270         struct cryptodesc *crde;
  271         struct cryptop *crp;
  272 
  273         SPLASSERT(net, "esp_input");
  274 
  275         KASSERT(sav != NULL, ("esp_input: null SA"));
  276         KASSERT(sav->tdb_encalgxform != NULL,
  277                 ("esp_input: null encoding xform"));
  278         KASSERT((skip&3) == 0 && (m->m_pkthdr.len&3) == 0,
  279                 ("esp_input: misaligned packet, skip %u pkt len %u",
  280                         skip, m->m_pkthdr.len));
  281 
  282         /* XXX don't pullup, just copy header */
  283         IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
  284 
  285         esph = sav->tdb_authalgxform;
  286         espx = sav->tdb_encalgxform;
  287 
  288         /* Determine the ESP header length */
  289         if (sav->flags & SADB_X_EXT_OLD)
  290                 hlen = sizeof (struct esp) + sav->ivlen;
  291         else
  292                 hlen = sizeof (struct newesp) + sav->ivlen;
  293         /* Authenticator hash size */
  294         alen = esph ? AH_HMAC_HASHLEN : 0;
  295 
  296         /*
  297          * Verify payload length is multiple of encryption algorithm
  298          * block size.
  299          *
  300          * NB: This works for the null algorithm because the blocksize
  301          *     is 4 and all packets must be 4-byte aligned regardless
  302          *     of the algorithm.
  303          */
  304         plen = m->m_pkthdr.len - (skip + hlen + alen);
  305         if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
  306                 DPRINTF(("esp_input: "
  307                     "payload of %d octets not a multiple of %d octets,"
  308                     "  SA %s/%08lx\n",
  309                     plen, espx->blocksize,
  310                     ipsec_address(&sav->sah->saidx.dst),
  311                     (u_long) ntohl(sav->spi)));
  312                 espstat.esps_badilen++;
  313                 m_freem(m);
  314                 return EINVAL;
  315         }
  316 
  317         /*
  318          * Check sequence number.
  319          */
  320         if (esph && sav->replay && !ipsec_chkreplay(ntohl(esp->esp_seq), sav)) {
  321                 DPRINTF(("esp_input: packet replay check for %s\n",
  322                     ipsec_logsastr(sav)));      /*XXX*/
  323                 espstat.esps_replay++;
  324                 m_freem(m);
  325                 return ENOBUFS;         /*XXX*/
  326         }
  327 
  328         /* Update the counters */
  329         espstat.esps_ibytes += m->m_pkthdr.len - skip - hlen - alen;
  330 
  331         /* Find out if we've already done crypto */
  332         for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
  333              mtag != NULL;
  334              mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
  335                 tdbi = (struct tdb_ident *) (mtag + 1);
  336                 if (tdbi->proto == sav->sah->saidx.proto &&
  337                     tdbi->spi == sav->spi &&
  338                     !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
  339                           sizeof(union sockaddr_union)))
  340                         break;
  341         }
  342 
  343         /* Get crypto descriptors */
  344         crp = crypto_getreq(esph && espx ? 2 : 1);
  345         if (crp == NULL) {
  346                 DPRINTF(("esp_input: failed to acquire crypto descriptors\n"));
  347                 espstat.esps_crypto++;
  348                 m_freem(m);
  349                 return ENOBUFS;
  350         }
  351 
  352         /* Get IPsec-specific opaque pointer */
  353         if (esph == NULL || mtag != NULL)
  354                 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
  355                     M_XDATA, M_NOWAIT|M_ZERO);
  356         else
  357                 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto) + alen,
  358                     M_XDATA, M_NOWAIT|M_ZERO);
  359         if (tc == NULL) {
  360                 crypto_freereq(crp);
  361                 DPRINTF(("esp_input: failed to allocate tdb_crypto\n"));
  362                 espstat.esps_crypto++;
  363                 m_freem(m);
  364                 return ENOBUFS;
  365         }
  366 
  367         tc->tc_ptr = (caddr_t) mtag;
  368 
  369         if (esph) {
  370                 struct cryptodesc *crda = crp->crp_desc;
  371 
  372                 KASSERT(crda != NULL, ("esp_input: null ah crypto descriptor"));
  373 
  374                 /* Authentication descriptor */
  375                 crda->crd_skip = skip;
  376                 crda->crd_len = m->m_pkthdr.len - (skip + alen);
  377                 crda->crd_inject = m->m_pkthdr.len - alen;
  378 
  379                 crda->crd_alg = esph->type;
  380                 crda->crd_key = _KEYBUF(sav->key_auth);
  381                 crda->crd_klen = _KEYBITS(sav->key_auth);
  382 
  383                 /* Copy the authenticator */
  384                 if (mtag == NULL)
  385                         m_copydata(m, m->m_pkthdr.len - alen, alen,
  386                                    (caddr_t) (tc + 1));
  387 
  388                 /* Chain authentication request */
  389                 crde = crda->crd_next;
  390         } else {
  391                 crde = crp->crp_desc;
  392         }
  393 
  394         /* Crypto operation descriptor */
  395         crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
  396         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  397         crp->crp_buf = (caddr_t) m;
  398         crp->crp_callback = esp_input_cb;
  399         crp->crp_sid = sav->tdb_cryptoid;
  400         crp->crp_opaque = (caddr_t) tc;
  401 
  402         /* These are passed as-is to the callback */
  403         tc->tc_spi = sav->spi;
  404         tc->tc_dst = sav->sah->saidx.dst;
  405         tc->tc_proto = sav->sah->saidx.proto;
  406         tc->tc_protoff = protoff;
  407         tc->tc_skip = skip;
  408 
  409         /* Decryption descriptor */
  410         if (espx) {
  411                 KASSERT(crde != NULL, ("esp_input: null esp crypto descriptor"));
  412                 crde->crd_skip = skip + hlen;
  413                 crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  414                 crde->crd_inject = skip + hlen - sav->ivlen;
  415 
  416                 crde->crd_alg = espx->type;
  417                 crde->crd_key = _KEYBUF(sav->key_enc);
  418                 crde->crd_klen = _KEYBITS(sav->key_enc);
  419                 /* XXX Rounds ? */
  420         }
  421 
  422         if (mtag == NULL)
  423                 return crypto_dispatch(crp);
  424         else
  425                 return esp_input_cb(crp);
  426 }
  427 
  428 #ifdef INET6
  429 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {              \
  430         if (saidx->dst.sa.sa_family == AF_INET6) {                           \
  431                 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
  432         } else {                                                             \
  433                 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
  434         }                                                                    \
  435 } while (0)
  436 #else
  437 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)                   \
  438         (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
  439 #endif
  440 
  441 /*
  442  * ESP input callback from the crypto driver.
  443  */
  444 static int
  445 esp_input_cb(struct cryptop *crp)
  446 {
  447         u_int8_t lastthree[3], aalg[AH_HMAC_HASHLEN];
  448         int s, hlen, skip, protoff, error;
  449         struct mbuf *m;
  450         struct cryptodesc *crd;
  451         struct auth_hash *esph;
  452         struct enc_xform *espx;
  453         struct tdb_crypto *tc;
  454         struct m_tag *mtag;
  455         struct secasvar *sav;
  456         struct secasindex *saidx;
  457         caddr_t ptr;
  458 
  459         crd = crp->crp_desc;
  460         KASSERT(crd != NULL, ("esp_input_cb: null crypto descriptor!"));
  461 
  462         tc = (struct tdb_crypto *) crp->crp_opaque;
  463         KASSERT(tc != NULL, ("esp_input_cb: null opaque crypto data area!"));
  464         skip = tc->tc_skip;
  465         protoff = tc->tc_protoff;
  466         mtag = (struct m_tag *) tc->tc_ptr;
  467         m = (struct mbuf *) crp->crp_buf;
  468 
  469         s = splnet();
  470 
  471         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
  472         if (sav == NULL) {
  473                 espstat.esps_notdb++;
  474                 DPRINTF(("esp_input_cb: SA expired while in crypto "
  475                     "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
  476                     (u_long) ntohl(tc->tc_spi), tc->tc_proto));
  477                 error = ENOBUFS;                /*XXX*/
  478                 goto bad;
  479         }
  480 
  481         saidx = &sav->sah->saidx;
  482         KASSERT(saidx->dst.sa.sa_family == AF_INET ||
  483                 saidx->dst.sa.sa_family == AF_INET6,
  484                 ("ah_input_cb: unexpected protocol family %u",
  485                  saidx->dst.sa.sa_family));
  486 
  487         esph = sav->tdb_authalgxform;
  488         espx = sav->tdb_encalgxform;
  489 
  490         /* Check for crypto errors */
  491         if (crp->crp_etype) {
  492                 /* Reset the session ID */
  493                 if (sav->tdb_cryptoid != 0)
  494                         sav->tdb_cryptoid = crp->crp_sid;
  495 
  496                 if (crp->crp_etype == EAGAIN) {
  497                         KEY_FREESAV(&sav);
  498                         splx(s);
  499                         return crypto_dispatch(crp);
  500                 }
  501 
  502                 espstat.esps_noxform++;
  503                 DPRINTF(("esp_input_cb: crypto error %d\n", crp->crp_etype));
  504                 error = crp->crp_etype;
  505                 goto bad;
  506         }
  507 
  508         /* Shouldn't happen... */
  509         if (m == NULL) {
  510                 espstat.esps_crypto++;
  511                 DPRINTF(("esp_input_cb: bogus returned buffer from crypto\n"));
  512                 error = EINVAL;
  513                 goto bad;
  514         }
  515         espstat.esps_hist[sav->alg_enc]++;
  516 
  517         /* If authentication was performed, check now. */
  518         if (esph != NULL) {
  519                 /*
  520                  * If we have a tag, it means an IPsec-aware NIC did
  521                  * the verification for us.  Otherwise we need to
  522                  * check the authentication calculation.
  523                  */
  524                 ahstat.ahs_hist[sav->alg_auth]++;
  525                 if (mtag == NULL) {
  526                         /* Copy the authenticator from the packet */
  527                         m_copydata(m, m->m_pkthdr.len - esph->authsize,
  528                                 esph->authsize, aalg);
  529 
  530                         ptr = (caddr_t) (tc + 1);
  531 
  532                         /* Verify authenticator */
  533                         if (bcmp(ptr, aalg, esph->authsize) != 0) {
  534                                 DPRINTF(("esp_input_cb: "
  535                     "authentication hash mismatch for packet in SA %s/%08lx\n",
  536                                     ipsec_address(&saidx->dst),
  537                                     (u_long) ntohl(sav->spi)));
  538                                 espstat.esps_badauth++;
  539                                 error = EACCES;
  540                                 goto bad;
  541                         }
  542                 }
  543 
  544                 /* Remove trailing authenticator */
  545                 m_adj(m, -(esph->authsize));
  546         }
  547 
  548         /* Release the crypto descriptors */
  549         free(tc, M_XDATA), tc = NULL;
  550         crypto_freereq(crp), crp = NULL;
  551 
  552         /*
  553          * Packet is now decrypted.
  554          */
  555         m->m_flags |= M_DECRYPTED;
  556 
  557         /*
  558          * Update replay sequence number, if appropriate.
  559          */
  560         if (sav->replay) {
  561                 u_int32_t seq;
  562 
  563                 m_copydata(m, skip + offsetof(struct newesp, esp_seq),
  564                            sizeof (seq), (caddr_t) &seq);
  565                 if (ipsec_updatereplay(ntohl(seq), sav)) {
  566                         DPRINTF(("%s: packet replay check for %s\n", __func__,
  567                             ipsec_logsastr(sav)));
  568                         espstat.esps_replay++;
  569                         error = ENOBUFS;
  570                         goto bad;
  571                 }
  572         }
  573 
  574         /* Determine the ESP header length */
  575         if (sav->flags & SADB_X_EXT_OLD)
  576                 hlen = sizeof (struct esp) + sav->ivlen;
  577         else
  578                 hlen = sizeof (struct newesp) + sav->ivlen;
  579 
  580         /* Remove the ESP header and IV from the mbuf. */
  581         error = m_striphdr(m, skip, hlen);
  582         if (error) {
  583                 espstat.esps_hdrops++;
  584                 DPRINTF(("esp_input_cb: bad mbuf chain, SA %s/%08lx\n",
  585                     ipsec_address(&sav->sah->saidx.dst),
  586                     (u_long) ntohl(sav->spi)));
  587                 goto bad;
  588         }
  589 
  590         /* Save the last three bytes of decrypted data */
  591         m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
  592 
  593         /* Verify pad length */
  594         if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
  595                 espstat.esps_badilen++;
  596                 DPRINTF(("esp_input_cb: invalid padding length %d "
  597                          "for %u byte packet in SA %s/%08lx\n",
  598                          lastthree[1], m->m_pkthdr.len - skip,
  599                          ipsec_address(&sav->sah->saidx.dst),
  600                          (u_long) ntohl(sav->spi)));
  601                 error = EINVAL;
  602                 goto bad;
  603         }
  604 
  605         /* Verify correct decryption by checking the last padding bytes */
  606         if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
  607                 if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
  608                         espstat.esps_badenc++;
  609                         DPRINTF(("esp_input_cb: decryption failed "
  610                                 "for packet in SA %s/%08lx\n",
  611                                 ipsec_address(&sav->sah->saidx.dst),
  612                                 (u_long) ntohl(sav->spi)));
  613 DPRINTF(("esp_input_cb: %x %x\n", lastthree[0], lastthree[1]));
  614                         error = EINVAL;
  615                         goto bad;
  616                 }
  617         }
  618 
  619         /* Trim the mbuf chain to remove trailing authenticator and padding */
  620         m_adj(m, -(lastthree[1] + 2));
  621 
  622         /* Restore the Next Protocol field */
  623         m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
  624 
  625         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag);
  626 
  627         KEY_FREESAV(&sav);
  628         splx(s);
  629         return error;
  630 bad:
  631         if (sav)
  632                 KEY_FREESAV(&sav);
  633         splx(s);
  634         if (m != NULL)
  635                 m_freem(m);
  636         if (tc != NULL)
  637                 free(tc, M_XDATA);
  638         if (crp != NULL)
  639                 crypto_freereq(crp);
  640         return error;
  641 }
  642 
  643 /*
  644  * ESP output routine, called by ipsec[46]_process_packet().
  645  */
  646 static int
  647 esp_output(
  648         struct mbuf *m,
  649         struct ipsecrequest *isr,
  650         struct mbuf **mp,
  651         int skip,
  652         int protoff
  653 )
  654 {
  655         struct enc_xform *espx;
  656         struct auth_hash *esph;
  657         int hlen, rlen, plen, padding, blks, alen, i, roff;
  658         struct mbuf *mo = (struct mbuf *) NULL;
  659         struct tdb_crypto *tc;
  660         struct secasvar *sav;
  661         struct secasindex *saidx;
  662         unsigned char *pad;
  663         u_int8_t prot;
  664         int error, maxpacketsize;
  665 
  666         struct cryptodesc *crde = NULL, *crda = NULL;
  667         struct cryptop *crp;
  668 
  669         SPLASSERT(net, "esp_output");
  670 
  671         sav = isr->sav;
  672         KASSERT(sav != NULL, ("esp_output: null SA"));
  673         esph = sav->tdb_authalgxform;
  674         espx = sav->tdb_encalgxform;
  675         KASSERT(espx != NULL, ("esp_output: null encoding xform"));
  676 
  677         if (sav->flags & SADB_X_EXT_OLD)
  678                 hlen = sizeof (struct esp) + sav->ivlen;
  679         else
  680                 hlen = sizeof (struct newesp) + sav->ivlen;
  681 
  682         rlen = m->m_pkthdr.len - skip;  /* Raw payload length. */
  683         /*
  684          * NB: The null encoding transform has a blocksize of 4
  685          *     so that headers are properly aligned.
  686          */
  687         blks = espx->blocksize;         /* IV blocksize */
  688 
  689         /* XXX clamp padding length a la KAME??? */
  690         padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
  691         plen = rlen + padding;          /* Padded payload length. */
  692 
  693         if (esph)
  694                 alen = AH_HMAC_HASHLEN;
  695         else
  696                 alen = 0;
  697 
  698         espstat.esps_output++;
  699 
  700         saidx = &sav->sah->saidx;
  701         /* Check for maximum packet size violations. */
  702         switch (saidx->dst.sa.sa_family) {
  703 #ifdef INET
  704         case AF_INET:
  705                 maxpacketsize = IP_MAXPACKET;
  706                 break;
  707 #endif /* INET */
  708 #ifdef INET6
  709         case AF_INET6:
  710                 maxpacketsize = IPV6_MAXPACKET;
  711                 break;
  712 #endif /* INET6 */
  713         default:
  714                 DPRINTF(("esp_output: unknown/unsupported protocol "
  715                     "family %d, SA %s/%08lx\n",
  716                     saidx->dst.sa.sa_family, ipsec_address(&saidx->dst),
  717                     (u_long) ntohl(sav->spi)));
  718                 espstat.esps_nopf++;
  719                 error = EPFNOSUPPORT;
  720                 goto bad;
  721         }
  722         if (skip + hlen + rlen + padding + alen > maxpacketsize) {
  723                 DPRINTF(("esp_output: packet in SA %s/%08lx got too big "
  724                     "(len %u, max len %u)\n",
  725                     ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi),
  726                     skip + hlen + rlen + padding + alen, maxpacketsize));
  727                 espstat.esps_toobig++;
  728                 error = EMSGSIZE;
  729                 goto bad;
  730         }
  731 
  732         /* Update the counters. */
  733         espstat.esps_obytes += m->m_pkthdr.len - skip;
  734 
  735         m = m_clone(m);
  736         if (m == NULL) {
  737                 DPRINTF(("esp_output: cannot clone mbuf chain, SA %s/%08lx\n",
  738                     ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
  739                 espstat.esps_hdrops++;
  740                 error = ENOBUFS;
  741                 goto bad;
  742         }
  743 
  744         /* Inject ESP header. */
  745         mo = m_makespace(m, skip, hlen, &roff);
  746         if (mo == NULL) {
  747                 DPRINTF(("esp_output: failed to inject %u byte ESP hdr for SA "
  748                     "%s/%08lx\n",
  749                     hlen, ipsec_address(&saidx->dst),
  750                     (u_long) ntohl(sav->spi)));
  751                 espstat.esps_hdrops++;          /* XXX diffs from openbsd */
  752                 error = ENOBUFS;
  753                 goto bad;
  754         }
  755 
  756         /* Initialize ESP header. */
  757         bcopy((caddr_t) &sav->spi, mtod(mo, caddr_t) + roff, sizeof(u_int32_t));
  758         if (sav->replay) {
  759                 u_int32_t replay = htonl(++(sav->replay->count));
  760                 bcopy((caddr_t) &replay,
  761                     mtod(mo, caddr_t) + roff + sizeof(u_int32_t),
  762                     sizeof(u_int32_t));
  763         }
  764 
  765         /*
  766          * Add padding -- better to do it ourselves than use the crypto engine,
  767          * although if/when we support compression, we'd have to do that.
  768          */
  769         pad = (u_char *) m_pad(m, padding + alen);
  770         if (pad == NULL) {
  771                 DPRINTF(("esp_output: m_pad failed for SA %s/%08lx\n",
  772                     ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
  773                 m = NULL;               /* NB: free'd by m_pad */
  774                 error = ENOBUFS;
  775                 goto bad;
  776         }
  777 
  778         /*
  779          * Add padding: random, zero, or self-describing.
  780          * XXX catch unexpected setting
  781          */
  782         switch (sav->flags & SADB_X_EXT_PMASK) {
  783         case SADB_X_EXT_PRAND:
  784                 (void) read_random(pad, padding - 2);
  785                 break;
  786         case SADB_X_EXT_PZERO:
  787                 bzero(pad, padding - 2);
  788                 break;
  789         case SADB_X_EXT_PSEQ:
  790                 for (i = 0; i < padding - 2; i++)
  791                         pad[i] = i+1;
  792                 break;
  793         }
  794 
  795         /* Fix padding length and Next Protocol in padding itself. */
  796         pad[padding - 2] = padding - 2;
  797         m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
  798 
  799         /* Fix Next Protocol in IPv4/IPv6 header. */
  800         prot = IPPROTO_ESP;
  801         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
  802 
  803         /* Get crypto descriptors. */
  804         crp = crypto_getreq(esph && espx ? 2 : 1);
  805         if (crp == NULL) {
  806                 DPRINTF(("esp_output: failed to acquire crypto descriptors\n"));
  807                 espstat.esps_crypto++;
  808                 error = ENOBUFS;
  809                 goto bad;
  810         }
  811 
  812         if (espx) {
  813                 crde = crp->crp_desc;
  814                 crda = crde->crd_next;
  815 
  816                 /* Encryption descriptor. */
  817                 crde->crd_skip = skip + hlen;
  818                 crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
  819                 crde->crd_flags = CRD_F_ENCRYPT;
  820                 crde->crd_inject = skip + hlen - sav->ivlen;
  821 
  822                 /* Encryption operation. */
  823                 crde->crd_alg = espx->type;
  824                 crde->crd_key = _KEYBUF(sav->key_enc);
  825                 crde->crd_klen = _KEYBITS(sav->key_enc);
  826                 /* XXX Rounds ? */
  827         } else
  828                 crda = crp->crp_desc;
  829 
  830         /* IPsec-specific opaque crypto info. */
  831         tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
  832                 M_XDATA, M_NOWAIT|M_ZERO);
  833         if (tc == NULL) {
  834                 crypto_freereq(crp);
  835                 DPRINTF(("esp_output: failed to allocate tdb_crypto\n"));
  836                 espstat.esps_crypto++;
  837                 error = ENOBUFS;
  838                 goto bad;
  839         }
  840 
  841         /* Callback parameters */
  842         tc->tc_isr = isr;
  843         tc->tc_spi = sav->spi;
  844         tc->tc_dst = saidx->dst;
  845         tc->tc_proto = saidx->proto;
  846 
  847         /* Crypto operation descriptor. */
  848         crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
  849         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
  850         crp->crp_buf = (caddr_t) m;
  851         crp->crp_callback = esp_output_cb;
  852         crp->crp_opaque = (caddr_t) tc;
  853         crp->crp_sid = sav->tdb_cryptoid;
  854 
  855         if (esph) {
  856                 /* Authentication descriptor. */
  857                 crda->crd_skip = skip;
  858                 crda->crd_len = m->m_pkthdr.len - (skip + alen);
  859                 crda->crd_inject = m->m_pkthdr.len - alen;
  860 
  861                 /* Authentication operation. */
  862                 crda->crd_alg = esph->type;
  863                 crda->crd_key = _KEYBUF(sav->key_auth);
  864                 crda->crd_klen = _KEYBITS(sav->key_auth);
  865         }
  866 
  867         return crypto_dispatch(crp);
  868 bad:
  869         if (m)
  870                 m_freem(m);
  871         return (error);
  872 }
  873 
  874 /*
  875  * ESP output callback from the crypto driver.
  876  */
  877 static int
  878 esp_output_cb(struct cryptop *crp)
  879 {
  880         struct tdb_crypto *tc;
  881         struct ipsecrequest *isr;
  882         struct secasvar *sav;
  883         struct mbuf *m;
  884         int s, err, error;
  885 
  886         tc = (struct tdb_crypto *) crp->crp_opaque;
  887         KASSERT(tc != NULL, ("esp_output_cb: null opaque data area!"));
  888         m = (struct mbuf *) crp->crp_buf;
  889 
  890         s = splnet();
  891 
  892         isr = tc->tc_isr;
  893         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
  894         if (sav == NULL) {
  895                 espstat.esps_notdb++;
  896                 DPRINTF(("esp_output_cb: SA expired while in crypto "
  897                     "(SA %s/%08lx proto %u)\n", ipsec_address(&tc->tc_dst),
  898                     (u_long) ntohl(tc->tc_spi), tc->tc_proto));
  899                 error = ENOBUFS;                /*XXX*/
  900                 goto bad;
  901         }
  902         KASSERT(isr->sav == sav,
  903                 ("esp_output_cb: SA changed was %p now %p\n", isr->sav, sav));
  904 
  905         /* Check for crypto errors. */
  906         if (crp->crp_etype) {
  907                 /* Reset session ID. */
  908                 if (sav->tdb_cryptoid != 0)
  909                         sav->tdb_cryptoid = crp->crp_sid;
  910 
  911                 if (crp->crp_etype == EAGAIN) {
  912                         KEY_FREESAV(&sav);
  913                         splx(s);
  914                         return crypto_dispatch(crp);
  915                 }
  916 
  917                 espstat.esps_noxform++;
  918                 DPRINTF(("esp_output_cb: crypto error %d\n", crp->crp_etype));
  919                 error = crp->crp_etype;
  920                 goto bad;
  921         }
  922 
  923         /* Shouldn't happen... */
  924         if (m == NULL) {
  925                 espstat.esps_crypto++;
  926                 DPRINTF(("esp_output_cb: bogus returned buffer from crypto\n"));
  927                 error = EINVAL;
  928                 goto bad;
  929         }
  930         espstat.esps_hist[sav->alg_enc]++;
  931         if (sav->tdb_authalgxform != NULL)
  932                 ahstat.ahs_hist[sav->alg_auth]++;
  933 
  934         /* Release crypto descriptors. */
  935         free(tc, M_XDATA);
  936         crypto_freereq(crp);
  937 
  938         /* NB: m is reclaimed by ipsec_process_done. */
  939         err = ipsec_process_done(m, isr);
  940         KEY_FREESAV(&sav);
  941         splx(s);
  942         return err;
  943 bad:
  944         if (sav)
  945                 KEY_FREESAV(&sav);
  946         splx(s);
  947         if (m)
  948                 m_freem(m);
  949         free(tc, M_XDATA);
  950         crypto_freereq(crp);
  951         return error;
  952 }
  953 
  954 static struct xformsw esp_xformsw = {
  955         XF_ESP,         XFT_CONF|XFT_AUTH,      "IPsec ESP",
  956         esp_init,       esp_zeroize,            esp_input,
  957         esp_output
  958 };
  959 
  960 static void
  961 esp_attach(void)
  962 {
  963 #define MAXIV(xform)                                    \
  964         if (xform.blocksize > esp_max_ivlen)            \
  965                 esp_max_ivlen = xform.blocksize         \
  966 
  967         esp_max_ivlen = 0;
  968         MAXIV(enc_xform_des);           /* SADB_EALG_DESCBC */
  969         MAXIV(enc_xform_3des);          /* SADB_EALG_3DESCBC */
  970         MAXIV(enc_xform_rijndael128);   /* SADB_X_EALG_AES */
  971         MAXIV(enc_xform_blf);           /* SADB_X_EALG_BLOWFISHCBC */
  972         MAXIV(enc_xform_cast5);         /* SADB_X_EALG_CAST128CBC */
  973         MAXIV(enc_xform_skipjack);      /* SADB_X_EALG_SKIPJACK */
  974         MAXIV(enc_xform_null);          /* SADB_EALG_NULL */
  975 
  976         xform_register(&esp_xformsw);
  977 #undef MAXIV
  978 }
  979 SYSINIT(esp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, esp_attach, NULL)

Cache object: 43a99d7ed56eb3a62c46db60828c7ae5


[ 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.