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

Cache object: 397e5550a2c6eb24ef2dac194c3047fb


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