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