xform_ipcomp.c revision 1.44 1 /* $NetBSD: xform_ipcomp.c,v 1.44 2017/07/19 09:03:08 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.44 2017/07/19 09:03:08 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 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 = malloc(sizeof(*tc), M_XDATA, M_NOWAIT|M_ZERO);
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 tc->tc_isr = isr;
489 tc->tc_spi = sav->spi;
490 tc->tc_dst = sav->sah->saidx.dst;
491 tc->tc_proto = sav->sah->saidx.proto;
492 tc->tc_skip = skip;
493 tc->tc_protoff = protoff;
494 tc->tc_sav = sav;
495 KEY_SA_REF(sav);
496
497 /* Crypto operation descriptor */
498 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
499 crp->crp_flags = CRYPTO_F_IMBUF;
500 crp->crp_buf = m;
501 crp->crp_callback = ipcomp_output_cb;
502 crp->crp_opaque = tc;
503 crp->crp_sid = sav->tdb_cryptoid;
504
505 return crypto_dispatch(crp);
506 bad:
507 if (m)
508 m_freem(m);
509 return (error);
510 }
511
512 /*
513 * IPComp output callback from the crypto driver.
514 */
515 static int
516 ipcomp_output_cb(struct cryptop *crp)
517 {
518 char buf[IPSEC_ADDRSTRLEN];
519 struct tdb_crypto *tc;
520 struct ipsecrequest *isr;
521 struct secasvar *sav;
522 struct mbuf *m, *mo;
523 int s, error, skip, rlen, roff;
524 uint8_t prot;
525 uint16_t cpi;
526 struct ipcomp * ipcomp;
527
528 KASSERT(crp->crp_opaque != NULL);
529 tc = crp->crp_opaque;
530 m = crp->crp_buf;
531 skip = tc->tc_skip;
532 rlen = crp->crp_ilen - skip;
533
534 s = splsoftnet();
535 mutex_enter(softnet_lock);
536
537 isr = tc->tc_isr;
538 sav = tc->tc_sav;
539 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
540 KEY_FREESAV(&sav);
541 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
542 if (sav == NULL) {
543 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
544 DPRINTF(("%s: SA expired while in crypto\n", __func__));
545 error = ENOBUFS; /*XXX*/
546 goto bad;
547 }
548 }
549
550 /* Check for crypto errors */
551 if (crp->crp_etype) {
552 /* Reset session ID */
553 if (sav->tdb_cryptoid != 0)
554 sav->tdb_cryptoid = crp->crp_sid;
555
556 if (crp->crp_etype == EAGAIN) {
557 mutex_exit(softnet_lock);
558 splx(s);
559 return crypto_dispatch(crp);
560 }
561 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
562 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
563 error = crp->crp_etype;
564 goto bad;
565 }
566 /* Shouldn't happen... */
567 if (m == NULL) {
568 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
569 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
570 error = EINVAL;
571 goto bad;
572 }
573 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
574
575 if (rlen > crp->crp_olen) {
576 /* Inject IPCOMP header */
577 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
578 if (mo == NULL) {
579 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
580 DPRINTF(("%s: failed to inject IPCOMP header for "
581 "IPCA %s/%08lx\n", __func__,
582 ipsec_address(&sav->sah->saidx.dst, buf,
583 sizeof(buf)), (u_long) ntohl(sav->spi)));
584 error = ENOBUFS;
585 goto bad;
586 }
587 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
588
589 /* Initialize the IPCOMP header */
590 /* XXX alignment always correct? */
591 switch (sav->sah->saidx.dst.sa.sa_family) {
592 #ifdef INET
593 case AF_INET:
594 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
595 break;
596 #endif /* INET */
597 #ifdef INET6
598 case AF_INET6:
599 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
600 break;
601 #endif
602 }
603 ipcomp->comp_flags = 0;
604
605 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
606 cpi = sav->alg_enc;
607 else
608 cpi = ntohl(sav->spi) & 0xffff;
609 ipcomp->comp_cpi = htons(cpi);
610
611 /* Fix Next Protocol in IPv4/IPv6 header */
612 prot = IPPROTO_IPCOMP;
613 m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
614
615 /* Adjust the length in the IP header */
616 switch (sav->sah->saidx.dst.sa.sa_family) {
617 #ifdef INET
618 case AF_INET:
619 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
620 break;
621 #endif /* INET */
622 #ifdef INET6
623 case AF_INET6:
624 mtod(m, struct ip6_hdr *)->ip6_plen =
625 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
626 break;
627 #endif /* INET6 */
628 default:
629 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
630 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
631 "family %d, IPCA %s/%08lx\n",
632 sav->sah->saidx.dst.sa.sa_family,
633 ipsec_address(&sav->sah->saidx.dst, buf,
634 sizeof(buf)), (u_long) ntohl(sav->spi)));
635 error = EPFNOSUPPORT;
636 goto bad;
637 }
638 } else {
639 /* compression was useless, we have lost time */
640 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
641 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
642 "and compressed size is %d\n", rlen, crp->crp_olen));
643 }
644
645
646 /* Release the crypto descriptor */
647 free(tc, M_XDATA);
648 crypto_freereq(crp);
649
650 /* NB: m is reclaimed by ipsec_process_done. */
651 error = ipsec_process_done(m, isr, sav);
652 KEY_FREESAV(&sav);
653 mutex_exit(softnet_lock);
654 splx(s);
655 return error;
656 bad:
657 if (sav)
658 KEY_FREESAV(&sav);
659 mutex_exit(softnet_lock);
660 splx(s);
661 if (m)
662 m_freem(m);
663 free(tc, M_XDATA);
664 crypto_freereq(crp);
665 return error;
666 }
667
668 static struct xformsw ipcomp_xformsw = {
669 .xf_type = XF_IPCOMP,
670 .xf_flags = XFT_COMP,
671 .xf_name = "IPcomp",
672 .xf_init = ipcomp_init,
673 .xf_zeroize = ipcomp_zeroize,
674 .xf_input = ipcomp_input,
675 .xf_output = ipcomp_output,
676 .xf_next = NULL,
677 };
678
679 void
680 ipcomp_attach(void)
681 {
682 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
683 xform_register(&ipcomp_xformsw);
684 }
685