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