xform_ipcomp.c revision 1.42 1 /* $NetBSD: xform_ipcomp.c,v 1.42 2017/07/14 01:24:23 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.42 2017/07/14 01:24:23 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, 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 tc->tc_sav = sav;
209 KEY_SA_REF(sav);
210
211 return crypto_dispatch(crp);
212 }
213
214 #ifdef INET6
215 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
216 if (saidx->dst.sa.sa_family == AF_INET6) { \
217 error = ipsec6_common_input_cb(m, sav, skip, protoff); \
218 } else { \
219 error = ipsec4_common_input_cb(m, sav, skip, protoff); \
220 } \
221 } while (0)
222 #else
223 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
224 (error = ipsec4_common_input_cb(m, sav, skip, protoff))
225 #endif
226
227 /*
228 * IPComp input callback from the crypto driver.
229 */
230 static int
231 ipcomp_input_cb(struct cryptop *crp)
232 {
233 char buf[IPSEC_ADDRSTRLEN];
234 struct tdb_crypto *tc;
235 int skip, protoff;
236 struct mbuf *m;
237 struct secasvar *sav;
238 struct secasindex *saidx __diagused;
239 int s, hlen = IPCOMP_HLENGTH, error, clen;
240 uint8_t nproto;
241 void *addr;
242 uint16_t dport;
243 uint16_t sport;
244
245 KASSERT(crp->crp_opaque != NULL);
246 tc = crp->crp_opaque;
247 skip = tc->tc_skip;
248 protoff = tc->tc_protoff;
249 m = crp->crp_buf;
250
251 /* find the source port for NAT-T */
252 nat_t_ports_get(m, &dport, &sport);
253
254 s = splsoftnet();
255 mutex_enter(softnet_lock);
256
257 sav = tc->tc_sav;
258 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
259 KEY_FREESAV(&sav);
260 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
261 sport, dport);
262 if (sav == NULL) {
263 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
264 DPRINTF(("%s: SA expired while in crypto\n", __func__));
265 error = ENOBUFS; /*XXX*/
266 goto bad;
267 }
268 }
269
270 saidx = &sav->sah->saidx;
271 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
272 saidx->dst.sa.sa_family == AF_INET6,
273 "unexpected protocol family %u", saidx->dst.sa.sa_family);
274
275 /* Check for crypto errors */
276 if (crp->crp_etype) {
277 /* Reset the session ID */
278 if (sav->tdb_cryptoid != 0)
279 sav->tdb_cryptoid = crp->crp_sid;
280
281 if (crp->crp_etype == EAGAIN) {
282 KEY_FREESAV(&sav);
283 mutex_exit(softnet_lock);
284 splx(s);
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 /* Shouldn't happen... */
294 if (m == NULL) {
295 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
296 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
297 error = EINVAL;
298 goto bad;
299 }
300 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
301
302 /* Update the counters */
303 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
304
305
306 clen = crp->crp_olen; /* Length of data after processing */
307
308 /* Release the crypto descriptors */
309 free(tc, M_XDATA), tc = NULL;
310 crypto_freereq(crp), crp = NULL;
311
312 /* In case it's not done already, adjust the size of the mbuf chain */
313 m->m_pkthdr.len = clen + hlen + skip;
314
315 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
316 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/
317 DPRINTF(("%s: m_pullup failed\n", __func__));
318 error = EINVAL; /*XXX*/
319 goto bad;
320 }
321
322 /* Keep the next protocol field */
323 addr = (uint8_t*) mtod(m, struct ip *) + skip;
324 nproto = ((struct ipcomp *) addr)->comp_nxt;
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(uint8_t), (uint8_t *) &nproto);
351
352 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
353
354 KEY_FREESAV(&sav);
355 mutex_exit(softnet_lock);
356 splx(s);
357 return error;
358 bad:
359 if (sav)
360 KEY_FREESAV(&sav);
361 mutex_exit(softnet_lock);
362 splx(s);
363 if (m)
364 m_freem(m);
365 if (tc != NULL)
366 free(tc, M_XDATA);
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 struct ipsecrequest *isr,
379 struct mbuf **mp,
380 int skip,
381 int protoff
382 )
383 {
384 char buf[IPSEC_ADDRSTRLEN];
385 struct secasvar *sav;
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(isr->sav != NULL);
394 sav = isr->sav;
395 KASSERT(sav->tdb_compalgxform != NULL);
396 ipcompx = sav->tdb_compalgxform;
397
398 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
399
400 /* Don't process the packet if it is too short */
401 if (ralen < ipcompx->minlen) {
402 IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
403 return ipsec_process_done(m,isr);
404 }
405
406 hlen = IPCOMP_HLENGTH;
407
408 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
409
410 /* Check for maximum packet size violations. */
411 switch (sav->sah->saidx.dst.sa.sa_family) {
412 #ifdef INET
413 case AF_INET:
414 maxpacketsize = IP_MAXPACKET;
415 break;
416 #endif /* INET */
417 #ifdef INET6
418 case AF_INET6:
419 maxpacketsize = IPV6_MAXPACKET;
420 break;
421 #endif /* INET6 */
422 default:
423 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
424 DPRINTF(("%s: unknown/unsupported protocol family %d"
425 ", IPCA %s/%08lx\n", __func__,
426 sav->sah->saidx.dst.sa.sa_family,
427 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
428 (u_long) ntohl(sav->spi)));
429 error = EPFNOSUPPORT;
430 goto bad;
431 }
432 if (skip + hlen + ralen > maxpacketsize) {
433 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
434 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
435 "(len %u, max len %u)\n", __func__,
436 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
437 (u_long) ntohl(sav->spi),
438 skip + hlen + ralen, maxpacketsize));
439 error = EMSGSIZE;
440 goto bad;
441 }
442
443 /* Update the counters */
444 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
445
446 m = m_clone(m);
447 if (m == NULL) {
448 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
449 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
450 __func__,
451 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
452 (u_long) ntohl(sav->spi)));
453 error = ENOBUFS;
454 goto bad;
455 }
456
457 /* Ok now, we can pass to the crypto processing */
458
459 /* Get crypto descriptors */
460 crp = crypto_getreq(1);
461 if (crp == NULL) {
462 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
463 DPRINTF(("%s: failed to acquire crypto descriptor\n",
464 __func__));
465 error = ENOBUFS;
466 goto bad;
467 }
468 crdc = crp->crp_desc;
469
470 /* Compression descriptor */
471 crdc->crd_skip = skip;
472 crdc->crd_len = m->m_pkthdr.len - skip;
473 crdc->crd_flags = CRD_F_COMP;
474 crdc->crd_inject = skip;
475
476 /* Compression operation */
477 crdc->crd_alg = ipcompx->type;
478
479 /* IPsec-specific opaque crypto info */
480 tc = malloc(sizeof(*tc), M_XDATA, M_NOWAIT|M_ZERO);
481 if (tc == NULL) {
482 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
483 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
484 crypto_freereq(crp);
485 error = ENOBUFS;
486 goto bad;
487 }
488
489 tc->tc_isr = isr;
490 tc->tc_spi = sav->spi;
491 tc->tc_dst = sav->sah->saidx.dst;
492 tc->tc_proto = sav->sah->saidx.proto;
493 tc->tc_skip = skip;
494 tc->tc_protoff = protoff;
495 tc->tc_sav = sav;
496 KEY_SA_REF(sav);
497
498 /* Crypto operation descriptor */
499 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
500 crp->crp_flags = CRYPTO_F_IMBUF;
501 crp->crp_buf = m;
502 crp->crp_callback = ipcomp_output_cb;
503 crp->crp_opaque = tc;
504 crp->crp_sid = sav->tdb_cryptoid;
505
506 return crypto_dispatch(crp);
507 bad:
508 if (m)
509 m_freem(m);
510 return (error);
511 }
512
513 /*
514 * IPComp output callback from the crypto driver.
515 */
516 static int
517 ipcomp_output_cb(struct cryptop *crp)
518 {
519 char buf[IPSEC_ADDRSTRLEN];
520 struct tdb_crypto *tc;
521 struct ipsecrequest *isr;
522 struct secasvar *sav;
523 struct mbuf *m, *mo;
524 int s, error, skip, rlen, roff;
525 uint8_t prot;
526 uint16_t cpi;
527 struct ipcomp * ipcomp;
528
529 KASSERT(crp->crp_opaque != NULL);
530 tc = crp->crp_opaque;
531 m = crp->crp_buf;
532 skip = tc->tc_skip;
533 rlen = crp->crp_ilen - skip;
534
535 s = splsoftnet();
536 mutex_enter(softnet_lock);
537
538 isr = tc->tc_isr;
539 sav = tc->tc_sav;
540 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
541 KEY_FREESAV(&sav);
542 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
543 if (sav == NULL) {
544 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
545 DPRINTF(("%s: SA expired while in crypto\n", __func__));
546 error = ENOBUFS; /*XXX*/
547 goto bad;
548 }
549 }
550 KASSERTMSG(isr->sav == sav, "SA changed");
551
552 /* Check for crypto errors */
553 if (crp->crp_etype) {
554 /* Reset session ID */
555 if (sav->tdb_cryptoid != 0)
556 sav->tdb_cryptoid = crp->crp_sid;
557
558 if (crp->crp_etype == EAGAIN) {
559 KEY_FREESAV(&sav);
560 mutex_exit(softnet_lock);
561 splx(s);
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 /* Shouldn't happen... */
570 if (m == NULL) {
571 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
572 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
573 error = EINVAL;
574 goto bad;
575 }
576 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
577
578 if (rlen > crp->crp_olen) {
579 /* Inject IPCOMP header */
580 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
581 if (mo == NULL) {
582 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
583 DPRINTF(("%s: failed to inject IPCOMP header for "
584 "IPCA %s/%08lx\n", __func__,
585 ipsec_address(&sav->sah->saidx.dst, buf,
586 sizeof(buf)), (u_long) ntohl(sav->spi)));
587 error = ENOBUFS;
588 goto bad;
589 }
590 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
591
592 /* Initialize the IPCOMP header */
593 /* XXX alignment always correct? */
594 switch (sav->sah->saidx.dst.sa.sa_family) {
595 #ifdef INET
596 case AF_INET:
597 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
598 break;
599 #endif /* INET */
600 #ifdef INET6
601 case AF_INET6:
602 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
603 break;
604 #endif
605 }
606 ipcomp->comp_flags = 0;
607
608 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
609 cpi = sav->alg_enc;
610 else
611 cpi = ntohl(sav->spi) & 0xffff;
612 ipcomp->comp_cpi = htons(cpi);
613
614 /* Fix Next Protocol in IPv4/IPv6 header */
615 prot = IPPROTO_IPCOMP;
616 m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
617
618 /* Adjust the length in the IP header */
619 switch (sav->sah->saidx.dst.sa.sa_family) {
620 #ifdef INET
621 case AF_INET:
622 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
623 break;
624 #endif /* INET */
625 #ifdef INET6
626 case AF_INET6:
627 mtod(m, struct ip6_hdr *)->ip6_plen =
628 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
629 break;
630 #endif /* INET6 */
631 default:
632 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
633 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
634 "family %d, IPCA %s/%08lx\n",
635 sav->sah->saidx.dst.sa.sa_family,
636 ipsec_address(&sav->sah->saidx.dst, buf,
637 sizeof(buf)), (u_long) ntohl(sav->spi)));
638 error = EPFNOSUPPORT;
639 goto bad;
640 }
641 } else {
642 /* compression was useless, we have lost time */
643 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
644 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
645 "and compressed size is %d\n", rlen, crp->crp_olen));
646 }
647
648
649 /* Release the crypto descriptor */
650 free(tc, M_XDATA);
651 crypto_freereq(crp);
652
653 /* NB: m is reclaimed by ipsec_process_done. */
654 error = ipsec_process_done(m, isr);
655 KEY_FREESAV(&sav);
656 mutex_exit(softnet_lock);
657 splx(s);
658 return error;
659 bad:
660 if (sav)
661 KEY_FREESAV(&sav);
662 mutex_exit(softnet_lock);
663 splx(s);
664 if (m)
665 m_freem(m);
666 free(tc, M_XDATA);
667 crypto_freereq(crp);
668 return error;
669 }
670
671 static struct xformsw ipcomp_xformsw = {
672 .xf_type = XF_IPCOMP,
673 .xf_flags = XFT_COMP,
674 .xf_name = "IPcomp",
675 .xf_init = ipcomp_init,
676 .xf_zeroize = ipcomp_zeroize,
677 .xf_input = ipcomp_input,
678 .xf_output = ipcomp_output,
679 .xf_next = NULL,
680 };
681
682 void
683 ipcomp_attach(void)
684 {
685 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
686 xform_register(&ipcomp_xformsw);
687 }
688