xform_ipcomp.c revision 1.62 1 /* $NetBSD: xform_ipcomp.c,v 1.62 2018/04/19 08:27:39 maxv Exp $ */
2 /* $FreeBSD: xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $ */
3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
4
5 /*
6 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj (at) wabbitt.org)
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.62 2018/04/19 08:27:39 maxv Exp $");
34
35 /* IP payload compression protocol (IPComp), see RFC 2393 */
36 #if defined(_KERNEL_OPT)
37 #include "opt_inet.h"
38 #endif
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/protosw.h>
46 #include <sys/sysctl.h>
47 #include <sys/pool.h>
48 #include <sys/pserialize.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54
55 #include <net/route.h>
56 #include <netipsec/ipsec.h>
57 #include <netipsec/ipsec_private.h>
58 #include <netipsec/xform.h>
59
60 #ifdef INET6
61 #include <netinet/ip6.h>
62 #include <netipsec/ipsec6.h>
63 #endif
64
65 #include <netipsec/ipcomp.h>
66 #include <netipsec/ipcomp_var.h>
67
68 #include <netipsec/key.h>
69 #include <netipsec/key_debug.h>
70
71 #include <opencrypto/cryptodev.h>
72 #include <opencrypto/deflate.h>
73 #include <opencrypto/xform.h>
74
75 percpu_t *ipcompstat_percpu;
76
77 int ipcomp_enable = 1;
78
79 static int ipcomp_input_cb(struct cryptop *crp);
80 static int ipcomp_output_cb(struct cryptop *crp);
81
82 const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
83
84 static pool_cache_t ipcomp_tdb_crypto_pool_cache;
85
86 const struct comp_algo *
87 ipcomp_algorithm_lookup(int alg)
88 {
89 switch (alg) {
90 case SADB_X_CALG_DEFLATE:
91 return &comp_algo_deflate_nogrow;
92 }
93 return NULL;
94 }
95
96 /*
97 * ipcomp_init() is called when an CPI is being set up.
98 */
99 static int
100 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
101 {
102 const struct comp_algo *tcomp;
103 struct cryptoini cric;
104 int ses;
105
106 /* NB: algorithm really comes in alg_enc and not alg_comp! */
107 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
108 if (tcomp == NULL) {
109 DPRINTF(("%s: unsupported compression algorithm %d\n",
110 __func__, sav->alg_comp));
111 return EINVAL;
112 }
113 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
114 sav->tdb_xform = xsp;
115 sav->tdb_compalgxform = tcomp;
116
117 /* Initialize crypto session */
118 memset(&cric, 0, sizeof(cric));
119 cric.cri_alg = sav->tdb_compalgxform->type;
120
121 ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
122 return ses;
123 }
124
125 /*
126 * ipcomp_zeroize() used when IPCA is deleted
127 */
128 static int
129 ipcomp_zeroize(struct secasvar *sav)
130 {
131 int err;
132
133 err = crypto_freesession(sav->tdb_cryptoid);
134 sav->tdb_cryptoid = 0;
135 return err;
136 }
137
138 /*
139 * ipcomp_input() gets called to uncompress an input packet
140 */
141 static int
142 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
143 {
144 struct tdb_crypto *tc;
145 struct cryptodesc *crdc;
146 struct cryptop *crp;
147 int error, hlen = IPCOMP_HLENGTH, stat = IPCOMP_STAT_CRYPTO;
148
149 IPSEC_SPLASSERT_SOFTNET(__func__);
150 KASSERT(skip + hlen <= m->m_pkthdr.len);
151
152 /* Get crypto descriptors */
153 crp = crypto_getreq(1);
154 if (crp == NULL) {
155 DPRINTF(("%s: no crypto descriptors\n", __func__));
156 error = ENOBUFS;
157 goto error_m;
158 }
159 /* Get IPsec-specific opaque pointer */
160 tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
161 if (tc == NULL) {
162 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
163 error = ENOBUFS;
164 goto error_crp;
165 }
166
167 error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
168 if (error) {
169 DPRINTF(("%s: m_makewritable failed\n", __func__));
170 goto error_tc;
171 }
172
173 {
174 int s = pserialize_read_enter();
175
176 /*
177 * Take another reference to the SA for opencrypto callback.
178 */
179 if (__predict_false(sav->state == SADB_SASTATE_DEAD)) {
180 pserialize_read_exit(s);
181 stat = IPCOMP_STAT_NOTDB;
182 error = ENOENT;
183 goto error_tc;
184 }
185 KEY_SA_REF(sav);
186 pserialize_read_exit(s);
187 }
188
189 crdc = crp->crp_desc;
190
191 crdc->crd_skip = skip + hlen;
192 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
193 crdc->crd_inject = 0; /* unused */
194
195 /* Decompression operation */
196 crdc->crd_alg = sav->tdb_compalgxform->type;
197
198 /* Crypto operation descriptor */
199 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
200 crp->crp_olen = MCLBYTES; /* hint to decompression code */
201 crp->crp_flags = CRYPTO_F_IMBUF;
202 crp->crp_buf = m;
203 crp->crp_callback = ipcomp_input_cb;
204 crp->crp_sid = sav->tdb_cryptoid;
205 crp->crp_opaque = tc;
206
207 /* These are passed as-is to the callback */
208 tc->tc_spi = sav->spi;
209 tc->tc_dst = sav->sah->saidx.dst;
210 tc->tc_proto = sav->sah->saidx.proto;
211 tc->tc_protoff = protoff;
212 tc->tc_skip = skip;
213 tc->tc_sav = sav;
214
215 return crypto_dispatch(crp);
216
217 error_tc:
218 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
219 error_crp:
220 crypto_freereq(crp);
221 error_m:
222 m_freem(m);
223 IPCOMP_STATINC(stat);
224 return error;
225 }
226
227 #ifdef INET6
228 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
229 if (saidx->dst.sa.sa_family == AF_INET6) { \
230 error = ipsec6_common_input_cb(m, sav, skip, protoff); \
231 } else { \
232 error = ipsec4_common_input_cb(m, sav, skip, protoff); \
233 } \
234 } while (0)
235 #else
236 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
237 (error = ipsec4_common_input_cb(m, sav, skip, protoff))
238 #endif
239
240 /*
241 * IPComp input callback from the crypto driver.
242 */
243 static int
244 ipcomp_input_cb(struct cryptop *crp)
245 {
246 char buf[IPSEC_ADDRSTRLEN];
247 struct tdb_crypto *tc;
248 int skip, protoff;
249 struct mbuf *m;
250 struct secasvar *sav;
251 struct secasindex *saidx __diagused;
252 int hlen = IPCOMP_HLENGTH, error, clen;
253 uint8_t nproto;
254 struct ipcomp *ipc;
255 uint16_t dport;
256 uint16_t sport;
257 IPSEC_DECLARE_LOCK_VARIABLE;
258
259 KASSERT(crp->crp_opaque != NULL);
260 tc = crp->crp_opaque;
261 skip = tc->tc_skip;
262 protoff = tc->tc_protoff;
263 m = crp->crp_buf;
264
265 /* find the source port for NAT-T */
266 nat_t_ports_get(m, &dport, &sport);
267
268 IPSEC_ACQUIRE_GLOBAL_LOCKS();
269
270 sav = tc->tc_sav;
271 saidx = &sav->sah->saidx;
272 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
273 saidx->dst.sa.sa_family == AF_INET6,
274 "unexpected protocol family %u", saidx->dst.sa.sa_family);
275
276 /* Check for crypto errors */
277 if (crp->crp_etype) {
278 /* Reset the session ID */
279 if (sav->tdb_cryptoid != 0)
280 sav->tdb_cryptoid = crp->crp_sid;
281
282 if (crp->crp_etype == EAGAIN) {
283 KEY_SA_UNREF(&sav);
284 IPSEC_RELEASE_GLOBAL_LOCKS();
285 return crypto_dispatch(crp);
286 }
287
288 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
289 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
290 error = crp->crp_etype;
291 goto bad;
292 }
293
294 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
295
296 /* Update the counters */
297 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
298
299 /* Length of data after processing */
300 clen = crp->crp_olen;
301
302 /* Release the crypto descriptors */
303 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
304 tc = NULL;
305 crypto_freereq(crp);
306 crp = NULL;
307
308 /* In case it's not done already, adjust the size of the mbuf chain */
309 m->m_pkthdr.len = clen + hlen + skip;
310
311 /*
312 * Get the next protocol field.
313 *
314 * XXX: Really, we should use m_copydata instead of m_pullup.
315 */
316 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
317 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
318 DPRINTF(("%s: m_pullup failed\n", __func__));
319 error = EINVAL;
320 goto bad;
321 }
322 ipc = (struct ipcomp *)(mtod(m, uint8_t *) + skip);
323 nproto = ipc->comp_nxt;
324
325 switch (nproto) {
326 case IPPROTO_IPCOMP:
327 case IPPROTO_AH:
328 case IPPROTO_ESP:
329 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
330 DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
331 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
332 (u_long) ntohl(sav->spi)));
333 error = EINVAL;
334 goto bad;
335 default:
336 break;
337 }
338
339 /* Remove the IPCOMP header */
340 error = m_striphdr(m, skip, hlen);
341 if (error) {
342 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
343 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
344 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
345 (u_long) ntohl(sav->spi)));
346 goto bad;
347 }
348
349 /* Restore the Next Protocol field */
350 m_copyback(m, protoff, sizeof(nproto), &nproto);
351
352 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
353
354 KEY_SA_UNREF(&sav);
355 IPSEC_RELEASE_GLOBAL_LOCKS();
356 return error;
357
358 bad:
359 if (sav)
360 KEY_SA_UNREF(&sav);
361 IPSEC_RELEASE_GLOBAL_LOCKS();
362 if (m)
363 m_freem(m);
364 if (tc != NULL)
365 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
366 if (crp)
367 crypto_freereq(crp);
368 return error;
369 }
370
371 /*
372 * IPComp output routine, called by ipsec[46]_process_packet()
373 */
374 static int
375 ipcomp_output(struct mbuf *m, const struct ipsecrequest *isr,
376 struct secasvar *sav, struct mbuf **mp, int skip, int protoff)
377 {
378 char buf[IPSEC_ADDRSTRLEN];
379 const struct comp_algo *ipcompx;
380 int error, ralen, hlen, maxpacketsize;
381 struct cryptodesc *crdc;
382 struct cryptop *crp;
383 struct tdb_crypto *tc;
384
385 IPSEC_SPLASSERT_SOFTNET(__func__);
386 KASSERT(sav != NULL);
387 KASSERT(sav->tdb_compalgxform != NULL);
388 ipcompx = sav->tdb_compalgxform;
389
390 /* Raw payload length before comp. */
391 ralen = m->m_pkthdr.len - skip;
392
393 /* Don't process the packet if it is too short */
394 if (ralen < ipcompx->minlen) {
395 IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
396 return ipsec_process_done(m, isr, sav);
397 }
398
399 hlen = IPCOMP_HLENGTH;
400
401 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
402
403 /* Check for maximum packet size violations. */
404 switch (sav->sah->saidx.dst.sa.sa_family) {
405 #ifdef INET
406 case AF_INET:
407 maxpacketsize = IP_MAXPACKET;
408 break;
409 #endif
410 #ifdef INET6
411 case AF_INET6:
412 maxpacketsize = IPV6_MAXPACKET;
413 break;
414 #endif
415 default:
416 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
417 DPRINTF(("%s: unknown/unsupported protocol family %d"
418 ", IPCA %s/%08lx\n", __func__,
419 sav->sah->saidx.dst.sa.sa_family,
420 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
421 (u_long) ntohl(sav->spi)));
422 error = EPFNOSUPPORT;
423 goto bad;
424 }
425 if (skip + hlen + ralen > maxpacketsize) {
426 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
427 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
428 "(len %u, max len %u)\n", __func__,
429 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
430 (u_long) ntohl(sav->spi),
431 skip + hlen + ralen, maxpacketsize));
432 error = EMSGSIZE;
433 goto bad;
434 }
435
436 /* Update the counters */
437 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
438
439 m = m_clone(m);
440 if (m == NULL) {
441 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
442 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
443 __func__,
444 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
445 (u_long) ntohl(sav->spi)));
446 error = ENOBUFS;
447 goto bad;
448 }
449
450 /* Ok now, we can pass to the crypto processing */
451
452 /* Get crypto descriptors */
453 crp = crypto_getreq(1);
454 if (crp == NULL) {
455 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
456 DPRINTF(("%s: failed to acquire crypto descriptor\n",
457 __func__));
458 error = ENOBUFS;
459 goto bad;
460 }
461 crdc = crp->crp_desc;
462
463 /* Compression descriptor */
464 crdc->crd_skip = skip;
465 crdc->crd_len = m->m_pkthdr.len - skip;
466 crdc->crd_flags = CRD_F_COMP;
467 crdc->crd_inject = skip;
468
469 /* Compression operation */
470 crdc->crd_alg = ipcompx->type;
471
472 /* IPsec-specific opaque crypto info */
473 tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
474 if (tc == NULL) {
475 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
476 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
477 crypto_freereq(crp);
478 error = ENOBUFS;
479 goto bad;
480 }
481
482 {
483 int s = pserialize_read_enter();
484
485 /*
486 * Take another reference to the SP and the SA for opencrypto callback.
487 */
488 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
489 sav->state == SADB_SASTATE_DEAD)) {
490 pserialize_read_exit(s);
491 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
492 crypto_freereq(crp);
493 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
494 error = ENOENT;
495 goto bad;
496 }
497 KEY_SP_REF(isr->sp);
498 KEY_SA_REF(sav);
499 pserialize_read_exit(s);
500 }
501
502 tc->tc_isr = isr;
503 tc->tc_spi = sav->spi;
504 tc->tc_dst = sav->sah->saidx.dst;
505 tc->tc_proto = sav->sah->saidx.proto;
506 tc->tc_skip = skip;
507 tc->tc_protoff = protoff;
508 tc->tc_sav = sav;
509
510 /* Crypto operation descriptor */
511 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
512 crp->crp_flags = CRYPTO_F_IMBUF;
513 crp->crp_buf = m;
514 crp->crp_callback = ipcomp_output_cb;
515 crp->crp_opaque = tc;
516 crp->crp_sid = sav->tdb_cryptoid;
517
518 return crypto_dispatch(crp);
519
520 bad:
521 if (m)
522 m_freem(m);
523 return error;
524 }
525
526 /*
527 * IPComp output callback from the crypto driver.
528 */
529 static int
530 ipcomp_output_cb(struct cryptop *crp)
531 {
532 char buf[IPSEC_ADDRSTRLEN];
533 struct tdb_crypto *tc;
534 const struct ipsecrequest *isr;
535 struct secasvar *sav;
536 struct mbuf *m, *mo;
537 int error, skip, rlen, roff;
538 uint8_t prot;
539 uint16_t cpi;
540 struct ipcomp * ipcomp;
541 IPSEC_DECLARE_LOCK_VARIABLE;
542
543 KASSERT(crp->crp_opaque != NULL);
544 tc = crp->crp_opaque;
545 m = crp->crp_buf;
546 skip = tc->tc_skip;
547 rlen = crp->crp_ilen - skip;
548
549 IPSEC_ACQUIRE_GLOBAL_LOCKS();
550
551 isr = tc->tc_isr;
552 sav = tc->tc_sav;
553
554 /* Check for crypto errors */
555 if (crp->crp_etype) {
556 /* Reset session ID */
557 if (sav->tdb_cryptoid != 0)
558 sav->tdb_cryptoid = crp->crp_sid;
559
560 if (crp->crp_etype == EAGAIN) {
561 IPSEC_RELEASE_GLOBAL_LOCKS();
562 return crypto_dispatch(crp);
563 }
564 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
565 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
566 error = crp->crp_etype;
567 goto bad;
568 }
569
570 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
571
572 if (rlen > crp->crp_olen) {
573 /* Inject IPCOMP header */
574 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
575 if (mo == NULL) {
576 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
577 DPRINTF(("%s: failed to inject IPCOMP header for "
578 "IPCA %s/%08lx\n", __func__,
579 ipsec_address(&sav->sah->saidx.dst, buf,
580 sizeof(buf)), (u_long) ntohl(sav->spi)));
581 error = ENOBUFS;
582 goto bad;
583 }
584 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
585
586 /* Initialize the IPCOMP header */
587 /* XXX alignment always correct? */
588 switch (sav->sah->saidx.dst.sa.sa_family) {
589 #ifdef INET
590 case AF_INET:
591 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
592 break;
593 #endif
594 #ifdef INET6
595 case AF_INET6:
596 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
597 break;
598 #endif
599 }
600 ipcomp->comp_flags = 0;
601
602 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
603 cpi = sav->alg_enc;
604 else
605 cpi = ntohl(sav->spi) & 0xffff;
606 ipcomp->comp_cpi = htons(cpi);
607
608 /* Fix Next Protocol in IPv4/IPv6 header */
609 prot = IPPROTO_IPCOMP;
610 m_copyback(m, tc->tc_protoff, sizeof(prot), &prot);
611
612 /* Adjust the length in the IP header */
613 switch (sav->sah->saidx.dst.sa.sa_family) {
614 #ifdef INET
615 case AF_INET:
616 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
617 break;
618 #endif
619 #ifdef INET6
620 case AF_INET6:
621 mtod(m, struct ip6_hdr *)->ip6_plen =
622 htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
623 break;
624 #endif
625 default:
626 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
627 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
628 "family %d, IPCA %s/%08lx\n",
629 sav->sah->saidx.dst.sa.sa_family,
630 ipsec_address(&sav->sah->saidx.dst, buf,
631 sizeof(buf)), (u_long) ntohl(sav->spi)));
632 error = EPFNOSUPPORT;
633 goto bad;
634 }
635 } else {
636 /* compression was useless, we have lost time */
637 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
638 DPRINTF(("ipcomp_output_cb: compression was useless: initial"
639 "size was %d and compressed size is %d\n", rlen,
640 crp->crp_olen));
641 }
642
643 /* Release the crypto descriptor */
644 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
645 crypto_freereq(crp);
646
647 /* NB: m is reclaimed by ipsec_process_done. */
648 error = ipsec_process_done(m, isr, sav);
649 KEY_SA_UNREF(&sav);
650 KEY_SP_UNREF(&isr->sp);
651 IPSEC_RELEASE_GLOBAL_LOCKS();
652 return error;
653
654 bad:
655 if (sav)
656 KEY_SA_UNREF(&sav);
657 KEY_SP_UNREF(&isr->sp);
658 IPSEC_RELEASE_GLOBAL_LOCKS();
659 if (m)
660 m_freem(m);
661 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
662 crypto_freereq(crp);
663 return error;
664 }
665
666 static struct xformsw ipcomp_xformsw = {
667 .xf_type = XF_IPCOMP,
668 .xf_flags = XFT_COMP,
669 .xf_name = "IPcomp",
670 .xf_init = ipcomp_init,
671 .xf_zeroize = ipcomp_zeroize,
672 .xf_input = ipcomp_input,
673 .xf_output = ipcomp_output,
674 .xf_next = NULL,
675 };
676
677 void
678 ipcomp_attach(void)
679 {
680 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
681 ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
682 coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
683 NULL, NULL, NULL);
684 xform_register(&ipcomp_xformsw);
685 }
686