2 /* $OpenBSD: ip_esp.c,v 1.69 2001/06/26 06:18:59 angelos Exp $ */
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).
8 * The original version of this code was written by John Ioannidis
9 * for BSD/OS in Athens, Greece, in November 1995.
11 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
12 * by Angelos D. Keromytis.
14 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * Additional features in 1999 by Angelos D. Keromytis.
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.
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
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
39 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.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>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_ecn.h>
57 #include <netinet/ip6.h>
59 #include <net/route.h>
60 #include <netipsec/ipsec.h>
61 #include <netipsec/ah.h>
62 #include <netipsec/ah_var.h>
63 #include <netipsec/esp.h>
64 #include <netipsec/esp_var.h>
65 #include <netipsec/xform.h>
68 #include <netinet6/ip6_var.h>
69 #include <netipsec/ipsec6.h>
70 #include <netinet6/ip6_ecn.h>
73 #include <netipsec/key.h>
74 #include <netipsec/key_debug.h>
76 #include <opencrypto/cryptodev.h>
77 #include <opencrypto/xform.h>
79 VNET_DEFINE(int, esp_enable) = 1;
80 VNET_DEFINE(struct espstat, espstat);
82 SYSCTL_DECL(_net_inet_esp);
83 SYSCTL_VNET_INT(_net_inet_esp, OID_AUTO,
84 esp_enable, CTLFLAG_RW, &VNET_NAME(esp_enable), 0, "");
85 SYSCTL_VNET_STRUCT(_net_inet_esp, IPSECCTL_STATS,
86 stats, CTLFLAG_RD, &VNET_NAME(espstat), espstat, "");
88 static VNET_DEFINE(int, esp_max_ivlen); /* max iv length over all algorithms */
89 #define V_esp_max_ivlen VNET(esp_max_ivlen)
91 static int esp_input_cb(struct cryptop *op);
92 static int esp_output_cb(struct cryptop *crp);
95 * NB: this is public for use by the PF_KEY support.
96 * NB: if you add support here; be sure to add code to esp_attach below!
99 esp_algorithm_lookup(int alg)
101 if (alg >= ESP_ALG_MAX)
104 case SADB_EALG_DESCBC:
105 return &enc_xform_des;
106 case SADB_EALG_3DESCBC:
107 return &enc_xform_3des;
108 case SADB_X_EALG_AES:
109 return &enc_xform_rijndael128;
110 case SADB_X_EALG_BLOWFISHCBC:
111 return &enc_xform_blf;
112 case SADB_X_EALG_CAST128CBC:
113 return &enc_xform_cast5;
114 case SADB_X_EALG_SKIPJACK:
115 return &enc_xform_skipjack;
117 return &enc_xform_null;
118 case SADB_X_EALG_CAMELLIACBC:
119 return &enc_xform_camellia;
125 esp_hdrsiz(struct secasvar *sav)
130 /*XXX not right for null algorithm--does it matter??*/
131 IPSEC_ASSERT(sav->tdb_encalgxform != NULL,
132 ("SA with null xform"));
133 if (sav->flags & SADB_X_EXT_OLD)
134 size = sizeof (struct esp);
136 size = sizeof (struct newesp);
137 size += sav->tdb_encalgxform->blocksize + 9;
138 /*XXX need alg check???*/
139 if (sav->tdb_authalgxform != NULL && sav->replay)
140 size += ah_hdrsiz(sav);
144 * + max iv length for CBC mode
146 * + sizeof (pad length field)
147 * + sizeof (next header field)
148 * + max icv supported.
150 size = sizeof (struct newesp) + V_esp_max_ivlen + 9 + 16;
156 * esp_init() is called when an SPI is being set up.
159 esp_init(struct secasvar *sav, struct xformsw *xsp)
161 struct enc_xform *txform;
162 struct cryptoini cria, crie;
166 txform = esp_algorithm_lookup(sav->alg_enc);
167 if (txform == NULL) {
168 DPRINTF(("%s: unsupported encryption algorithm %d\n",
169 __func__, sav->alg_enc));
172 if (sav->key_enc == NULL) {
173 DPRINTF(("%s: no encoding key for %s algorithm\n",
174 __func__, txform->name));
177 if ((sav->flags&(SADB_X_EXT_OLD|SADB_X_EXT_IV4B)) == SADB_X_EXT_IV4B) {
178 DPRINTF(("%s: 4-byte IV not supported with protocol\n",
182 keylen = _KEYLEN(sav->key_enc);
183 if (txform->minkey > keylen || keylen > txform->maxkey) {
184 DPRINTF(("%s: invalid key length %u, must be in the range "
185 "[%u..%u] for algorithm %s\n", __func__,
186 keylen, txform->minkey, txform->maxkey,
192 * NB: The null xform needs a non-zero blocksize to keep the
193 * crypto code happy but if we use it to set ivlen then
194 * the ESP header will be processed incorrectly. The
195 * compromise is to force it to zero here.
197 sav->ivlen = (txform == &enc_xform_null ? 0 : txform->blocksize);
198 sav->iv = (caddr_t) malloc(sav->ivlen, M_XDATA, M_WAITOK);
199 if (sav->iv == NULL) {
200 DPRINTF(("%s: no memory for IV\n", __func__));
203 key_randomfill(sav->iv, sav->ivlen); /*XXX*/
206 * Setup AH-related state.
208 if (sav->alg_auth != 0) {
209 error = ah_init0(sav, xsp, &cria);
214 /* NB: override anything set in ah_init0 */
215 sav->tdb_xform = xsp;
216 sav->tdb_encalgxform = txform;
218 /* Initialize crypto session. */
219 bzero(&crie, sizeof (crie));
220 crie.cri_alg = sav->tdb_encalgxform->type;
221 crie.cri_klen = _KEYBITS(sav->key_enc);
222 crie.cri_key = sav->key_enc->key_data;
225 if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
226 /* init both auth & enc */
227 crie.cri_next = &cria;
228 error = crypto_newsession(&sav->tdb_cryptoid,
229 &crie, V_crypto_support);
230 } else if (sav->tdb_encalgxform) {
231 error = crypto_newsession(&sav->tdb_cryptoid,
232 &crie, V_crypto_support);
233 } else if (sav->tdb_authalgxform) {
234 error = crypto_newsession(&sav->tdb_cryptoid,
235 &cria, V_crypto_support);
237 /* XXX cannot happen? */
238 DPRINTF(("%s: no encoding OR authentication xform!\n",
249 esp_zeroize(struct secasvar *sav)
251 /* NB: ah_zerorize free's the crypto session state */
252 int error = ah_zeroize(sav);
255 bzero(sav->key_enc->key_data, _KEYLEN(sav->key_enc));
257 free(sav->iv, M_XDATA);
260 sav->tdb_encalgxform = NULL;
261 sav->tdb_xform = NULL;
266 * ESP input processing, called (eventually) through the protocol switch.
269 esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
271 struct auth_hash *esph;
272 struct enc_xform *espx;
273 struct tdb_ident *tdbi;
274 struct tdb_crypto *tc;
275 int plen, alen, hlen;
279 struct cryptodesc *crde;
282 IPSEC_ASSERT(sav != NULL, ("null SA"));
283 IPSEC_ASSERT(sav->tdb_encalgxform != NULL, ("null encoding xform"));
285 /* Valid IP Packet length ? */
286 if ( (skip&3) || (m->m_pkthdr.len&3) ){
287 DPRINTF(("%s: misaligned packet, skip %u pkt len %u",
288 __func__, skip, m->m_pkthdr.len));
289 V_espstat.esps_badilen++;
294 /* XXX don't pullup, just copy header */
295 IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof (struct newesp));
297 esph = sav->tdb_authalgxform;
298 espx = sav->tdb_encalgxform;
300 /* Determine the ESP header length */
301 if (sav->flags & SADB_X_EXT_OLD)
302 hlen = sizeof (struct esp) + sav->ivlen;
304 hlen = sizeof (struct newesp) + sav->ivlen;
305 /* Authenticator hash size */
307 switch (esph->type) {
308 case CRYPTO_SHA2_256_HMAC:
309 case CRYPTO_SHA2_384_HMAC:
310 case CRYPTO_SHA2_512_HMAC:
311 alen = esph->hashsize/2;
314 alen = AH_HMAC_HASHLEN;
321 * Verify payload length is multiple of encryption algorithm
324 * NB: This works for the null algorithm because the blocksize
325 * is 4 and all packets must be 4-byte aligned regardless
328 plen = m->m_pkthdr.len - (skip + hlen + alen);
329 if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
330 DPRINTF(("%s: payload of %d octets not a multiple of %d octets,"
331 " SA %s/%08lx\n", __func__,
332 plen, espx->blocksize,
333 ipsec_address(&sav->sah->saidx.dst),
334 (u_long) ntohl(sav->spi)));
335 V_espstat.esps_badilen++;
341 * Check sequence number.
343 if (esph && sav->replay && !ipsec_chkreplay(ntohl(esp->esp_seq), sav)) {
344 DPRINTF(("%s: packet replay check for %s\n", __func__,
345 ipsec_logsastr(sav))); /*XXX*/
346 V_espstat.esps_replay++;
348 return ENOBUFS; /*XXX*/
351 /* Update the counters */
352 V_espstat.esps_ibytes += m->m_pkthdr.len - (skip + hlen + alen);
354 /* Find out if we've already done crypto */
355 for (mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, NULL);
357 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_CRYPTO_DONE, mtag)) {
358 tdbi = (struct tdb_ident *) (mtag + 1);
359 if (tdbi->proto == sav->sah->saidx.proto &&
360 tdbi->spi == sav->spi &&
361 !bcmp(&tdbi->dst, &sav->sah->saidx.dst,
362 sizeof(union sockaddr_union)))
366 /* Get crypto descriptors */
367 crp = crypto_getreq(esph && espx ? 2 : 1);
369 DPRINTF(("%s: failed to acquire crypto descriptors\n",
371 V_espstat.esps_crypto++;
376 /* Get IPsec-specific opaque pointer */
377 if (esph == NULL || mtag != NULL)
378 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
379 M_XDATA, M_NOWAIT|M_ZERO);
381 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto) + alen,
382 M_XDATA, M_NOWAIT|M_ZERO);
385 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
386 V_espstat.esps_crypto++;
391 tc->tc_ptr = (caddr_t) mtag;
394 struct cryptodesc *crda = crp->crp_desc;
396 IPSEC_ASSERT(crda != NULL, ("null ah crypto descriptor"));
398 /* Authentication descriptor */
399 crda->crd_skip = skip;
400 crda->crd_len = m->m_pkthdr.len - (skip + alen);
401 crda->crd_inject = m->m_pkthdr.len - alen;
403 crda->crd_alg = esph->type;
404 crda->crd_key = sav->key_auth->key_data;
405 crda->crd_klen = _KEYBITS(sav->key_auth);
407 /* Copy the authenticator */
409 m_copydata(m, m->m_pkthdr.len - alen, alen,
412 /* Chain authentication request */
413 crde = crda->crd_next;
415 crde = crp->crp_desc;
418 /* Crypto operation descriptor */
419 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
420 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
421 crp->crp_buf = (caddr_t) m;
422 crp->crp_callback = esp_input_cb;
423 crp->crp_sid = sav->tdb_cryptoid;
424 crp->crp_opaque = (caddr_t) tc;
426 /* These are passed as-is to the callback */
427 tc->tc_spi = sav->spi;
428 tc->tc_dst = sav->sah->saidx.dst;
429 tc->tc_proto = sav->sah->saidx.proto;
430 tc->tc_protoff = protoff;
435 /* Decryption descriptor */
437 IPSEC_ASSERT(crde != NULL, ("null esp crypto descriptor"));
438 crde->crd_skip = skip + hlen;
439 crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
440 crde->crd_inject = skip + hlen - sav->ivlen;
442 crde->crd_alg = espx->type;
443 crde->crd_key = sav->key_enc->key_data;
444 crde->crd_klen = _KEYBITS(sav->key_enc);
449 return crypto_dispatch(crp);
451 return esp_input_cb(crp);
455 * ESP input callback from the crypto driver.
458 esp_input_cb(struct cryptop *crp)
460 u_int8_t lastthree[3], aalg[AH_HMAC_MAXHASHLEN];
461 int hlen, skip, protoff, error, alen;
463 struct cryptodesc *crd;
464 struct auth_hash *esph;
465 struct enc_xform *espx;
466 struct tdb_crypto *tc;
468 struct secasvar *sav;
469 struct secasindex *saidx;
473 IPSEC_ASSERT(crd != NULL, ("null crypto descriptor!"));
475 tc = (struct tdb_crypto *) crp->crp_opaque;
476 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
478 protoff = tc->tc_protoff;
479 mtag = (struct m_tag *) tc->tc_ptr;
480 m = (struct mbuf *) crp->crp_buf;
483 IPSEC_ASSERT(sav != NULL, ("null SA!"));
485 saidx = &sav->sah->saidx;
486 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
487 saidx->dst.sa.sa_family == AF_INET6,
488 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
490 esph = sav->tdb_authalgxform;
491 espx = sav->tdb_encalgxform;
493 /* Check for crypto errors */
494 if (crp->crp_etype) {
495 /* Reset the session ID */
496 if (sav->tdb_cryptoid != 0)
497 sav->tdb_cryptoid = crp->crp_sid;
499 if (crp->crp_etype == EAGAIN) {
500 error = crypto_dispatch(crp);
504 V_espstat.esps_noxform++;
505 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
506 error = crp->crp_etype;
510 /* Shouldn't happen... */
512 V_espstat.esps_crypto++;
513 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
517 V_espstat.esps_hist[sav->alg_enc]++;
519 /* If authentication was performed, check now. */
521 switch (esph->type) {
522 case CRYPTO_SHA2_256_HMAC:
523 case CRYPTO_SHA2_384_HMAC:
524 case CRYPTO_SHA2_512_HMAC:
525 alen = esph->hashsize/2;
528 alen = AH_HMAC_HASHLEN;
532 * If we have a tag, it means an IPsec-aware NIC did
533 * the verification for us. Otherwise we need to
534 * check the authentication calculation.
536 V_ahstat.ahs_hist[sav->alg_auth]++;
538 /* Copy the authenticator from the packet */
539 m_copydata(m, m->m_pkthdr.len - alen,
542 ptr = (caddr_t) (tc + 1);
544 /* Verify authenticator */
545 if (bcmp(ptr, aalg, alen) != 0) {
547 "authentication hash mismatch for packet in SA %s/%08lx\n",
549 ipsec_address(&saidx->dst),
550 (u_long) ntohl(sav->spi)));
551 V_espstat.esps_badauth++;
557 /* Remove trailing authenticator */
561 /* Release the crypto descriptors */
562 free(tc, M_XDATA), tc = NULL;
563 crypto_freereq(crp), crp = NULL;
566 * Packet is now decrypted.
568 m->m_flags |= M_DECRYPTED;
571 * Update replay sequence number, if appropriate.
576 m_copydata(m, skip + offsetof(struct newesp, esp_seq),
577 sizeof (seq), (caddr_t) &seq);
578 if (ipsec_updatereplay(ntohl(seq), sav)) {
579 DPRINTF(("%s: packet replay check for %s\n", __func__,
580 ipsec_logsastr(sav)));
581 V_espstat.esps_replay++;
587 /* Determine the ESP header length */
588 if (sav->flags & SADB_X_EXT_OLD)
589 hlen = sizeof (struct esp) + sav->ivlen;
591 hlen = sizeof (struct newesp) + sav->ivlen;
593 /* Remove the ESP header and IV from the mbuf. */
594 error = m_striphdr(m, skip, hlen);
596 V_espstat.esps_hdrops++;
597 DPRINTF(("%s: bad mbuf chain, SA %s/%08lx\n", __func__,
598 ipsec_address(&sav->sah->saidx.dst),
599 (u_long) ntohl(sav->spi)));
603 /* Save the last three bytes of decrypted data */
604 m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
606 /* Verify pad length */
607 if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
608 V_espstat.esps_badilen++;
609 DPRINTF(("%s: invalid padding length %d for %u byte packet "
610 "in SA %s/%08lx\n", __func__,
611 lastthree[1], m->m_pkthdr.len - skip,
612 ipsec_address(&sav->sah->saidx.dst),
613 (u_long) ntohl(sav->spi)));
618 /* Verify correct decryption by checking the last padding bytes */
619 if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
620 if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
621 V_espstat.esps_badenc++;
622 DPRINTF(("%s: decryption failed for packet in "
623 "SA %s/%08lx\n", __func__,
624 ipsec_address(&sav->sah->saidx.dst),
625 (u_long) ntohl(sav->spi)));
631 /* Trim the mbuf chain to remove trailing authenticator and padding */
632 m_adj(m, -(lastthree[1] + 2));
634 /* Restore the Next Protocol field */
635 m_copyback(m, protoff, sizeof (u_int8_t), lastthree + 2);
637 switch (saidx->dst.sa.sa_family) {
640 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag);
645 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag);
649 panic("%s: Unexpected address family: %d saidx=%p", __func__,
650 saidx->dst.sa.sa_family, saidx);
668 * ESP output routine, called by ipsec[46]_process_packet().
673 struct ipsecrequest *isr,
679 struct enc_xform *espx;
680 struct auth_hash *esph;
681 int hlen, rlen, plen, padding, blks, alen, i, roff;
682 struct mbuf *mo = (struct mbuf *) NULL;
683 struct tdb_crypto *tc;
684 struct secasvar *sav;
685 struct secasindex *saidx;
688 int error, maxpacketsize;
690 struct cryptodesc *crde = NULL, *crda = NULL;
694 IPSEC_ASSERT(sav != NULL, ("null SA"));
695 esph = sav->tdb_authalgxform;
696 espx = sav->tdb_encalgxform;
697 IPSEC_ASSERT(espx != NULL, ("null encoding xform"));
699 if (sav->flags & SADB_X_EXT_OLD)
700 hlen = sizeof (struct esp) + sav->ivlen;
702 hlen = sizeof (struct newesp) + sav->ivlen;
704 rlen = m->m_pkthdr.len - skip; /* Raw payload length. */
706 * NB: The null encoding transform has a blocksize of 4
707 * so that headers are properly aligned.
709 blks = espx->blocksize; /* IV blocksize */
711 /* XXX clamp padding length a la KAME??? */
712 padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
713 plen = rlen + padding; /* Padded payload length. */
716 switch (esph->type) {
717 case CRYPTO_SHA2_256_HMAC:
718 case CRYPTO_SHA2_384_HMAC:
719 case CRYPTO_SHA2_512_HMAC:
720 alen = esph->hashsize/2;
723 alen = AH_HMAC_HASHLEN;
729 V_espstat.esps_output++;
731 saidx = &sav->sah->saidx;
732 /* Check for maximum packet size violations. */
733 switch (saidx->dst.sa.sa_family) {
736 maxpacketsize = IP_MAXPACKET;
741 maxpacketsize = IPV6_MAXPACKET;
745 DPRINTF(("%s: unknown/unsupported protocol "
746 "family %d, SA %s/%08lx\n", __func__,
747 saidx->dst.sa.sa_family, ipsec_address(&saidx->dst),
748 (u_long) ntohl(sav->spi)));
749 V_espstat.esps_nopf++;
750 error = EPFNOSUPPORT;
753 if (skip + hlen + rlen + padding + alen > maxpacketsize) {
754 DPRINTF(("%s: packet in SA %s/%08lx got too big "
755 "(len %u, max len %u)\n", __func__,
756 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi),
757 skip + hlen + rlen + padding + alen, maxpacketsize));
758 V_espstat.esps_toobig++;
763 /* Update the counters. */
764 V_espstat.esps_obytes += m->m_pkthdr.len - skip;
766 m = m_unshare(m, M_NOWAIT);
768 DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
769 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
770 V_espstat.esps_hdrops++;
775 /* Inject ESP header. */
776 mo = m_makespace(m, skip, hlen, &roff);
778 DPRINTF(("%s: %u byte ESP hdr inject failed for SA %s/%08lx\n",
779 __func__, hlen, ipsec_address(&saidx->dst),
780 (u_long) ntohl(sav->spi)));
781 V_espstat.esps_hdrops++; /* XXX diffs from openbsd */
786 /* Initialize ESP header. */
787 bcopy((caddr_t) &sav->spi, mtod(mo, caddr_t) + roff, sizeof(u_int32_t));
792 /* Emulate replay attack when ipsec_replay is TRUE. */
795 sav->replay->count++;
796 replay = htonl(sav->replay->count);
797 bcopy((caddr_t) &replay,
798 mtod(mo, caddr_t) + roff + sizeof(u_int32_t),
803 * Add padding -- better to do it ourselves than use the crypto engine,
804 * although if/when we support compression, we'd have to do that.
806 pad = (u_char *) m_pad(m, padding + alen);
808 DPRINTF(("%s: m_pad failed for SA %s/%08lx\n", __func__,
809 ipsec_address(&saidx->dst), (u_long) ntohl(sav->spi)));
810 m = NULL; /* NB: free'd by m_pad */
816 * Add padding: random, zero, or self-describing.
817 * XXX catch unexpected setting
819 switch (sav->flags & SADB_X_EXT_PMASK) {
820 case SADB_X_EXT_PRAND:
821 (void) read_random(pad, padding - 2);
823 case SADB_X_EXT_PZERO:
824 bzero(pad, padding - 2);
826 case SADB_X_EXT_PSEQ:
827 for (i = 0; i < padding - 2; i++)
832 /* Fix padding length and Next Protocol in padding itself. */
833 pad[padding - 2] = padding - 2;
834 m_copydata(m, protoff, sizeof(u_int8_t), pad + padding - 1);
836 /* Fix Next Protocol in IPv4/IPv6 header. */
838 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
840 /* Get crypto descriptors. */
841 crp = crypto_getreq(esph && espx ? 2 : 1);
843 DPRINTF(("%s: failed to acquire crypto descriptors\n",
845 V_espstat.esps_crypto++;
851 crde = crp->crp_desc;
852 crda = crde->crd_next;
854 /* Encryption descriptor. */
855 crde->crd_skip = skip + hlen;
856 crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
857 crde->crd_flags = CRD_F_ENCRYPT;
858 crde->crd_inject = skip + hlen - sav->ivlen;
860 /* Encryption operation. */
861 crde->crd_alg = espx->type;
862 crde->crd_key = sav->key_enc->key_data;
863 crde->crd_klen = _KEYBITS(sav->key_enc);
866 crda = crp->crp_desc;
868 /* IPsec-specific opaque crypto info. */
869 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
870 M_XDATA, M_NOWAIT|M_ZERO);
873 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
874 V_espstat.esps_crypto++;
879 /* Callback parameters */
883 tc->tc_spi = sav->spi;
884 tc->tc_dst = saidx->dst;
885 tc->tc_proto = saidx->proto;
887 /* Crypto operation descriptor. */
888 crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
889 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
890 crp->crp_buf = (caddr_t) m;
891 crp->crp_callback = esp_output_cb;
892 crp->crp_opaque = (caddr_t) tc;
893 crp->crp_sid = sav->tdb_cryptoid;
896 /* Authentication descriptor. */
897 crda->crd_skip = skip;
898 crda->crd_len = m->m_pkthdr.len - (skip + alen);
899 crda->crd_inject = m->m_pkthdr.len - alen;
901 /* Authentication operation. */
902 crda->crd_alg = esph->type;
903 crda->crd_key = sav->key_auth->key_data;
904 crda->crd_klen = _KEYBITS(sav->key_auth);
907 return crypto_dispatch(crp);
915 * ESP output callback from the crypto driver.
918 esp_output_cb(struct cryptop *crp)
920 struct tdb_crypto *tc;
921 struct ipsecrequest *isr;
922 struct secasvar *sav;
926 tc = (struct tdb_crypto *) crp->crp_opaque;
927 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
928 m = (struct mbuf *) crp->crp_buf;
931 IPSECREQUEST_LOCK(isr);
933 /* With the isr lock released SA pointer can be updated. */
934 if (sav != isr->sav) {
935 V_espstat.esps_notdb++;
936 DPRINTF(("%s: SA gone during crypto (SA %s/%08lx proto %u)\n",
937 __func__, ipsec_address(&tc->tc_dst),
938 (u_long) ntohl(tc->tc_spi), tc->tc_proto));
939 error = ENOBUFS; /*XXX*/
943 /* Check for crypto errors. */
944 if (crp->crp_etype) {
945 /* Reset session ID. */
946 if (sav->tdb_cryptoid != 0)
947 sav->tdb_cryptoid = crp->crp_sid;
949 if (crp->crp_etype == EAGAIN) {
950 IPSECREQUEST_UNLOCK(isr);
951 error = crypto_dispatch(crp);
955 V_espstat.esps_noxform++;
956 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
957 error = crp->crp_etype;
961 /* Shouldn't happen... */
963 V_espstat.esps_crypto++;
964 DPRINTF(("%s: bogus returned buffer from crypto\n", __func__));
968 V_espstat.esps_hist[sav->alg_enc]++;
969 if (sav->tdb_authalgxform != NULL)
970 V_ahstat.ahs_hist[sav->alg_auth]++;
972 /* Release crypto descriptors. */
977 /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
978 if (V_ipsec_integrity) {
979 static unsigned char ipseczeroes[AH_HMAC_MAXHASHLEN];
980 struct auth_hash *esph;
983 * Corrupt HMAC if we want to test integrity verification of
986 esph = sav->tdb_authalgxform;
990 switch (esph->type) {
991 case CRYPTO_SHA2_256_HMAC:
992 case CRYPTO_SHA2_384_HMAC:
993 case CRYPTO_SHA2_512_HMAC:
994 alen = esph->hashsize/2;
997 alen = AH_HMAC_HASHLEN;
1000 m_copyback(m, m->m_pkthdr.len - alen,
1006 /* NB: m is reclaimed by ipsec_process_done. */
1007 err = ipsec_process_done(m, isr);
1009 IPSECREQUEST_UNLOCK(isr);
1014 IPSECREQUEST_UNLOCK(isr);
1018 crypto_freereq(crp);
1022 static struct xformsw esp_xformsw = {
1023 XF_ESP, XFT_CONF|XFT_AUTH, "IPsec ESP",
1024 esp_init, esp_zeroize, esp_input,
1031 #define MAXIV(xform) \
1032 if (xform.blocksize > V_esp_max_ivlen) \
1033 V_esp_max_ivlen = xform.blocksize \
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_null); /* SADB_EALG_NULL */
1042 MAXIV(enc_xform_camellia); /* SADB_X_EALG_CAMELLIACBC */
1044 xform_register(&esp_xformsw);
1047 SYSINIT(esp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, esp_attach, NULL);