xform_esp.c revision 1.70 1 1.70 ozaki /* $NetBSD: xform_esp.c,v 1.70 2017/08/09 09:48:11 ozaki-r Exp $ */
2 1.1 jonathan /* $FreeBSD: src/sys/netipsec/xform_esp.c,v 1.2.2.1 2003/01/24 05:11:36 sam Exp $ */
3 1.1 jonathan /* $OpenBSD: ip_esp.c,v 1.69 2001/06/26 06:18:59 angelos Exp $ */
4 1.1 jonathan
5 1.1 jonathan /*
6 1.1 jonathan * The authors of this code are John Ioannidis (ji (at) tla.org),
7 1.1 jonathan * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 1.1 jonathan * Niels Provos (provos (at) physnet.uni-hamburg.de).
9 1.1 jonathan *
10 1.1 jonathan * The original version of this code was written by John Ioannidis
11 1.1 jonathan * for BSD/OS in Athens, Greece, in November 1995.
12 1.1 jonathan *
13 1.1 jonathan * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 1.1 jonathan * by Angelos D. Keromytis.
15 1.1 jonathan *
16 1.1 jonathan * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 1.1 jonathan * and Niels Provos.
18 1.1 jonathan *
19 1.1 jonathan * Additional features in 1999 by Angelos D. Keromytis.
20 1.1 jonathan *
21 1.1 jonathan * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 1.1 jonathan * Angelos D. Keromytis and Niels Provos.
23 1.1 jonathan * Copyright (c) 2001 Angelos D. Keromytis.
24 1.1 jonathan *
25 1.1 jonathan * Permission to use, copy, and modify this software with or without fee
26 1.1 jonathan * is hereby granted, provided that this entire notice is included in
27 1.1 jonathan * all copies of any software which is or includes a copy or
28 1.1 jonathan * modification of this software.
29 1.1 jonathan * You may use this code under the GNU public license if you so wish. Please
30 1.1 jonathan * contribute changes back to the authors under this freer than GPL license
31 1.1 jonathan * so that we may further the use of strong encryption without limitations to
32 1.1 jonathan * all.
33 1.1 jonathan *
34 1.1 jonathan * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 1.1 jonathan * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 1.1 jonathan * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 1.1 jonathan * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38 1.1 jonathan * PURPOSE.
39 1.1 jonathan */
40 1.1 jonathan
41 1.1 jonathan #include <sys/cdefs.h>
42 1.70 ozaki __KERNEL_RCSID(0, "$NetBSD: xform_esp.c,v 1.70 2017/08/09 09:48:11 ozaki-r Exp $");
43 1.1 jonathan
44 1.47 ozaki #if defined(_KERNEL_OPT)
45 1.1 jonathan #include "opt_inet.h"
46 1.46 ozaki #include "opt_ipsec.h"
47 1.47 ozaki #endif
48 1.1 jonathan
49 1.1 jonathan #include <sys/param.h>
50 1.1 jonathan #include <sys/systm.h>
51 1.1 jonathan #include <sys/mbuf.h>
52 1.1 jonathan #include <sys/socket.h>
53 1.1 jonathan #include <sys/syslog.h>
54 1.1 jonathan #include <sys/kernel.h>
55 1.1 jonathan #include <sys/sysctl.h>
56 1.54 ozaki #include <sys/cprng.h>
57 1.66 ozaki #include <sys/pool.h>
58 1.68 ozaki #include <sys/pserialize.h>
59 1.1 jonathan
60 1.1 jonathan #include <net/if.h>
61 1.1 jonathan
62 1.1 jonathan #include <netinet/in.h>
63 1.1 jonathan #include <netinet/in_systm.h>
64 1.1 jonathan #include <netinet/ip.h>
65 1.1 jonathan #include <netinet/ip_ecn.h>
66 1.1 jonathan #include <netinet/ip6.h>
67 1.1 jonathan
68 1.1 jonathan #include <net/route.h>
69 1.1 jonathan #include <netipsec/ipsec.h>
70 1.18 thorpej #include <netipsec/ipsec_private.h>
71 1.1 jonathan #include <netipsec/ah.h>
72 1.1 jonathan #include <netipsec/ah_var.h>
73 1.1 jonathan #include <netipsec/esp.h>
74 1.1 jonathan #include <netipsec/esp_var.h>
75 1.1 jonathan #include <netipsec/xform.h>
76 1.1 jonathan
77 1.1 jonathan #ifdef INET6
78 1.1 jonathan #include <netinet6/ip6_var.h>
79 1.1 jonathan #include <netipsec/ipsec6.h>
80 1.1 jonathan #endif
81 1.1 jonathan
82 1.4 tls #include <netipsec/key.h>
83 1.4 tls #include <netipsec/key_debug.h>
84 1.1 jonathan
85 1.1 jonathan #include <opencrypto/cryptodev.h>
86 1.1 jonathan #include <opencrypto/xform.h>
87 1.1 jonathan
88 1.18 thorpej percpu_t *espstat_percpu;
89 1.18 thorpej
90 1.1 jonathan int esp_enable = 1;
91 1.1 jonathan
92 1.1 jonathan #ifdef __FreeBSD__
93 1.1 jonathan SYSCTL_DECL(_net_inet_esp);
94 1.1 jonathan SYSCTL_INT(_net_inet_esp, OID_AUTO,
95 1.1 jonathan esp_enable, CTLFLAG_RW, &esp_enable, 0, "");
96 1.1 jonathan SYSCTL_STRUCT(_net_inet_esp, IPSECCTL_STATS,
97 1.1 jonathan stats, CTLFLAG_RD, &espstat, espstat, "");
98 1.4 tls #endif /* __FreeBSD__ */
99 1.1 jonathan
100 1.1 jonathan static int esp_max_ivlen; /* max iv length over all algorithms */
101 1.1 jonathan
102 1.1 jonathan static int esp_input_cb(struct cryptop *op);
103 1.1 jonathan static int esp_output_cb(struct cryptop *crp);
104 1.1 jonathan
105 1.50 christos const uint8_t esp_stats[256] = { SADB_EALG_STATS_INIT };
106 1.50 christos
107 1.66 ozaki static struct pool esp_tdb_crypto_pool;
108 1.66 ozaki static size_t esp_pool_item_size;
109 1.66 ozaki
110 1.1 jonathan /*
111 1.1 jonathan * NB: this is public for use by the PF_KEY support.
112 1.1 jonathan * NB: if you add support here; be sure to add code to esp_attach below!
113 1.1 jonathan */
114 1.28 drochner const struct enc_xform *
115 1.1 jonathan esp_algorithm_lookup(int alg)
116 1.1 jonathan {
117 1.49 ozaki
118 1.1 jonathan switch (alg) {
119 1.1 jonathan case SADB_EALG_DESCBC:
120 1.1 jonathan return &enc_xform_des;
121 1.1 jonathan case SADB_EALG_3DESCBC:
122 1.1 jonathan return &enc_xform_3des;
123 1.1 jonathan case SADB_X_EALG_AES:
124 1.1 jonathan return &enc_xform_rijndael128;
125 1.1 jonathan case SADB_X_EALG_BLOWFISHCBC:
126 1.1 jonathan return &enc_xform_blf;
127 1.1 jonathan case SADB_X_EALG_CAST128CBC:
128 1.1 jonathan return &enc_xform_cast5;
129 1.1 jonathan case SADB_X_EALG_SKIPJACK:
130 1.1 jonathan return &enc_xform_skipjack;
131 1.32 drochner case SADB_X_EALG_CAMELLIACBC:
132 1.32 drochner return &enc_xform_camellia;
133 1.36 drochner case SADB_X_EALG_AESCTR:
134 1.36 drochner return &enc_xform_aes_ctr;
135 1.38 drochner case SADB_X_EALG_AESGCM16:
136 1.38 drochner return &enc_xform_aes_gcm;
137 1.38 drochner case SADB_X_EALG_AESGMAC:
138 1.38 drochner return &enc_xform_aes_gmac;
139 1.1 jonathan case SADB_EALG_NULL:
140 1.1 jonathan return &enc_xform_null;
141 1.1 jonathan }
142 1.1 jonathan return NULL;
143 1.1 jonathan }
144 1.1 jonathan
145 1.1 jonathan size_t
146 1.28 drochner esp_hdrsiz(const struct secasvar *sav)
147 1.1 jonathan {
148 1.1 jonathan size_t size;
149 1.1 jonathan
150 1.1 jonathan if (sav != NULL) {
151 1.1 jonathan /*XXX not right for null algorithm--does it matter??*/
152 1.53 ozaki KASSERT(sav->tdb_encalgxform != NULL);
153 1.1 jonathan if (sav->flags & SADB_X_EXT_OLD)
154 1.51 christos size = sizeof(struct esp);
155 1.1 jonathan else
156 1.51 christos size = sizeof(struct newesp);
157 1.35 drochner size += sav->tdb_encalgxform->ivsize + 9;
158 1.1 jonathan /*XXX need alg check???*/
159 1.1 jonathan if (sav->tdb_authalgxform != NULL && sav->replay)
160 1.1 jonathan size += ah_hdrsiz(sav);
161 1.1 jonathan } else {
162 1.1 jonathan /*
163 1.1 jonathan * base header size
164 1.1 jonathan * + max iv length for CBC mode
165 1.1 jonathan * + max pad length
166 1.51 christos * + sizeof(pad length field)
167 1.51 christos * + sizeof(next header field)
168 1.1 jonathan * + max icv supported.
169 1.1 jonathan */
170 1.60 ozaki size = sizeof(struct newesp) + esp_max_ivlen + 9 +
171 1.60 ozaki ah_hdrsiz(NULL);
172 1.1 jonathan }
173 1.1 jonathan return size;
174 1.1 jonathan }
175 1.1 jonathan
176 1.1 jonathan /*
177 1.1 jonathan * esp_init() is called when an SPI is being set up.
178 1.1 jonathan */
179 1.1 jonathan static int
180 1.28 drochner esp_init(struct secasvar *sav, const struct xformsw *xsp)
181 1.1 jonathan {
182 1.28 drochner const struct enc_xform *txform;
183 1.48 christos struct cryptoini cria, crie, *cr;
184 1.1 jonathan int keylen;
185 1.1 jonathan int error;
186 1.1 jonathan
187 1.1 jonathan txform = esp_algorithm_lookup(sav->alg_enc);
188 1.1 jonathan if (txform == NULL) {
189 1.48 christos DPRINTF(("%s: unsupported encryption algorithm %d\n", __func__,
190 1.48 christos sav->alg_enc));
191 1.1 jonathan return EINVAL;
192 1.1 jonathan }
193 1.1 jonathan if (sav->key_enc == NULL) {
194 1.48 christos DPRINTF(("%s: no encoding key for %s algorithm\n", __func__,
195 1.48 christos txform->name));
196 1.1 jonathan return EINVAL;
197 1.1 jonathan }
198 1.1 jonathan if ((sav->flags&(SADB_X_EXT_OLD|SADB_X_EXT_IV4B)) == SADB_X_EXT_IV4B) {
199 1.48 christos DPRINTF(("%s: 4-byte IV not supported with protocol\n",
200 1.48 christos __func__));
201 1.1 jonathan return EINVAL;
202 1.1 jonathan }
203 1.1 jonathan keylen = _KEYLEN(sav->key_enc);
204 1.1 jonathan if (txform->minkey > keylen || keylen > txform->maxkey) {
205 1.48 christos DPRINTF(("%s: invalid key length %u, must be in "
206 1.48 christos "the range [%u..%u] for algorithm %s\n", __func__,
207 1.48 christos keylen, txform->minkey, txform->maxkey, txform->name));
208 1.1 jonathan return EINVAL;
209 1.1 jonathan }
210 1.1 jonathan
211 1.35 drochner sav->ivlen = txform->ivsize;
212 1.1 jonathan
213 1.1 jonathan /*
214 1.1 jonathan * Setup AH-related state.
215 1.1 jonathan */
216 1.1 jonathan if (sav->alg_auth != 0) {
217 1.1 jonathan error = ah_init0(sav, xsp, &cria);
218 1.1 jonathan if (error)
219 1.1 jonathan return error;
220 1.1 jonathan }
221 1.1 jonathan
222 1.1 jonathan /* NB: override anything set in ah_init0 */
223 1.1 jonathan sav->tdb_xform = xsp;
224 1.1 jonathan sav->tdb_encalgxform = txform;
225 1.1 jonathan
226 1.48 christos switch (sav->alg_enc) {
227 1.48 christos case SADB_X_EALG_AESGCM16:
228 1.48 christos case SADB_X_EALG_AESGMAC:
229 1.38 drochner switch (keylen) {
230 1.38 drochner case 20:
231 1.38 drochner sav->alg_auth = SADB_X_AALG_AES128GMAC;
232 1.38 drochner sav->tdb_authalgxform = &auth_hash_gmac_aes_128;
233 1.38 drochner break;
234 1.38 drochner case 28:
235 1.38 drochner sav->alg_auth = SADB_X_AALG_AES192GMAC;
236 1.38 drochner sav->tdb_authalgxform = &auth_hash_gmac_aes_192;
237 1.38 drochner break;
238 1.38 drochner case 36:
239 1.38 drochner sav->alg_auth = SADB_X_AALG_AES256GMAC;
240 1.38 drochner sav->tdb_authalgxform = &auth_hash_gmac_aes_256;
241 1.38 drochner break;
242 1.48 christos default:
243 1.48 christos DPRINTF(("%s: invalid key length %u, must be either of "
244 1.48 christos "20, 28 or 36\n", __func__, keylen));
245 1.48 christos return EINVAL;
246 1.48 christos }
247 1.48 christos
248 1.38 drochner memset(&cria, 0, sizeof(cria));
249 1.38 drochner cria.cri_alg = sav->tdb_authalgxform->type;
250 1.38 drochner cria.cri_klen = _KEYBITS(sav->key_enc);
251 1.38 drochner cria.cri_key = _KEYBUF(sav->key_enc);
252 1.48 christos break;
253 1.48 christos default:
254 1.48 christos break;
255 1.38 drochner }
256 1.38 drochner
257 1.1 jonathan /* Initialize crypto session. */
258 1.51 christos memset(&crie, 0, sizeof(crie));
259 1.1 jonathan crie.cri_alg = sav->tdb_encalgxform->type;
260 1.1 jonathan crie.cri_klen = _KEYBITS(sav->key_enc);
261 1.1 jonathan crie.cri_key = _KEYBUF(sav->key_enc);
262 1.1 jonathan /* XXX Rounds ? */
263 1.1 jonathan
264 1.1 jonathan if (sav->tdb_authalgxform && sav->tdb_encalgxform) {
265 1.1 jonathan /* init both auth & enc */
266 1.1 jonathan crie.cri_next = &cria;
267 1.48 christos cr = &crie;
268 1.1 jonathan } else if (sav->tdb_encalgxform) {
269 1.48 christos cr = &crie;
270 1.1 jonathan } else if (sav->tdb_authalgxform) {
271 1.48 christos cr = &cria;
272 1.1 jonathan } else {
273 1.1 jonathan /* XXX cannot happen? */
274 1.48 christos DPRINTF(("%s: no encoding OR authentication xform!\n",
275 1.48 christos __func__));
276 1.48 christos return EINVAL;
277 1.1 jonathan }
278 1.48 christos
279 1.48 christos return crypto_newsession(&sav->tdb_cryptoid, cr, crypto_support);
280 1.1 jonathan }
281 1.1 jonathan
282 1.1 jonathan /*
283 1.1 jonathan * Paranoia.
284 1.1 jonathan */
285 1.1 jonathan static int
286 1.1 jonathan esp_zeroize(struct secasvar *sav)
287 1.1 jonathan {
288 1.1 jonathan /* NB: ah_zerorize free's the crypto session state */
289 1.1 jonathan int error = ah_zeroize(sav);
290 1.1 jonathan
291 1.59 ozaki if (sav->key_enc) {
292 1.59 ozaki explicit_memset(_KEYBUF(sav->key_enc), 0,
293 1.59 ozaki _KEYLEN(sav->key_enc));
294 1.59 ozaki }
295 1.1 jonathan sav->tdb_encalgxform = NULL;
296 1.1 jonathan sav->tdb_xform = NULL;
297 1.1 jonathan return error;
298 1.1 jonathan }
299 1.1 jonathan
300 1.1 jonathan /*
301 1.1 jonathan * ESP input processing, called (eventually) through the protocol switch.
302 1.1 jonathan */
303 1.1 jonathan static int
304 1.61 ozaki esp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
305 1.1 jonathan {
306 1.28 drochner const struct auth_hash *esph;
307 1.28 drochner const struct enc_xform *espx;
308 1.1 jonathan struct tdb_crypto *tc;
309 1.40 drochner int plen, alen, hlen, error;
310 1.1 jonathan struct newesp *esp;
311 1.1 jonathan
312 1.1 jonathan struct cryptodesc *crde;
313 1.1 jonathan struct cryptop *crp;
314 1.1 jonathan
315 1.51 christos IPSEC_SPLASSERT_SOFTNET(__func__);
316 1.1 jonathan
317 1.53 ozaki KASSERT(sav != NULL);
318 1.53 ozaki KASSERT(sav->tdb_encalgxform != NULL);
319 1.53 ozaki KASSERTMSG((skip&3) == 0 && (m->m_pkthdr.len&3) == 0,
320 1.53 ozaki "misaligned packet, skip %u pkt len %u",
321 1.53 ozaki skip, m->m_pkthdr.len);
322 1.1 jonathan
323 1.1 jonathan /* XXX don't pullup, just copy header */
324 1.51 christos IP6_EXTHDR_GET(esp, struct newesp *, m, skip, sizeof(struct newesp));
325 1.1 jonathan
326 1.1 jonathan esph = sav->tdb_authalgxform;
327 1.1 jonathan espx = sav->tdb_encalgxform;
328 1.1 jonathan
329 1.1 jonathan /* Determine the ESP header length */
330 1.1 jonathan if (sav->flags & SADB_X_EXT_OLD)
331 1.51 christos hlen = sizeof(struct esp) + sav->ivlen;
332 1.1 jonathan else
333 1.51 christos hlen = sizeof(struct newesp) + sav->ivlen;
334 1.1 jonathan /* Authenticator hash size */
335 1.30 drochner alen = esph ? esph->authsize : 0;
336 1.1 jonathan
337 1.1 jonathan /*
338 1.1 jonathan * Verify payload length is multiple of encryption algorithm
339 1.1 jonathan * block size.
340 1.1 jonathan *
341 1.1 jonathan * NB: This works for the null algorithm because the blocksize
342 1.1 jonathan * is 4 and all packets must be 4-byte aligned regardless
343 1.1 jonathan * of the algorithm.
344 1.1 jonathan */
345 1.1 jonathan plen = m->m_pkthdr.len - (skip + hlen + alen);
346 1.1 jonathan if ((plen & (espx->blocksize - 1)) || (plen <= 0)) {
347 1.55 ryo char buf[IPSEC_ADDRSTRLEN];
348 1.51 christos DPRINTF(("%s: payload of %d octets not a multiple of %d octets,"
349 1.51 christos " SA %s/%08lx\n", __func__, plen, espx->blocksize,
350 1.55 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
351 1.1 jonathan (u_long) ntohl(sav->spi)));
352 1.18 thorpej ESP_STATINC(ESP_STAT_BADILEN);
353 1.1 jonathan m_freem(m);
354 1.1 jonathan return EINVAL;
355 1.1 jonathan }
356 1.1 jonathan
357 1.1 jonathan /*
358 1.1 jonathan * Check sequence number.
359 1.1 jonathan */
360 1.1 jonathan if (esph && sav->replay && !ipsec_chkreplay(ntohl(esp->esp_seq), sav)) {
361 1.55 ryo char logbuf[IPSEC_LOGSASTRLEN];
362 1.55 ryo DPRINTF(("%s: packet replay check for %s\n", __func__,
363 1.55 ryo ipsec_logsastr(sav, logbuf, sizeof(logbuf)))); /*XXX*/
364 1.18 thorpej ESP_STATINC(ESP_STAT_REPLAY);
365 1.1 jonathan m_freem(m);
366 1.1 jonathan return ENOBUFS; /*XXX*/
367 1.1 jonathan }
368 1.1 jonathan
369 1.1 jonathan /* Update the counters */
370 1.18 thorpej ESP_STATADD(ESP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen - alen);
371 1.1 jonathan
372 1.1 jonathan /* Get crypto descriptors */
373 1.1 jonathan crp = crypto_getreq(esph && espx ? 2 : 1);
374 1.1 jonathan if (crp == NULL) {
375 1.51 christos DPRINTF(("%s: failed to acquire crypto descriptors\n",
376 1.51 christos __func__));
377 1.51 christos error = ENOBUFS;
378 1.51 christos goto out;
379 1.1 jonathan }
380 1.1 jonathan
381 1.1 jonathan /* Get IPsec-specific opaque pointer */
382 1.66 ozaki size_t extra __diagused = esph == NULL ? 0 : alen;
383 1.66 ozaki KASSERTMSG(sizeof(*tc) + extra <= esp_pool_item_size,
384 1.66 ozaki "sizeof(*tc) + extra=%zu > esp_pool_item_size=%zu\n",
385 1.66 ozaki sizeof(*tc) + extra, esp_pool_item_size);
386 1.66 ozaki tc = pool_get(&esp_tdb_crypto_pool, PR_NOWAIT);
387 1.1 jonathan if (tc == NULL) {
388 1.51 christos DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
389 1.51 christos error = ENOBUFS;
390 1.51 christos goto out1;
391 1.1 jonathan }
392 1.1 jonathan
393 1.40 drochner error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
394 1.40 drochner if (error) {
395 1.51 christos DPRINTF(("%s: m_makewritable failed\n", __func__));
396 1.51 christos goto out2;
397 1.40 drochner }
398 1.40 drochner
399 1.1 jonathan if (esph) {
400 1.53 ozaki struct cryptodesc *crda;
401 1.1 jonathan
402 1.53 ozaki KASSERT(crp->crp_desc != NULL);
403 1.53 ozaki crda = crp->crp_desc;
404 1.1 jonathan
405 1.1 jonathan /* Authentication descriptor */
406 1.1 jonathan crda->crd_skip = skip;
407 1.38 drochner if (espx && espx->type == CRYPTO_AES_GCM_16)
408 1.38 drochner crda->crd_len = hlen - sav->ivlen;
409 1.38 drochner else
410 1.38 drochner crda->crd_len = m->m_pkthdr.len - (skip + alen);
411 1.1 jonathan crda->crd_inject = m->m_pkthdr.len - alen;
412 1.1 jonathan
413 1.1 jonathan crda->crd_alg = esph->type;
414 1.38 drochner if (espx && (espx->type == CRYPTO_AES_GCM_16 ||
415 1.38 drochner espx->type == CRYPTO_AES_GMAC)) {
416 1.38 drochner crda->crd_key = _KEYBUF(sav->key_enc);
417 1.38 drochner crda->crd_klen = _KEYBITS(sav->key_enc);
418 1.38 drochner } else {
419 1.38 drochner crda->crd_key = _KEYBUF(sav->key_auth);
420 1.38 drochner crda->crd_klen = _KEYBITS(sav->key_auth);
421 1.38 drochner }
422 1.1 jonathan
423 1.1 jonathan /* Copy the authenticator */
424 1.57 ozaki m_copydata(m, m->m_pkthdr.len - alen, alen, (tc + 1));
425 1.1 jonathan
426 1.1 jonathan /* Chain authentication request */
427 1.1 jonathan crde = crda->crd_next;
428 1.1 jonathan } else {
429 1.1 jonathan crde = crp->crp_desc;
430 1.1 jonathan }
431 1.1 jonathan
432 1.70 ozaki {
433 1.70 ozaki int s = pserialize_read_enter();
434 1.70 ozaki
435 1.70 ozaki /*
436 1.70 ozaki * Take another reference to the SA for opencrypto callback.
437 1.70 ozaki */
438 1.70 ozaki if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
439 1.70 ozaki pserialize_read_exit(s);
440 1.70 ozaki pool_put(&esp_tdb_crypto_pool, tc);
441 1.70 ozaki crypto_freereq(crp);
442 1.70 ozaki ESP_STATINC(ESP_STAT_NOTDB);
443 1.70 ozaki return ENOENT;
444 1.70 ozaki }
445 1.70 ozaki KEY_SA_REF(sav);
446 1.70 ozaki pserialize_read_exit(s);
447 1.70 ozaki }
448 1.70 ozaki
449 1.1 jonathan /* Crypto operation descriptor */
450 1.1 jonathan crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
451 1.1 jonathan crp->crp_flags = CRYPTO_F_IMBUF;
452 1.15 degroote crp->crp_buf = m;
453 1.1 jonathan crp->crp_callback = esp_input_cb;
454 1.1 jonathan crp->crp_sid = sav->tdb_cryptoid;
455 1.15 degroote crp->crp_opaque = tc;
456 1.1 jonathan
457 1.1 jonathan /* These are passed as-is to the callback */
458 1.1 jonathan tc->tc_spi = sav->spi;
459 1.1 jonathan tc->tc_dst = sav->sah->saidx.dst;
460 1.1 jonathan tc->tc_proto = sav->sah->saidx.proto;
461 1.1 jonathan tc->tc_protoff = protoff;
462 1.1 jonathan tc->tc_skip = skip;
463 1.61 ozaki tc->tc_sav = sav;
464 1.1 jonathan
465 1.1 jonathan /* Decryption descriptor */
466 1.1 jonathan if (espx) {
467 1.53 ozaki KASSERTMSG(crde != NULL, "null esp crypto descriptor");
468 1.1 jonathan crde->crd_skip = skip + hlen;
469 1.38 drochner if (espx->type == CRYPTO_AES_GMAC)
470 1.38 drochner crde->crd_len = 0;
471 1.38 drochner else
472 1.38 drochner crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
473 1.1 jonathan crde->crd_inject = skip + hlen - sav->ivlen;
474 1.1 jonathan
475 1.1 jonathan crde->crd_alg = espx->type;
476 1.1 jonathan crde->crd_key = _KEYBUF(sav->key_enc);
477 1.1 jonathan crde->crd_klen = _KEYBITS(sav->key_enc);
478 1.1 jonathan /* XXX Rounds ? */
479 1.1 jonathan }
480 1.1 jonathan
481 1.57 ozaki return crypto_dispatch(crp);
482 1.51 christos
483 1.51 christos out2:
484 1.66 ozaki pool_put(&esp_tdb_crypto_pool, tc);
485 1.51 christos out1:
486 1.51 christos crypto_freereq(crp);
487 1.51 christos out:
488 1.51 christos ESP_STATINC(ESP_STAT_CRYPTO);
489 1.51 christos m_freem(m);
490 1.51 christos return error;
491 1.1 jonathan }
492 1.1 jonathan
493 1.1 jonathan #ifdef INET6
494 1.57 ozaki #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
495 1.1 jonathan if (saidx->dst.sa.sa_family == AF_INET6) { \
496 1.57 ozaki error = ipsec6_common_input_cb(m, sav, skip, protoff); \
497 1.1 jonathan } else { \
498 1.57 ozaki error = ipsec4_common_input_cb(m, sav, skip, protoff); \
499 1.1 jonathan } \
500 1.1 jonathan } while (0)
501 1.1 jonathan #else
502 1.57 ozaki #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
503 1.57 ozaki (error = ipsec4_common_input_cb(m, sav, skip, protoff))
504 1.1 jonathan #endif
505 1.1 jonathan
506 1.1 jonathan /*
507 1.1 jonathan * ESP input callback from the crypto driver.
508 1.1 jonathan */
509 1.1 jonathan static int
510 1.1 jonathan esp_input_cb(struct cryptop *crp)
511 1.1 jonathan {
512 1.55 ryo char buf[IPSEC_ADDRSTRLEN];
513 1.51 christos uint8_t lastthree[3], aalg[AH_ALEN_MAX];
514 1.67 ozaki int hlen, skip, protoff, error;
515 1.1 jonathan struct mbuf *m;
516 1.28 drochner const struct auth_hash *esph;
517 1.1 jonathan struct tdb_crypto *tc;
518 1.1 jonathan struct secasvar *sav;
519 1.1 jonathan struct secasindex *saidx;
520 1.13 christos void *ptr;
521 1.51 christos uint16_t dport;
522 1.51 christos uint16_t sport;
523 1.67 ozaki IPSEC_DECLARE_LOCK_VARIABLE;
524 1.1 jonathan
525 1.53 ozaki KASSERT(crp->crp_desc != NULL);
526 1.53 ozaki KASSERT(crp->crp_opaque != NULL);
527 1.1 jonathan
528 1.51 christos tc = crp->crp_opaque;
529 1.1 jonathan skip = tc->tc_skip;
530 1.1 jonathan protoff = tc->tc_protoff;
531 1.51 christos m = crp->crp_buf;
532 1.1 jonathan
533 1.16 degroote /* find the source port for NAT-T */
534 1.42 christos nat_t_ports_get(m, &dport, &sport);
535 1.16 degroote
536 1.67 ozaki IPSEC_ACQUIRE_GLOBAL_LOCKS();
537 1.1 jonathan
538 1.61 ozaki sav = tc->tc_sav;
539 1.61 ozaki if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
540 1.69 ozaki KEY_SA_UNREF(&sav);
541 1.61 ozaki sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
542 1.61 ozaki sport, dport);
543 1.61 ozaki if (sav == NULL) {
544 1.61 ozaki ESP_STATINC(ESP_STAT_NOTDB);
545 1.61 ozaki DPRINTF(("%s: SA expired while in crypto "
546 1.61 ozaki "(SA %s/%08lx proto %u)\n", __func__,
547 1.61 ozaki ipsec_address(&tc->tc_dst, buf, sizeof(buf)),
548 1.61 ozaki (u_long) ntohl(tc->tc_spi), tc->tc_proto));
549 1.61 ozaki error = ENOBUFS; /*XXX*/
550 1.61 ozaki goto bad;
551 1.61 ozaki }
552 1.1 jonathan }
553 1.1 jonathan
554 1.1 jonathan saidx = &sav->sah->saidx;
555 1.53 ozaki KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
556 1.51 christos saidx->dst.sa.sa_family == AF_INET6,
557 1.53 ozaki "unexpected protocol family %u", saidx->dst.sa.sa_family);
558 1.1 jonathan
559 1.1 jonathan esph = sav->tdb_authalgxform;
560 1.1 jonathan
561 1.1 jonathan /* Check for crypto errors */
562 1.1 jonathan if (crp->crp_etype) {
563 1.1 jonathan /* Reset the session ID */
564 1.1 jonathan if (sav->tdb_cryptoid != 0)
565 1.1 jonathan sav->tdb_cryptoid = crp->crp_sid;
566 1.1 jonathan
567 1.1 jonathan if (crp->crp_etype == EAGAIN) {
568 1.69 ozaki KEY_SA_UNREF(&sav);
569 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
570 1.1 jonathan return crypto_dispatch(crp);
571 1.1 jonathan }
572 1.1 jonathan
573 1.18 thorpej ESP_STATINC(ESP_STAT_NOXFORM);
574 1.51 christos DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
575 1.1 jonathan error = crp->crp_etype;
576 1.1 jonathan goto bad;
577 1.1 jonathan }
578 1.1 jonathan
579 1.50 christos ESP_STATINC(ESP_STAT_HIST + esp_stats[sav->alg_enc]);
580 1.1 jonathan
581 1.1 jonathan /* If authentication was performed, check now. */
582 1.1 jonathan if (esph != NULL) {
583 1.1 jonathan /*
584 1.1 jonathan * If we have a tag, it means an IPsec-aware NIC did
585 1.1 jonathan * the verification for us. Otherwise we need to
586 1.1 jonathan * check the authentication calculation.
587 1.1 jonathan */
588 1.50 christos AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
589 1.57 ozaki /* Copy the authenticator from the packet */
590 1.57 ozaki m_copydata(m, m->m_pkthdr.len - esph->authsize,
591 1.57 ozaki esph->authsize, aalg);
592 1.57 ozaki
593 1.57 ozaki ptr = (tc + 1);
594 1.57 ozaki
595 1.57 ozaki /* Verify authenticator */
596 1.57 ozaki if (!consttime_memequal(ptr, aalg, esph->authsize)) {
597 1.57 ozaki DPRINTF(("%s: authentication hash mismatch "
598 1.57 ozaki "for packet in SA %s/%08lx\n", __func__,
599 1.57 ozaki ipsec_address(&saidx->dst, buf,
600 1.57 ozaki sizeof(buf)), (u_long) ntohl(sav->spi)));
601 1.57 ozaki ESP_STATINC(ESP_STAT_BADAUTH);
602 1.57 ozaki error = EACCES;
603 1.57 ozaki goto bad;
604 1.1 jonathan }
605 1.1 jonathan
606 1.1 jonathan /* Remove trailing authenticator */
607 1.1 jonathan m_adj(m, -(esph->authsize));
608 1.1 jonathan }
609 1.1 jonathan
610 1.1 jonathan /* Release the crypto descriptors */
611 1.66 ozaki pool_put(&esp_tdb_crypto_pool, tc);
612 1.66 ozaki tc = NULL;
613 1.1 jonathan crypto_freereq(crp), crp = NULL;
614 1.1 jonathan
615 1.1 jonathan /*
616 1.1 jonathan * Packet is now decrypted.
617 1.1 jonathan */
618 1.1 jonathan m->m_flags |= M_DECRYPTED;
619 1.1 jonathan
620 1.8 rpaulo /*
621 1.8 rpaulo * Update replay sequence number, if appropriate.
622 1.8 rpaulo */
623 1.8 rpaulo if (sav->replay) {
624 1.51 christos uint32_t seq;
625 1.8 rpaulo
626 1.8 rpaulo m_copydata(m, skip + offsetof(struct newesp, esp_seq),
627 1.51 christos sizeof(seq), &seq);
628 1.8 rpaulo if (ipsec_updatereplay(ntohl(seq), sav)) {
629 1.55 ryo char logbuf[IPSEC_LOGSASTRLEN];
630 1.8 rpaulo DPRINTF(("%s: packet replay check for %s\n", __func__,
631 1.55 ryo ipsec_logsastr(sav, logbuf, sizeof(logbuf))));
632 1.18 thorpej ESP_STATINC(ESP_STAT_REPLAY);
633 1.8 rpaulo error = ENOBUFS;
634 1.8 rpaulo goto bad;
635 1.8 rpaulo }
636 1.8 rpaulo }
637 1.8 rpaulo
638 1.1 jonathan /* Determine the ESP header length */
639 1.1 jonathan if (sav->flags & SADB_X_EXT_OLD)
640 1.51 christos hlen = sizeof(struct esp) + sav->ivlen;
641 1.1 jonathan else
642 1.51 christos hlen = sizeof(struct newesp) + sav->ivlen;
643 1.1 jonathan
644 1.1 jonathan /* Remove the ESP header and IV from the mbuf. */
645 1.1 jonathan error = m_striphdr(m, skip, hlen);
646 1.1 jonathan if (error) {
647 1.18 thorpej ESP_STATINC(ESP_STAT_HDROPS);
648 1.51 christos DPRINTF(("%s: bad mbuf chain, SA %s/%08lx\n", __func__,
649 1.55 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
650 1.1 jonathan (u_long) ntohl(sav->spi)));
651 1.1 jonathan goto bad;
652 1.1 jonathan }
653 1.1 jonathan
654 1.1 jonathan /* Save the last three bytes of decrypted data */
655 1.1 jonathan m_copydata(m, m->m_pkthdr.len - 3, 3, lastthree);
656 1.1 jonathan
657 1.1 jonathan /* Verify pad length */
658 1.1 jonathan if (lastthree[1] + 2 > m->m_pkthdr.len - skip) {
659 1.18 thorpej ESP_STATINC(ESP_STAT_BADILEN);
660 1.51 christos DPRINTF(("%s: invalid padding length %d "
661 1.51 christos "for %u byte packet in SA %s/%08lx\n", __func__,
662 1.51 christos lastthree[1], m->m_pkthdr.len - skip,
663 1.55 ryo ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
664 1.51 christos (u_long) ntohl(sav->spi)));
665 1.1 jonathan error = EINVAL;
666 1.1 jonathan goto bad;
667 1.1 jonathan }
668 1.1 jonathan
669 1.1 jonathan /* Verify correct decryption by checking the last padding bytes */
670 1.1 jonathan if ((sav->flags & SADB_X_EXT_PMASK) != SADB_X_EXT_PRAND) {
671 1.1 jonathan if (lastthree[1] != lastthree[0] && lastthree[1] != 0) {
672 1.18 thorpej ESP_STATINC(ESP_STAT_BADENC);
673 1.51 christos DPRINTF(("%s: decryption failed for packet in SA "
674 1.51 christos "%s/%08lx\n", __func__,
675 1.55 ryo ipsec_address(&sav->sah->saidx.dst, buf,
676 1.55 ryo sizeof(buf)), (u_long) ntohl(sav->spi)));
677 1.51 christos DPRINTF(("%s: %x %x\n", __func__, lastthree[0],
678 1.51 christos lastthree[1]));
679 1.1 jonathan error = EINVAL;
680 1.1 jonathan goto bad;
681 1.1 jonathan }
682 1.1 jonathan }
683 1.1 jonathan
684 1.1 jonathan /* Trim the mbuf chain to remove trailing authenticator and padding */
685 1.1 jonathan m_adj(m, -(lastthree[1] + 2));
686 1.1 jonathan
687 1.1 jonathan /* Restore the Next Protocol field */
688 1.51 christos m_copyback(m, protoff, sizeof(uint8_t), lastthree + 2);
689 1.1 jonathan
690 1.57 ozaki IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
691 1.1 jonathan
692 1.69 ozaki KEY_SA_UNREF(&sav);
693 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
694 1.1 jonathan return error;
695 1.1 jonathan bad:
696 1.1 jonathan if (sav)
697 1.69 ozaki KEY_SA_UNREF(&sav);
698 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
699 1.1 jonathan if (m != NULL)
700 1.1 jonathan m_freem(m);
701 1.1 jonathan if (tc != NULL)
702 1.66 ozaki pool_put(&esp_tdb_crypto_pool, tc);
703 1.1 jonathan if (crp != NULL)
704 1.1 jonathan crypto_freereq(crp);
705 1.1 jonathan return error;
706 1.1 jonathan }
707 1.1 jonathan
708 1.1 jonathan /*
709 1.1 jonathan * ESP output routine, called by ipsec[46]_process_packet().
710 1.1 jonathan */
711 1.1 jonathan static int
712 1.1 jonathan esp_output(
713 1.11 christos struct mbuf *m,
714 1.11 christos struct ipsecrequest *isr,
715 1.62 ozaki struct secasvar *sav,
716 1.12 christos struct mbuf **mp,
717 1.11 christos int skip,
718 1.11 christos int protoff
719 1.1 jonathan )
720 1.1 jonathan {
721 1.55 ryo char buf[IPSEC_ADDRSTRLEN];
722 1.28 drochner const struct enc_xform *espx;
723 1.28 drochner const struct auth_hash *esph;
724 1.45 mrg int hlen, rlen, padding, blks, alen, i, roff;
725 1.39 plunky struct mbuf *mo = NULL;
726 1.1 jonathan struct tdb_crypto *tc;
727 1.1 jonathan struct secasindex *saidx;
728 1.1 jonathan unsigned char *pad;
729 1.51 christos uint8_t prot;
730 1.1 jonathan int error, maxpacketsize;
731 1.1 jonathan
732 1.1 jonathan struct cryptodesc *crde = NULL, *crda = NULL;
733 1.1 jonathan struct cryptop *crp;
734 1.1 jonathan
735 1.51 christos IPSEC_SPLASSERT_SOFTNET(__func__);
736 1.1 jonathan
737 1.1 jonathan esph = sav->tdb_authalgxform;
738 1.53 ozaki KASSERT(sav->tdb_encalgxform != NULL);
739 1.1 jonathan espx = sav->tdb_encalgxform;
740 1.1 jonathan
741 1.1 jonathan if (sav->flags & SADB_X_EXT_OLD)
742 1.51 christos hlen = sizeof(struct esp) + sav->ivlen;
743 1.1 jonathan else
744 1.51 christos hlen = sizeof(struct newesp) + sav->ivlen;
745 1.1 jonathan
746 1.1 jonathan rlen = m->m_pkthdr.len - skip; /* Raw payload length. */
747 1.1 jonathan /*
748 1.1 jonathan * NB: The null encoding transform has a blocksize of 4
749 1.1 jonathan * so that headers are properly aligned.
750 1.1 jonathan */
751 1.1 jonathan blks = espx->blocksize; /* IV blocksize */
752 1.1 jonathan
753 1.1 jonathan /* XXX clamp padding length a la KAME??? */
754 1.1 jonathan padding = ((blks - ((rlen + 2) % blks)) % blks) + 2;
755 1.1 jonathan
756 1.1 jonathan if (esph)
757 1.30 drochner alen = esph->authsize;
758 1.1 jonathan else
759 1.1 jonathan alen = 0;
760 1.1 jonathan
761 1.18 thorpej ESP_STATINC(ESP_STAT_OUTPUT);
762 1.1 jonathan
763 1.1 jonathan saidx = &sav->sah->saidx;
764 1.1 jonathan /* Check for maximum packet size violations. */
765 1.1 jonathan switch (saidx->dst.sa.sa_family) {
766 1.1 jonathan #ifdef INET
767 1.1 jonathan case AF_INET:
768 1.1 jonathan maxpacketsize = IP_MAXPACKET;
769 1.1 jonathan break;
770 1.1 jonathan #endif /* INET */
771 1.1 jonathan #ifdef INET6
772 1.1 jonathan case AF_INET6:
773 1.1 jonathan maxpacketsize = IPV6_MAXPACKET;
774 1.1 jonathan break;
775 1.1 jonathan #endif /* INET6 */
776 1.1 jonathan default:
777 1.51 christos DPRINTF(("%s: unknown/unsupported protocol family %d, "
778 1.51 christos "SA %s/%08lx\n", __func__, saidx->dst.sa.sa_family,
779 1.55 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
780 1.55 ryo (u_long)ntohl(sav->spi)));
781 1.18 thorpej ESP_STATINC(ESP_STAT_NOPF);
782 1.1 jonathan error = EPFNOSUPPORT;
783 1.1 jonathan goto bad;
784 1.1 jonathan }
785 1.1 jonathan if (skip + hlen + rlen + padding + alen > maxpacketsize) {
786 1.51 christos DPRINTF(("%s: packet in SA %s/%08lx got too big (len %u, "
787 1.55 ryo "max len %u)\n", __func__,
788 1.55 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
789 1.51 christos (u_long) ntohl(sav->spi),
790 1.1 jonathan skip + hlen + rlen + padding + alen, maxpacketsize));
791 1.18 thorpej ESP_STATINC(ESP_STAT_TOOBIG);
792 1.1 jonathan error = EMSGSIZE;
793 1.1 jonathan goto bad;
794 1.1 jonathan }
795 1.1 jonathan
796 1.1 jonathan /* Update the counters. */
797 1.25 drochner ESP_STATADD(ESP_STAT_OBYTES, m->m_pkthdr.len - skip);
798 1.1 jonathan
799 1.1 jonathan m = m_clone(m);
800 1.1 jonathan if (m == NULL) {
801 1.51 christos DPRINTF(("%s: cannot clone mbuf chain, SA %s/%08lx\n", __func__,
802 1.55 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
803 1.55 ryo (u_long) ntohl(sav->spi)));
804 1.18 thorpej ESP_STATINC(ESP_STAT_HDROPS);
805 1.1 jonathan error = ENOBUFS;
806 1.1 jonathan goto bad;
807 1.1 jonathan }
808 1.1 jonathan
809 1.1 jonathan /* Inject ESP header. */
810 1.1 jonathan mo = m_makespace(m, skip, hlen, &roff);
811 1.1 jonathan if (mo == NULL) {
812 1.51 christos DPRINTF(("%s: failed to inject %u byte ESP hdr for SA "
813 1.55 ryo "%s/%08lx\n", __func__, hlen,
814 1.55 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
815 1.1 jonathan (u_long) ntohl(sav->spi)));
816 1.18 thorpej ESP_STATINC(ESP_STAT_HDROPS); /* XXX diffs from openbsd */
817 1.1 jonathan error = ENOBUFS;
818 1.1 jonathan goto bad;
819 1.1 jonathan }
820 1.1 jonathan
821 1.1 jonathan /* Initialize ESP header. */
822 1.51 christos memcpy(mtod(mo, char *) + roff, &sav->spi, sizeof(uint32_t));
823 1.1 jonathan if (sav->replay) {
824 1.51 christos uint32_t replay;
825 1.9 rpaulo
826 1.9 rpaulo #ifdef IPSEC_DEBUG
827 1.9 rpaulo /* Emulate replay attack when ipsec_replay is TRUE. */
828 1.9 rpaulo if (!ipsec_replay)
829 1.9 rpaulo #endif
830 1.9 rpaulo sav->replay->count++;
831 1.9 rpaulo
832 1.9 rpaulo replay = htonl(sav->replay->count);
833 1.51 christos memcpy(mtod(mo,char *) + roff + sizeof(uint32_t), &replay,
834 1.51 christos sizeof(uint32_t));
835 1.1 jonathan }
836 1.1 jonathan
837 1.1 jonathan /*
838 1.1 jonathan * Add padding -- better to do it ourselves than use the crypto engine,
839 1.1 jonathan * although if/when we support compression, we'd have to do that.
840 1.1 jonathan */
841 1.51 christos pad = m_pad(m, padding + alen);
842 1.1 jonathan if (pad == NULL) {
843 1.51 christos DPRINTF(("%s: m_pad failed for SA %s/%08lx\n", __func__,
844 1.55 ryo ipsec_address(&saidx->dst, buf, sizeof(buf)),
845 1.55 ryo (u_long) ntohl(sav->spi)));
846 1.1 jonathan m = NULL; /* NB: free'd by m_pad */
847 1.1 jonathan error = ENOBUFS;
848 1.1 jonathan goto bad;
849 1.1 jonathan }
850 1.1 jonathan
851 1.1 jonathan /*
852 1.1 jonathan * Add padding: random, zero, or self-describing.
853 1.1 jonathan * XXX catch unexpected setting
854 1.1 jonathan */
855 1.1 jonathan switch (sav->flags & SADB_X_EXT_PMASK) {
856 1.1 jonathan case SADB_X_EXT_PRAND:
857 1.54 ozaki (void) cprng_fast(pad, padding - 2);
858 1.1 jonathan break;
859 1.1 jonathan case SADB_X_EXT_PZERO:
860 1.20 cegger memset(pad, 0, padding - 2);
861 1.1 jonathan break;
862 1.1 jonathan case SADB_X_EXT_PSEQ:
863 1.1 jonathan for (i = 0; i < padding - 2; i++)
864 1.1 jonathan pad[i] = i+1;
865 1.1 jonathan break;
866 1.1 jonathan }
867 1.1 jonathan
868 1.1 jonathan /* Fix padding length and Next Protocol in padding itself. */
869 1.1 jonathan pad[padding - 2] = padding - 2;
870 1.51 christos m_copydata(m, protoff, sizeof(uint8_t), pad + padding - 1);
871 1.1 jonathan
872 1.1 jonathan /* Fix Next Protocol in IPv4/IPv6 header. */
873 1.1 jonathan prot = IPPROTO_ESP;
874 1.51 christos m_copyback(m, protoff, sizeof(uint8_t), &prot);
875 1.1 jonathan
876 1.1 jonathan /* Get crypto descriptors. */
877 1.1 jonathan crp = crypto_getreq(esph && espx ? 2 : 1);
878 1.1 jonathan if (crp == NULL) {
879 1.51 christos DPRINTF(("%s: failed to acquire crypto descriptors\n",
880 1.51 christos __func__));
881 1.18 thorpej ESP_STATINC(ESP_STAT_CRYPTO);
882 1.1 jonathan error = ENOBUFS;
883 1.1 jonathan goto bad;
884 1.1 jonathan }
885 1.1 jonathan
886 1.1 jonathan if (espx) {
887 1.1 jonathan crde = crp->crp_desc;
888 1.1 jonathan crda = crde->crd_next;
889 1.1 jonathan
890 1.1 jonathan /* Encryption descriptor. */
891 1.1 jonathan crde->crd_skip = skip + hlen;
892 1.38 drochner if (espx->type == CRYPTO_AES_GMAC)
893 1.38 drochner crde->crd_len = 0;
894 1.38 drochner else
895 1.38 drochner crde->crd_len = m->m_pkthdr.len - (skip + hlen + alen);
896 1.1 jonathan crde->crd_flags = CRD_F_ENCRYPT;
897 1.1 jonathan crde->crd_inject = skip + hlen - sav->ivlen;
898 1.1 jonathan
899 1.1 jonathan /* Encryption operation. */
900 1.1 jonathan crde->crd_alg = espx->type;
901 1.1 jonathan crde->crd_key = _KEYBUF(sav->key_enc);
902 1.1 jonathan crde->crd_klen = _KEYBITS(sav->key_enc);
903 1.1 jonathan /* XXX Rounds ? */
904 1.1 jonathan } else
905 1.1 jonathan crda = crp->crp_desc;
906 1.1 jonathan
907 1.1 jonathan /* IPsec-specific opaque crypto info. */
908 1.66 ozaki tc = pool_get(&esp_tdb_crypto_pool, PR_NOWAIT);
909 1.1 jonathan if (tc == NULL) {
910 1.1 jonathan crypto_freereq(crp);
911 1.51 christos DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
912 1.18 thorpej ESP_STATINC(ESP_STAT_CRYPTO);
913 1.1 jonathan error = ENOBUFS;
914 1.1 jonathan goto bad;
915 1.1 jonathan }
916 1.1 jonathan
917 1.68 ozaki {
918 1.68 ozaki int s = pserialize_read_enter();
919 1.68 ozaki
920 1.70 ozaki /*
921 1.70 ozaki * Take another reference to the SP and the SA for opencrypto callback.
922 1.70 ozaki */
923 1.70 ozaki if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
924 1.70 ozaki sav->state == SADB_SASTATE_DEAD)) {
925 1.68 ozaki pserialize_read_exit(s);
926 1.68 ozaki pool_put(&esp_tdb_crypto_pool, tc);
927 1.68 ozaki crypto_freereq(crp);
928 1.68 ozaki ESP_STATINC(ESP_STAT_NOTDB);
929 1.68 ozaki error = ENOENT;
930 1.68 ozaki goto bad;
931 1.68 ozaki }
932 1.68 ozaki KEY_SP_REF(isr->sp);
933 1.70 ozaki KEY_SA_REF(sav);
934 1.68 ozaki pserialize_read_exit(s);
935 1.68 ozaki }
936 1.68 ozaki
937 1.1 jonathan /* Callback parameters */
938 1.1 jonathan tc->tc_isr = isr;
939 1.1 jonathan tc->tc_spi = sav->spi;
940 1.1 jonathan tc->tc_dst = saidx->dst;
941 1.1 jonathan tc->tc_proto = saidx->proto;
942 1.61 ozaki tc->tc_sav = sav;
943 1.1 jonathan
944 1.1 jonathan /* Crypto operation descriptor. */
945 1.1 jonathan crp->crp_ilen = m->m_pkthdr.len; /* Total input length. */
946 1.1 jonathan crp->crp_flags = CRYPTO_F_IMBUF;
947 1.15 degroote crp->crp_buf = m;
948 1.1 jonathan crp->crp_callback = esp_output_cb;
949 1.15 degroote crp->crp_opaque = tc;
950 1.1 jonathan crp->crp_sid = sav->tdb_cryptoid;
951 1.1 jonathan
952 1.1 jonathan if (esph) {
953 1.1 jonathan /* Authentication descriptor. */
954 1.1 jonathan crda->crd_skip = skip;
955 1.38 drochner if (espx && espx->type == CRYPTO_AES_GCM_16)
956 1.38 drochner crda->crd_len = hlen - sav->ivlen;
957 1.38 drochner else
958 1.38 drochner crda->crd_len = m->m_pkthdr.len - (skip + alen);
959 1.1 jonathan crda->crd_inject = m->m_pkthdr.len - alen;
960 1.1 jonathan
961 1.1 jonathan /* Authentication operation. */
962 1.1 jonathan crda->crd_alg = esph->type;
963 1.38 drochner if (espx && (espx->type == CRYPTO_AES_GCM_16 ||
964 1.38 drochner espx->type == CRYPTO_AES_GMAC)) {
965 1.38 drochner crda->crd_key = _KEYBUF(sav->key_enc);
966 1.38 drochner crda->crd_klen = _KEYBITS(sav->key_enc);
967 1.38 drochner } else {
968 1.38 drochner crda->crd_key = _KEYBUF(sav->key_auth);
969 1.38 drochner crda->crd_klen = _KEYBITS(sav->key_auth);
970 1.38 drochner }
971 1.1 jonathan }
972 1.1 jonathan
973 1.1 jonathan return crypto_dispatch(crp);
974 1.1 jonathan bad:
975 1.1 jonathan if (m)
976 1.1 jonathan m_freem(m);
977 1.1 jonathan return (error);
978 1.1 jonathan }
979 1.1 jonathan
980 1.1 jonathan /*
981 1.1 jonathan * ESP output callback from the crypto driver.
982 1.1 jonathan */
983 1.1 jonathan static int
984 1.1 jonathan esp_output_cb(struct cryptop *crp)
985 1.1 jonathan {
986 1.1 jonathan struct tdb_crypto *tc;
987 1.1 jonathan struct ipsecrequest *isr;
988 1.1 jonathan struct secasvar *sav;
989 1.1 jonathan struct mbuf *m;
990 1.67 ozaki int err, error;
991 1.67 ozaki IPSEC_DECLARE_LOCK_VARIABLE;
992 1.1 jonathan
993 1.53 ozaki KASSERT(crp->crp_opaque != NULL);
994 1.51 christos tc = crp->crp_opaque;
995 1.51 christos m = crp->crp_buf;
996 1.1 jonathan
997 1.67 ozaki IPSEC_ACQUIRE_GLOBAL_LOCKS();
998 1.1 jonathan
999 1.1 jonathan isr = tc->tc_isr;
1000 1.61 ozaki sav = tc->tc_sav;
1001 1.65 ozaki if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
1002 1.65 ozaki ESP_STATINC(ESP_STAT_NOTDB);
1003 1.65 ozaki IPSECLOG(LOG_DEBUG,
1004 1.65 ozaki "SP is being destroyed while in crypto (id=%u)\n",
1005 1.65 ozaki isr->sp->id);
1006 1.65 ozaki error = ENOENT;
1007 1.65 ozaki goto bad;
1008 1.65 ozaki }
1009 1.61 ozaki if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
1010 1.69 ozaki KEY_SA_UNREF(&sav);
1011 1.61 ozaki sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
1012 1.61 ozaki if (sav == NULL) {
1013 1.61 ozaki char buf[IPSEC_ADDRSTRLEN];
1014 1.61 ozaki ESP_STATINC(ESP_STAT_NOTDB);
1015 1.61 ozaki DPRINTF(("%s: SA expired while in crypto (SA %s/%08lx "
1016 1.61 ozaki "proto %u)\n", __func__,
1017 1.61 ozaki ipsec_address(&tc->tc_dst, buf, sizeof(buf)),
1018 1.61 ozaki (u_long) ntohl(tc->tc_spi), tc->tc_proto));
1019 1.61 ozaki error = ENOBUFS; /*XXX*/
1020 1.61 ozaki goto bad;
1021 1.61 ozaki }
1022 1.1 jonathan }
1023 1.1 jonathan
1024 1.1 jonathan /* Check for crypto errors. */
1025 1.1 jonathan if (crp->crp_etype) {
1026 1.1 jonathan /* Reset session ID. */
1027 1.1 jonathan if (sav->tdb_cryptoid != 0)
1028 1.1 jonathan sav->tdb_cryptoid = crp->crp_sid;
1029 1.1 jonathan
1030 1.1 jonathan if (crp->crp_etype == EAGAIN) {
1031 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
1032 1.1 jonathan return crypto_dispatch(crp);
1033 1.1 jonathan }
1034 1.1 jonathan
1035 1.18 thorpej ESP_STATINC(ESP_STAT_NOXFORM);
1036 1.51 christos DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
1037 1.1 jonathan error = crp->crp_etype;
1038 1.1 jonathan goto bad;
1039 1.1 jonathan }
1040 1.1 jonathan
1041 1.50 christos ESP_STATINC(ESP_STAT_HIST + esp_stats[sav->alg_enc]);
1042 1.1 jonathan if (sav->tdb_authalgxform != NULL)
1043 1.50 christos AH_STATINC(AH_STAT_HIST + ah_stats[sav->alg_auth]);
1044 1.1 jonathan
1045 1.1 jonathan /* Release crypto descriptors. */
1046 1.66 ozaki pool_put(&esp_tdb_crypto_pool, tc);
1047 1.1 jonathan crypto_freereq(crp);
1048 1.1 jonathan
1049 1.9 rpaulo #ifdef IPSEC_DEBUG
1050 1.9 rpaulo /* Emulate man-in-the-middle attack when ipsec_integrity is TRUE. */
1051 1.9 rpaulo if (ipsec_integrity) {
1052 1.30 drochner static unsigned char ipseczeroes[AH_ALEN_MAX];
1053 1.29 degroote const struct auth_hash *esph;
1054 1.9 rpaulo
1055 1.9 rpaulo /*
1056 1.9 rpaulo * Corrupt HMAC if we want to test integrity verification of
1057 1.9 rpaulo * the other side.
1058 1.9 rpaulo */
1059 1.9 rpaulo esph = sav->tdb_authalgxform;
1060 1.9 rpaulo if (esph != NULL) {
1061 1.31 spz m_copyback(m, m->m_pkthdr.len - esph->authsize,
1062 1.31 spz esph->authsize, ipseczeroes);
1063 1.9 rpaulo }
1064 1.9 rpaulo }
1065 1.9 rpaulo #endif
1066 1.9 rpaulo
1067 1.1 jonathan /* NB: m is reclaimed by ipsec_process_done. */
1068 1.62 ozaki err = ipsec_process_done(m, isr, sav);
1069 1.69 ozaki KEY_SA_UNREF(&sav);
1070 1.68 ozaki KEY_SP_UNREF(&isr->sp);
1071 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
1072 1.1 jonathan return err;
1073 1.1 jonathan bad:
1074 1.1 jonathan if (sav)
1075 1.69 ozaki KEY_SA_UNREF(&sav);
1076 1.68 ozaki KEY_SP_UNREF(&isr->sp);
1077 1.67 ozaki IPSEC_RELEASE_GLOBAL_LOCKS();
1078 1.1 jonathan if (m)
1079 1.1 jonathan m_freem(m);
1080 1.66 ozaki pool_put(&esp_tdb_crypto_pool, tc);
1081 1.1 jonathan crypto_freereq(crp);
1082 1.1 jonathan return error;
1083 1.1 jonathan }
1084 1.1 jonathan
1085 1.1 jonathan static struct xformsw esp_xformsw = {
1086 1.56 ozaki .xf_type = XF_ESP,
1087 1.56 ozaki .xf_flags = XFT_CONF|XFT_AUTH,
1088 1.56 ozaki .xf_name = "IPsec ESP",
1089 1.56 ozaki .xf_init = esp_init,
1090 1.56 ozaki .xf_zeroize = esp_zeroize,
1091 1.56 ozaki .xf_input = esp_input,
1092 1.56 ozaki .xf_output = esp_output,
1093 1.56 ozaki .xf_next = NULL,
1094 1.1 jonathan };
1095 1.1 jonathan
1096 1.54 ozaki void
1097 1.1 jonathan esp_attach(void)
1098 1.1 jonathan {
1099 1.18 thorpej
1100 1.18 thorpej espstat_percpu = percpu_alloc(sizeof(uint64_t) * ESP_NSTATS);
1101 1.18 thorpej
1102 1.66 ozaki extern int ah_max_authsize;
1103 1.66 ozaki KASSERT(ah_max_authsize != 0);
1104 1.66 ozaki esp_pool_item_size = sizeof(struct tdb_crypto) + ah_max_authsize;
1105 1.66 ozaki pool_init(&esp_tdb_crypto_pool, esp_pool_item_size,
1106 1.66 ozaki 0, 0, 0, "esp_tdb_crypto", NULL, IPL_SOFTNET);
1107 1.66 ozaki
1108 1.1 jonathan #define MAXIV(xform) \
1109 1.35 drochner if (xform.ivsize > esp_max_ivlen) \
1110 1.35 drochner esp_max_ivlen = xform.ivsize \
1111 1.1 jonathan
1112 1.1 jonathan esp_max_ivlen = 0;
1113 1.1 jonathan MAXIV(enc_xform_des); /* SADB_EALG_DESCBC */
1114 1.1 jonathan MAXIV(enc_xform_3des); /* SADB_EALG_3DESCBC */
1115 1.1 jonathan MAXIV(enc_xform_rijndael128); /* SADB_X_EALG_AES */
1116 1.1 jonathan MAXIV(enc_xform_blf); /* SADB_X_EALG_BLOWFISHCBC */
1117 1.1 jonathan MAXIV(enc_xform_cast5); /* SADB_X_EALG_CAST128CBC */
1118 1.1 jonathan MAXIV(enc_xform_skipjack); /* SADB_X_EALG_SKIPJACK */
1119 1.32 drochner MAXIV(enc_xform_camellia); /* SADB_X_EALG_CAMELLIACBC */
1120 1.36 drochner MAXIV(enc_xform_aes_ctr); /* SADB_X_EALG_AESCTR */
1121 1.1 jonathan MAXIV(enc_xform_null); /* SADB_EALG_NULL */
1122 1.1 jonathan
1123 1.1 jonathan xform_register(&esp_xformsw);
1124 1.1 jonathan #undef MAXIV
1125 1.1 jonathan }
1126