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