xform_ipcomp.c revision 1.35 1 /* $NetBSD: xform_ipcomp.c,v 1.35 2017/04/18 05:25:32 ozaki-r Exp $ */
2 /* $FreeBSD: src/sys/netipsec/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.35 2017/04/18 05:25:32 ozaki-r 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/socketvar.h> /* for softnet_lock */
48
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53
54 #include <net/route.h>
55 #include <netipsec/ipsec.h>
56 #include <netipsec/ipsec_private.h>
57 #include <netipsec/xform.h>
58
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #include <netipsec/ipsec6.h>
62 #endif
63
64 #include <netipsec/ipcomp.h>
65 #include <netipsec/ipcomp_var.h>
66
67 #include <netipsec/key.h>
68 #include <netipsec/key_debug.h>
69
70 #include <netipsec/ipsec_osdep.h>
71
72 #include <opencrypto/cryptodev.h>
73 #include <opencrypto/deflate.h>
74 #include <opencrypto/xform.h>
75
76 percpu_t *ipcompstat_percpu;
77
78 int ipcomp_enable = 1;
79
80 #ifdef __FreeBSD__
81 SYSCTL_DECL(_net_inet_ipcomp);
82 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
83 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
84 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
85 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
86 #endif /* __FreeBSD__ */
87
88 static int ipcomp_input_cb(struct cryptop *crp);
89 static int ipcomp_output_cb(struct cryptop *crp);
90
91 const uint8_t ipcomp_stats[256] = { SADB_CALG_STATS_INIT };
92
93 const struct comp_algo *
94 ipcomp_algorithm_lookup(int alg)
95 {
96 switch (alg) {
97 case SADB_X_CALG_DEFLATE:
98 return &comp_algo_deflate_nogrow;
99 }
100 return NULL;
101 }
102
103 /*
104 * ipcomp_init() is called when an CPI is being set up.
105 */
106 static int
107 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
108 {
109 const struct comp_algo *tcomp;
110 struct cryptoini cric;
111 int ses;
112
113 /* NB: algorithm really comes in alg_enc and not alg_comp! */
114 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
115 if (tcomp == NULL) {
116 DPRINTF(("%s: unsupported compression algorithm %d\n",
117 __func__, sav->alg_comp));
118 return EINVAL;
119 }
120 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
121 sav->tdb_xform = xsp;
122 sav->tdb_compalgxform = tcomp;
123
124 /* Initialize crypto session */
125 memset(&cric, 0, sizeof(cric));
126 cric.cri_alg = sav->tdb_compalgxform->type;
127
128 ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
129 return ses;
130 }
131
132 /*
133 * ipcomp_zeroize() used when IPCA is deleted
134 */
135 static int
136 ipcomp_zeroize(struct secasvar *sav)
137 {
138 int err;
139
140 err = crypto_freesession(sav->tdb_cryptoid);
141 sav->tdb_cryptoid = 0;
142 return err;
143 }
144
145 /*
146 * ipcomp_input() gets called to uncompress an input packet
147 */
148 static int
149 ipcomp_input(struct mbuf *m, const struct secasvar *sav, int skip, int protoff)
150 {
151 struct tdb_crypto *tc;
152 struct cryptodesc *crdc;
153 struct cryptop *crp;
154 int error, hlen = IPCOMP_HLENGTH;
155
156 IPSEC_SPLASSERT_SOFTNET(__func__);
157
158 /* Get crypto descriptors */
159 crp = crypto_getreq(1);
160 if (crp == NULL) {
161 m_freem(m);
162 DPRINTF(("%s: no crypto descriptors\n", __func__));
163 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
164 return ENOBUFS;
165 }
166 /* Get IPsec-specific opaque pointer */
167 tc = malloc(sizeof(*tc), M_XDATA, M_NOWAIT|M_ZERO);
168 if (tc == NULL) {
169 m_freem(m);
170 crypto_freereq(crp);
171 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
172 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
173 return ENOBUFS;
174 }
175
176 error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
177 if (error) {
178 DPRINTF(("%s: m_makewritable failed\n", __func__));
179 m_freem(m);
180 free(tc, M_XDATA);
181 crypto_freereq(crp);
182 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
183 return error;
184 }
185
186 crdc = crp->crp_desc;
187
188 crdc->crd_skip = skip + hlen;
189 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
190 crdc->crd_inject = 0; /* unused */
191
192 tc->tc_ptr = 0;
193
194 /* Decompression operation */
195 crdc->crd_alg = sav->tdb_compalgxform->type;
196
197 /* Crypto operation descriptor */
198 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
199 crp->crp_olen = MCLBYTES; /* hint to decompression code */
200 crp->crp_flags = CRYPTO_F_IMBUF;
201 crp->crp_buf = m;
202 crp->crp_callback = ipcomp_input_cb;
203 crp->crp_sid = sav->tdb_cryptoid;
204 crp->crp_opaque = tc;
205
206 /* These are passed as-is to the callback */
207 tc->tc_spi = sav->spi;
208 tc->tc_dst = sav->sah->saidx.dst;
209 tc->tc_proto = sav->sah->saidx.proto;
210 tc->tc_protoff = protoff;
211 tc->tc_skip = skip;
212
213 return crypto_dispatch(crp);
214 }
215
216 #ifdef INET6
217 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
218 if (saidx->dst.sa.sa_family == AF_INET6) { \
219 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
220 } else { \
221 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
222 } \
223 } while (0)
224 #else
225 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
226 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
227 #endif
228
229 /*
230 * IPComp input callback from the crypto driver.
231 */
232 static int
233 ipcomp_input_cb(struct cryptop *crp)
234 {
235 struct tdb_crypto *tc;
236 int skip, protoff;
237 struct mbuf *m;
238 struct secasvar *sav;
239 struct secasindex *saidx __diagused;
240 int s, hlen = IPCOMP_HLENGTH, error, clen;
241 uint8_t nproto;
242 void *addr;
243 uint16_t dport;
244 uint16_t sport;
245
246 tc = crp->crp_opaque;
247 IPSEC_ASSERT(tc != NULL, ("%s: null opaque crypto data area!",
248 __func__));
249 skip = tc->tc_skip;
250 protoff = tc->tc_protoff;
251 m = crp->crp_buf;
252
253 /* find the source port for NAT-T */
254 nat_t_ports_get(m, &dport, &sport);
255
256 s = splsoftnet();
257 mutex_enter(softnet_lock);
258
259 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport);
260 if (sav == NULL) {
261 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
262 DPRINTF(("%s: SA expired while in crypto\n", __func__));
263 error = ENOBUFS; /*XXX*/
264 goto bad;
265 }
266
267 saidx = &sav->sah->saidx;
268 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
269 saidx->dst.sa.sa_family == AF_INET6,
270 ("%s: unexpected protocol family %u", __func__,
271 saidx->dst.sa.sa_family));
272
273 /* Check for crypto errors */
274 if (crp->crp_etype) {
275 /* Reset the session ID */
276 if (sav->tdb_cryptoid != 0)
277 sav->tdb_cryptoid = crp->crp_sid;
278
279 if (crp->crp_etype == EAGAIN) {
280 KEY_FREESAV(&sav);
281 mutex_exit(softnet_lock);
282 splx(s);
283 return crypto_dispatch(crp);
284 }
285
286 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
287 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
288 error = crp->crp_etype;
289 goto bad;
290 }
291 /* Shouldn't happen... */
292 if (m == NULL) {
293 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
294 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
295 error = EINVAL;
296 goto bad;
297 }
298 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
299
300 /* Update the counters */
301 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
302
303
304 clen = crp->crp_olen; /* Length of data after processing */
305
306 /* Release the crypto descriptors */
307 free(tc, M_XDATA), tc = NULL;
308 crypto_freereq(crp), crp = NULL;
309
310 /* In case it's not done already, adjust the size of the mbuf chain */
311 m->m_pkthdr.len = clen + hlen + skip;
312
313 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
314 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/
315 DPRINTF(("%s: m_pullup failed\n", __func__));
316 error = EINVAL; /*XXX*/
317 goto bad;
318 }
319
320 /* Keep the next protocol field */
321 addr = (uint8_t*) mtod(m, struct ip *) + skip;
322 nproto = ((struct ipcomp *) addr)->comp_nxt;
323 switch (nproto) {
324 case IPPROTO_IPCOMP:
325 case IPPROTO_AH:
326 case IPPROTO_ESP:
327 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
328 DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
329 ipsec_address(&sav->sah->saidx.dst),
330 (u_long) ntohl(sav->spi)));
331 error = EINVAL;
332 goto bad;
333 default:
334 break;
335 }
336
337 /* Remove the IPCOMP header */
338 error = m_striphdr(m, skip, hlen);
339 if (error) {
340 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
341 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
342 ipsec_address(&sav->sah->saidx.dst),
343 (u_long) ntohl(sav->spi)));
344 goto bad;
345 }
346
347 /* Restore the Next Protocol field */
348 m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
349
350 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
351
352 KEY_FREESAV(&sav);
353 mutex_exit(softnet_lock);
354 splx(s);
355 return error;
356 bad:
357 if (sav)
358 KEY_FREESAV(&sav);
359 mutex_exit(softnet_lock);
360 splx(s);
361 if (m)
362 m_freem(m);
363 if (tc != NULL)
364 free(tc, M_XDATA);
365 if (crp)
366 crypto_freereq(crp);
367 return error;
368 }
369
370 /*
371 * IPComp output routine, called by ipsec[46]_process_packet()
372 */
373 static int
374 ipcomp_output(
375 struct mbuf *m,
376 struct ipsecrequest *isr,
377 struct mbuf **mp,
378 int skip,
379 int protoff
380 )
381 {
382 const struct secasvar *sav;
383 const struct comp_algo *ipcompx;
384 int error, ralen, hlen, maxpacketsize;
385 struct cryptodesc *crdc;
386 struct cryptop *crp;
387 struct tdb_crypto *tc;
388
389 IPSEC_SPLASSERT_SOFTNET(__func__);
390 sav = isr->sav;
391 IPSEC_ASSERT(sav != NULL, ("%s: null SA", __func__));
392 ipcompx = sav->tdb_compalgxform;
393 IPSEC_ASSERT(ipcompx != NULL, ("%s: null compression xform", __func__));
394
395 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
396
397 /* Don't process the packet if it is too short */
398 if (ralen < ipcompx->minlen) {
399 IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
400 return ipsec_process_done(m,isr);
401 }
402
403 hlen = IPCOMP_HLENGTH;
404
405 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
406
407 /* Check for maximum packet size violations. */
408 switch (sav->sah->saidx.dst.sa.sa_family) {
409 #ifdef INET
410 case AF_INET:
411 maxpacketsize = IP_MAXPACKET;
412 break;
413 #endif /* INET */
414 #ifdef INET6
415 case AF_INET6:
416 maxpacketsize = IPV6_MAXPACKET;
417 break;
418 #endif /* INET6 */
419 default:
420 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
421 DPRINTF(("%s: unknown/unsupported protocol family %d"
422 ", IPCA %s/%08lx\n", __func__,
423 sav->sah->saidx.dst.sa.sa_family,
424 ipsec_address(&sav->sah->saidx.dst),
425 (u_long) ntohl(sav->spi)));
426 error = EPFNOSUPPORT;
427 goto bad;
428 }
429 if (skip + hlen + ralen > maxpacketsize) {
430 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
431 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
432 "(len %u, max len %u)\n", __func__,
433 ipsec_address(&sav->sah->saidx.dst),
434 (u_long) ntohl(sav->spi),
435 skip + hlen + ralen, maxpacketsize));
436 error = EMSGSIZE;
437 goto bad;
438 }
439
440 /* Update the counters */
441 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
442
443 m = m_clone(m);
444 if (m == NULL) {
445 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
446 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
447 __func__, ipsec_address(&sav->sah->saidx.dst),
448 (u_long) ntohl(sav->spi)));
449 error = ENOBUFS;
450 goto bad;
451 }
452
453 /* Ok now, we can pass to the crypto processing */
454
455 /* Get crypto descriptors */
456 crp = crypto_getreq(1);
457 if (crp == NULL) {
458 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
459 DPRINTF(("%s: failed to acquire crypto descriptor\n",
460 __func__));
461 error = ENOBUFS;
462 goto bad;
463 }
464 crdc = crp->crp_desc;
465
466 /* Compression descriptor */
467 crdc->crd_skip = skip;
468 crdc->crd_len = m->m_pkthdr.len - skip;
469 crdc->crd_flags = CRD_F_COMP;
470 crdc->crd_inject = skip;
471
472 /* Compression operation */
473 crdc->crd_alg = ipcompx->type;
474
475 /* IPsec-specific opaque crypto info */
476 tc = malloc(sizeof(*tc), M_XDATA, M_NOWAIT|M_ZERO);
477 if (tc == NULL) {
478 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
479 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
480 crypto_freereq(crp);
481 error = ENOBUFS;
482 goto bad;
483 }
484
485 tc->tc_isr = isr;
486 tc->tc_spi = sav->spi;
487 tc->tc_dst = sav->sah->saidx.dst;
488 tc->tc_proto = sav->sah->saidx.proto;
489 tc->tc_skip = skip;
490 tc->tc_protoff = protoff;
491
492 /* Crypto operation descriptor */
493 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
494 crp->crp_flags = CRYPTO_F_IMBUF;
495 crp->crp_buf = m;
496 crp->crp_callback = ipcomp_output_cb;
497 crp->crp_opaque = tc;
498 crp->crp_sid = sav->tdb_cryptoid;
499
500 return crypto_dispatch(crp);
501 bad:
502 if (m)
503 m_freem(m);
504 return (error);
505 }
506
507 /*
508 * IPComp output callback from the crypto driver.
509 */
510 static int
511 ipcomp_output_cb(struct cryptop *crp)
512 {
513 struct tdb_crypto *tc;
514 struct ipsecrequest *isr;
515 struct secasvar *sav;
516 struct mbuf *m, *mo;
517 int s, error, skip, rlen, roff;
518 uint8_t prot;
519 uint16_t cpi;
520 struct ipcomp * ipcomp;
521
522
523 tc = crp->crp_opaque;
524 IPSEC_ASSERT(tc != NULL, ("%s: null opaque data area!", __func__));
525 m = crp->crp_buf;
526 skip = tc->tc_skip;
527 rlen = crp->crp_ilen - skip;
528
529 s = splsoftnet();
530 mutex_enter(softnet_lock);
531
532 isr = tc->tc_isr;
533 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
534 if (sav == NULL) {
535 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
536 DPRINTF(("%s: SA expired while in crypto\n", __func__));
537 error = ENOBUFS; /*XXX*/
538 goto bad;
539 }
540 IPSEC_ASSERT(isr->sav == sav, ("%s: SA changed", __func__));
541
542 /* Check for crypto errors */
543 if (crp->crp_etype) {
544 /* Reset session ID */
545 if (sav->tdb_cryptoid != 0)
546 sav->tdb_cryptoid = crp->crp_sid;
547
548 if (crp->crp_etype == EAGAIN) {
549 KEY_FREESAV(&sav);
550 mutex_exit(softnet_lock);
551 splx(s);
552 return crypto_dispatch(crp);
553 }
554 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
555 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
556 error = crp->crp_etype;
557 goto bad;
558 }
559 /* Shouldn't happen... */
560 if (m == NULL) {
561 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
562 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
563 error = EINVAL;
564 goto bad;
565 }
566 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
567
568 if (rlen > crp->crp_olen) {
569 /* Inject IPCOMP header */
570 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
571 if (mo == NULL) {
572 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
573 DPRINTF(("%s: failed to inject IPCOMP header for "
574 "IPCA %s/%08lx\n", __func__,
575 ipsec_address(&sav->sah->saidx.dst),
576 (u_long) ntohl(sav->spi)));
577 error = ENOBUFS;
578 goto bad;
579 }
580 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
581
582 /* Initialize the IPCOMP header */
583 /* XXX alignment always correct? */
584 switch (sav->sah->saidx.dst.sa.sa_family) {
585 #ifdef INET
586 case AF_INET:
587 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
588 break;
589 #endif /* INET */
590 #ifdef INET6
591 case AF_INET6:
592 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
593 break;
594 #endif
595 }
596 ipcomp->comp_flags = 0;
597
598 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
599 cpi = sav->alg_enc;
600 else
601 cpi = ntohl(sav->spi) & 0xffff;
602 ipcomp->comp_cpi = htons(cpi);
603
604 /* Fix Next Protocol in IPv4/IPv6 header */
605 prot = IPPROTO_IPCOMP;
606 m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
607
608 /* Adjust the length in the IP header */
609 switch (sav->sah->saidx.dst.sa.sa_family) {
610 #ifdef INET
611 case AF_INET:
612 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
613 break;
614 #endif /* INET */
615 #ifdef INET6
616 case AF_INET6:
617 mtod(m, struct ip6_hdr *)->ip6_plen =
618 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
619 break;
620 #endif /* INET6 */
621 default:
622 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
623 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
624 "family %d, IPCA %s/%08lx\n",
625 sav->sah->saidx.dst.sa.sa_family,
626 ipsec_address(&sav->sah->saidx.dst),
627 (u_long) ntohl(sav->spi)));
628 error = EPFNOSUPPORT;
629 goto bad;
630 }
631 } else {
632 /* compression was useless, we have lost time */
633 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
634 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
635 "and compressed size is %d\n", rlen, crp->crp_olen));
636 }
637
638
639 /* Release the crypto descriptor */
640 free(tc, M_XDATA);
641 crypto_freereq(crp);
642
643 /* NB: m is reclaimed by ipsec_process_done. */
644 error = ipsec_process_done(m, isr);
645 KEY_FREESAV(&sav);
646 mutex_exit(softnet_lock);
647 splx(s);
648 return error;
649 bad:
650 if (sav)
651 KEY_FREESAV(&sav);
652 mutex_exit(softnet_lock);
653 splx(s);
654 if (m)
655 m_freem(m);
656 free(tc, M_XDATA);
657 crypto_freereq(crp);
658 return error;
659 }
660
661 static struct xformsw ipcomp_xformsw = {
662 XF_IPCOMP, XFT_COMP, "IPcomp",
663 ipcomp_init, ipcomp_zeroize, ipcomp_input,
664 ipcomp_output,
665 NULL,
666 };
667
668 INITFN void
669 ipcomp_attach(void)
670 {
671 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
672 xform_register(&ipcomp_xformsw);
673 }
674