xform_ipcomp.c revision 1.32 1 /* $NetBSD: xform_ipcomp.c,v 1.32 2017/04/06 09:20:07 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.32 2017/04/06 09:20:07 ozaki-r Exp $");
34
35 /* IP payload compression protocol (IPComp), see RFC 2393 */
36 #if defined(_KERNEL_OPT)
37 #include "opt_inet.h"
38 #ifdef __FreeBSD__
39 #include "opt_inet6.h"
40 #endif
41 #endif
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/kernel.h>
48 #include <sys/protosw.h>
49 #include <sys/sysctl.h>
50 #include <sys/socketvar.h> /* for softnet_lock */
51
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56
57 #include <net/route.h>
58 #include <netipsec/ipsec.h>
59 #include <netipsec/ipsec_private.h>
60 #include <netipsec/xform.h>
61
62 #ifdef INET6
63 #include <netinet/ip6.h>
64 #include <netipsec/ipsec6.h>
65 #endif
66
67 #include <netipsec/ipcomp.h>
68 #include <netipsec/ipcomp_var.h>
69
70 #include <netipsec/key.h>
71 #include <netipsec/key_debug.h>
72
73 #include <netipsec/ipsec_osdep.h>
74
75 #include <opencrypto/cryptodev.h>
76 #include <opencrypto/deflate.h>
77 #include <opencrypto/xform.h>
78
79 percpu_t *ipcompstat_percpu;
80
81 int ipcomp_enable = 1;
82
83 #ifdef __FreeBSD__
84 SYSCTL_DECL(_net_inet_ipcomp);
85 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
86 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
87 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
88 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
89 #endif /* __FreeBSD__ */
90
91 static int ipcomp_input_cb(struct cryptop *crp);
92 static int ipcomp_output_cb(struct cryptop *crp);
93
94 const struct comp_algo *
95 ipcomp_algorithm_lookup(int alg)
96 {
97 if (alg >= IPCOMP_ALG_MAX)
98 return NULL;
99 switch (alg) {
100 case SADB_X_CALG_DEFLATE:
101 return &comp_algo_deflate_nogrow;
102 }
103 return NULL;
104 }
105
106 /*
107 * ipcomp_init() is called when an CPI is being set up.
108 */
109 static int
110 ipcomp_init(struct secasvar *sav, const struct xformsw *xsp)
111 {
112 const struct comp_algo *tcomp;
113 struct cryptoini cric;
114 int ses;
115
116 /* NB: algorithm really comes in alg_enc and not alg_comp! */
117 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
118 if (tcomp == NULL) {
119 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
120 sav->alg_comp));
121 return EINVAL;
122 }
123 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
124 sav->tdb_xform = xsp;
125 sav->tdb_compalgxform = tcomp;
126
127 /* Initialize crypto session */
128 memset(&cric, 0, sizeof (cric));
129 cric.cri_alg = sav->tdb_compalgxform->type;
130
131 ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
132 return ses;
133 }
134
135 /*
136 * ipcomp_zeroize() used when IPCA is deleted
137 */
138 static int
139 ipcomp_zeroize(struct secasvar *sav)
140 {
141 int err;
142
143 err = crypto_freesession(sav->tdb_cryptoid);
144 sav->tdb_cryptoid = 0;
145 return err;
146 }
147
148 /*
149 * ipcomp_input() gets called to uncompress an input packet
150 */
151 static int
152 ipcomp_input(struct mbuf *m, const struct secasvar *sav, int skip, int protoff)
153 {
154 struct tdb_crypto *tc;
155 struct cryptodesc *crdc;
156 struct cryptop *crp;
157 int error, hlen = IPCOMP_HLENGTH;
158
159 IPSEC_SPLASSERT_SOFTNET("ipcomp_input");
160
161 /* Get crypto descriptors */
162 crp = crypto_getreq(1);
163 if (crp == NULL) {
164 m_freem(m);
165 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
166 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
167 return ENOBUFS;
168 }
169 /* Get IPsec-specific opaque pointer */
170 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
171 if (tc == NULL) {
172 m_freem(m);
173 crypto_freereq(crp);
174 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
175 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
176 return ENOBUFS;
177 }
178
179 error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
180 if (error) {
181 DPRINTF(("ipcomp_input: m_makewritable failed\n"));
182 m_freem(m);
183 free(tc, M_XDATA);
184 crypto_freereq(crp);
185 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
186 return error;
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 tc->tc_ptr = 0;
196
197 /* Decompression operation */
198 crdc->crd_alg = sav->tdb_compalgxform->type;
199
200 /* Crypto operation descriptor */
201 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
202 crp->crp_olen = MCLBYTES; /* hint to decompression code */
203 crp->crp_flags = CRYPTO_F_IMBUF;
204 crp->crp_buf = m;
205 crp->crp_callback = ipcomp_input_cb;
206 crp->crp_sid = sav->tdb_cryptoid;
207 crp->crp_opaque = tc;
208
209 /* These are passed as-is to the callback */
210 tc->tc_spi = sav->spi;
211 tc->tc_dst = sav->sah->saidx.dst;
212 tc->tc_proto = sav->sah->saidx.proto;
213 tc->tc_protoff = protoff;
214 tc->tc_skip = skip;
215
216 return crypto_dispatch(crp);
217 }
218
219 #ifdef INET6
220 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
221 if (saidx->dst.sa.sa_family == AF_INET6) { \
222 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
223 } else { \
224 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
225 } \
226 } while (0)
227 #else
228 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
229 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
230 #endif
231
232 /*
233 * IPComp input callback from the crypto driver.
234 */
235 static int
236 ipcomp_input_cb(struct cryptop *crp)
237 {
238 struct tdb_crypto *tc;
239 int skip, protoff;
240 struct mbuf *m;
241 struct secasvar *sav;
242 struct secasindex *saidx __diagused;
243 int s, hlen = IPCOMP_HLENGTH, error, clen;
244 u_int8_t nproto;
245 void *addr;
246 u_int16_t dport;
247 u_int16_t sport;
248
249 tc = (struct tdb_crypto *) crp->crp_opaque;
250 IPSEC_ASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
251 skip = tc->tc_skip;
252 protoff = tc->tc_protoff;
253 m = (struct mbuf *) crp->crp_buf;
254
255 /* find the source port for NAT-T */
256 nat_t_ports_get(m, &dport, &sport);
257
258 s = splsoftnet();
259 mutex_enter(softnet_lock);
260
261 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport);
262 if (sav == NULL) {
263 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
264 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
265 error = ENOBUFS; /*XXX*/
266 goto bad;
267 }
268
269 saidx = &sav->sah->saidx;
270 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
271 saidx->dst.sa.sa_family == AF_INET6,
272 ("ipcomp_input_cb: unexpected protocol family %u",
273 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(("ipcomp_input_cb: crypto error %d\n", 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(("ipcomp_input_cb: null mbuf returned from crypto\n"));
297 error = EINVAL;
298 goto bad;
299 }
300 IPCOMP_STATINC(IPCOMP_STAT_HIST + 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(("ipcomp_input_cb: m_pullup failed\n"));
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 if (nproto == IPPROTO_IPCOMP || nproto == IPPROTO_AH || nproto == IPPROTO_ESP) {
326 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
327 DPRINTF(("ipcomp_input_cb: nested ipcomp, IPCA %s/%08lx\n",
328 ipsec_address(&sav->sah->saidx.dst),
329 (u_long) ntohl(sav->spi)));
330 error = EINVAL;
331 goto bad;
332 }
333
334 /* Remove the IPCOMP header */
335 error = m_striphdr(m, skip, hlen);
336 if (error) {
337 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
338 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
339 ipsec_address(&sav->sah->saidx.dst),
340 (u_long) ntohl(sav->spi)));
341 goto bad;
342 }
343
344 /* Restore the Next Protocol field */
345 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
346
347 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
348
349 KEY_FREESAV(&sav);
350 mutex_exit(softnet_lock);
351 splx(s);
352 return error;
353 bad:
354 if (sav)
355 KEY_FREESAV(&sav);
356 mutex_exit(softnet_lock);
357 splx(s);
358 if (m)
359 m_freem(m);
360 if (tc != NULL)
361 free(tc, M_XDATA);
362 if (crp)
363 crypto_freereq(crp);
364 return error;
365 }
366
367 /*
368 * IPComp output routine, called by ipsec[46]_process_packet()
369 */
370 static int
371 ipcomp_output(
372 struct mbuf *m,
373 struct ipsecrequest *isr,
374 struct mbuf **mp,
375 int skip,
376 int protoff
377 )
378 {
379 const struct secasvar *sav;
380 const struct comp_algo *ipcompx;
381 int error, ralen, hlen, maxpacketsize;
382 struct cryptodesc *crdc;
383 struct cryptop *crp;
384 struct tdb_crypto *tc;
385
386 IPSEC_SPLASSERT_SOFTNET("ipcomp_output");
387 sav = isr->sav;
388 IPSEC_ASSERT(sav != NULL, ("ipcomp_output: null SA"));
389 ipcompx = sav->tdb_compalgxform;
390 IPSEC_ASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
391
392 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
393
394 /* Don't process the packet if it is too short */
395 if (ralen < ipcompx->minlen) {
396 IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
397 return ipsec_process_done(m,isr);
398 }
399
400 hlen = IPCOMP_HLENGTH;
401
402 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
403
404 /* Check for maximum packet size violations. */
405 switch (sav->sah->saidx.dst.sa.sa_family) {
406 #ifdef INET
407 case AF_INET:
408 maxpacketsize = IP_MAXPACKET;
409 break;
410 #endif /* INET */
411 #ifdef INET6
412 case AF_INET6:
413 maxpacketsize = IPV6_MAXPACKET;
414 break;
415 #endif /* INET6 */
416 default:
417 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
418 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
419 ", IPCA %s/%08lx\n",
420 sav->sah->saidx.dst.sa.sa_family,
421 ipsec_address(&sav->sah->saidx.dst),
422 (u_long) ntohl(sav->spi)));
423 error = EPFNOSUPPORT;
424 goto bad;
425 }
426 if (skip + hlen + ralen > maxpacketsize) {
427 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
428 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
429 "(len %u, max len %u)\n",
430 ipsec_address(&sav->sah->saidx.dst),
431 (u_long) ntohl(sav->spi),
432 skip + hlen + ralen, maxpacketsize));
433 error = EMSGSIZE;
434 goto bad;
435 }
436
437 /* Update the counters */
438 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
439
440 m = m_clone(m);
441 if (m == NULL) {
442 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
443 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
444 ipsec_address(&sav->sah->saidx.dst),
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(("ipcomp_output: failed to acquire crypto descriptor\n"));
457 error = ENOBUFS;
458 goto bad;
459 }
460 crdc = crp->crp_desc;
461
462 /* Compression descriptor */
463 crdc->crd_skip = skip;
464 crdc->crd_len = m->m_pkthdr.len - skip;
465 crdc->crd_flags = CRD_F_COMP;
466 crdc->crd_inject = skip;
467
468 /* Compression operation */
469 crdc->crd_alg = ipcompx->type;
470
471 /* IPsec-specific opaque crypto info */
472 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
473 M_XDATA, M_NOWAIT|M_ZERO);
474 if (tc == NULL) {
475 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
476 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
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 struct tdb_crypto *tc;
511 struct ipsecrequest *isr;
512 struct secasvar *sav;
513 struct mbuf *m, *mo;
514 int s, error, skip, rlen, roff;
515 u_int8_t prot;
516 u_int16_t cpi;
517 struct ipcomp * ipcomp;
518
519
520 tc = (struct tdb_crypto *) crp->crp_opaque;
521 IPSEC_ASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
522 m = (struct mbuf *) 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_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
531 if (sav == NULL) {
532 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
533 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
534 error = ENOBUFS; /*XXX*/
535 goto bad;
536 }
537 IPSEC_ASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
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(("ipcomp_output_cb: crypto error %d\n", 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(("ipcomp_output_cb: bogus return buffer from crypto\n"));
560 error = EINVAL;
561 goto bad;
562 }
563 IPCOMP_STATINC(IPCOMP_STAT_HIST + 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(("ipcomp_output: failed to inject IPCOMP header for "
571 "IPCA %s/%08lx\n",
572 ipsec_address(&sav->sah->saidx.dst),
573 (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(u_int8_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),
624 (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_IPCOMP, XFT_COMP, "IPcomp",
660 ipcomp_init, ipcomp_zeroize, ipcomp_input,
661 ipcomp_output,
662 NULL,
663 };
664
665 INITFN void
666 ipcomp_attach(void)
667 {
668 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
669 xform_register(&ipcomp_xformsw);
670 }
671
672 #ifdef __FreeBSD__
673 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)
674 #endif /* __FreeBSD__ */
675