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

Cache object: 066c8739bf6d32342ad4b442fcdd14e6


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