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