xform_ipcomp.c revision 1.50 1 /* $NetBSD: xform_ipcomp.c,v 1.50 2017/08/03 06:32:51 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.50 2017/08/03 06:32:51 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 struct pool ipcomp_tdb_crypto_pool;
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;
156
157 IPSEC_SPLASSERT_SOFTNET(__func__);
158
159 /* Get crypto descriptors */
160 crp = crypto_getreq(1);
161 if (crp == NULL) {
162 m_freem(m);
163 DPRINTF(("%s: no crypto descriptors\n", __func__));
164 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
165 return ENOBUFS;
166 }
167 /* Get IPsec-specific opaque pointer */
168 tc = pool_get(&ipcomp_tdb_crypto_pool, PR_NOWAIT);
169 if (tc == NULL) {
170 m_freem(m);
171 crypto_freereq(crp);
172 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
173 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
174 return ENOBUFS;
175 }
176
177 error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT);
178 if (error) {
179 DPRINTF(("%s: m_makewritable failed\n", __func__));
180 m_freem(m);
181 pool_put(&ipcomp_tdb_crypto_pool, tc);
182 crypto_freereq(crp);
183 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
184 return error;
185 }
186
187 crdc = crp->crp_desc;
188
189 crdc->crd_skip = skip + hlen;
190 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
191 crdc->crd_inject = 0; /* unused */
192
193 /* Decompression operation */
194 crdc->crd_alg = sav->tdb_compalgxform->type;
195
196 /* Crypto operation descriptor */
197 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
198 crp->crp_olen = MCLBYTES; /* hint to decompression code */
199 crp->crp_flags = CRYPTO_F_IMBUF;
200 crp->crp_buf = m;
201 crp->crp_callback = ipcomp_input_cb;
202 crp->crp_sid = sav->tdb_cryptoid;
203 crp->crp_opaque = tc;
204
205 /* These are passed as-is to the callback */
206 tc->tc_spi = sav->spi;
207 tc->tc_dst = sav->sah->saidx.dst;
208 tc->tc_proto = sav->sah->saidx.proto;
209 tc->tc_protoff = protoff;
210 tc->tc_skip = skip;
211 tc->tc_sav = sav;
212 KEY_SA_REF(sav);
213
214 return crypto_dispatch(crp);
215 }
216
217 #ifdef INET6
218 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) do { \
219 if (saidx->dst.sa.sa_family == AF_INET6) { \
220 error = ipsec6_common_input_cb(m, sav, skip, protoff); \
221 } else { \
222 error = ipsec4_common_input_cb(m, sav, skip, protoff); \
223 } \
224 } while (0)
225 #else
226 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff) \
227 (error = ipsec4_common_input_cb(m, sav, skip, protoff))
228 #endif
229
230 /*
231 * IPComp input callback from the crypto driver.
232 */
233 static int
234 ipcomp_input_cb(struct cryptop *crp)
235 {
236 char buf[IPSEC_ADDRSTRLEN];
237 struct tdb_crypto *tc;
238 int skip, protoff;
239 struct mbuf *m;
240 struct secasvar *sav;
241 struct secasindex *saidx __diagused;
242 int hlen = IPCOMP_HLENGTH, error, clen;
243 uint8_t nproto;
244 void *addr;
245 uint16_t dport;
246 uint16_t sport;
247 IPSEC_DECLARE_LOCK_VARIABLE;
248
249 KASSERT(crp->crp_opaque != NULL);
250 tc = crp->crp_opaque;
251 skip = tc->tc_skip;
252 protoff = tc->tc_protoff;
253 m = crp->crp_buf;
254
255 /* find the source port for NAT-T */
256 nat_t_ports_get(m, &dport, &sport);
257
258 IPSEC_ACQUIRE_GLOBAL_LOCKS();
259
260 sav = tc->tc_sav;
261 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
262 KEY_SA_UNREF(&sav);
263 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
264 sport, dport);
265 if (sav == NULL) {
266 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
267 DPRINTF(("%s: SA expired while in crypto\n", __func__));
268 error = ENOBUFS; /*XXX*/
269 goto bad;
270 }
271 }
272
273 saidx = &sav->sah->saidx;
274 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
275 saidx->dst.sa.sa_family == AF_INET6,
276 "unexpected protocol family %u", saidx->dst.sa.sa_family);
277
278 /* Check for crypto errors */
279 if (crp->crp_etype) {
280 /* Reset the session ID */
281 if (sav->tdb_cryptoid != 0)
282 sav->tdb_cryptoid = crp->crp_sid;
283
284 if (crp->crp_etype == EAGAIN) {
285 KEY_SA_UNREF(&sav);
286 IPSEC_RELEASE_GLOBAL_LOCKS();
287 return crypto_dispatch(crp);
288 }
289
290 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
291 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
292 error = crp->crp_etype;
293 goto bad;
294 }
295
296 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
297
298 /* Update the counters */
299 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
300
301
302 clen = crp->crp_olen; /* Length of data after processing */
303
304 /* Release the crypto descriptors */
305 pool_put(&ipcomp_tdb_crypto_pool, tc);
306 tc = NULL;
307 crypto_freereq(crp), crp = NULL;
308
309 /* In case it's not done already, adjust the size of the mbuf chain */
310 m->m_pkthdr.len = clen + hlen + skip;
311
312 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
313 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/
314 DPRINTF(("%s: m_pullup failed\n", __func__));
315 error = EINVAL; /*XXX*/
316 goto bad;
317 }
318
319 /* Keep the next protocol field */
320 addr = (uint8_t*) mtod(m, struct ip *) + skip;
321 nproto = ((struct ipcomp *) addr)->comp_nxt;
322 switch (nproto) {
323 case IPPROTO_IPCOMP:
324 case IPPROTO_AH:
325 case IPPROTO_ESP:
326 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
327 DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
328 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
329 (u_long) ntohl(sav->spi)));
330 error = EINVAL;
331 goto bad;
332 default:
333 break;
334 }
335
336 /* Remove the IPCOMP header */
337 error = m_striphdr(m, skip, hlen);
338 if (error) {
339 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
340 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
341 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
342 (u_long) ntohl(sav->spi)));
343 goto bad;
344 }
345
346 /* Restore the Next Protocol field */
347 m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
348
349 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
350
351 KEY_SA_UNREF(&sav);
352 IPSEC_RELEASE_GLOBAL_LOCKS();
353 return error;
354 bad:
355 if (sav)
356 KEY_SA_UNREF(&sav);
357 IPSEC_RELEASE_GLOBAL_LOCKS();
358 if (m)
359 m_freem(m);
360 if (tc != NULL)
361 pool_put(&ipcomp_tdb_crypto_pool, tc);
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 secasvar *sav,
375 struct mbuf **mp,
376 int skip,
377 int protoff
378 )
379 {
380 char buf[IPSEC_ADDRSTRLEN];
381 const struct comp_algo *ipcompx;
382 int error, ralen, hlen, maxpacketsize;
383 struct cryptodesc *crdc;
384 struct cryptop *crp;
385 struct tdb_crypto *tc;
386
387 IPSEC_SPLASSERT_SOFTNET(__func__);
388 KASSERT(sav != NULL);
389 KASSERT(sav->tdb_compalgxform != NULL);
390 ipcompx = sav->tdb_compalgxform;
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, sav);
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(("%s: unknown/unsupported protocol family %d"
419 ", IPCA %s/%08lx\n", __func__,
420 sav->sah->saidx.dst.sa.sa_family,
421 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
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(("%s: packet in IPCA %s/%08lx got too big "
429 "(len %u, max len %u)\n", __func__,
430 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
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(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
444 __func__,
445 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
446 (u_long) ntohl(sav->spi)));
447 error = ENOBUFS;
448 goto bad;
449 }
450
451 /* Ok now, we can pass to the crypto processing */
452
453 /* Get crypto descriptors */
454 crp = crypto_getreq(1);
455 if (crp == NULL) {
456 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
457 DPRINTF(("%s: failed to acquire crypto descriptor\n",
458 __func__));
459 error = ENOBUFS;
460 goto bad;
461 }
462 crdc = crp->crp_desc;
463
464 /* Compression descriptor */
465 crdc->crd_skip = skip;
466 crdc->crd_len = m->m_pkthdr.len - skip;
467 crdc->crd_flags = CRD_F_COMP;
468 crdc->crd_inject = skip;
469
470 /* Compression operation */
471 crdc->crd_alg = ipcompx->type;
472
473 /* IPsec-specific opaque crypto info */
474 tc = pool_get(&ipcomp_tdb_crypto_pool, PR_NOWAIT);
475 if (tc == NULL) {
476 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
477 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
478 crypto_freereq(crp);
479 error = ENOBUFS;
480 goto bad;
481 }
482
483 {
484 int s = pserialize_read_enter();
485
486 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
487 pserialize_read_exit(s);
488 pool_put(&ipcomp_tdb_crypto_pool, tc);
489 crypto_freereq(crp);
490 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
491 error = ENOENT;
492 goto bad;
493 }
494 KEY_SP_REF(isr->sp);
495 pserialize_read_exit(s);
496 }
497
498 tc->tc_isr = isr;
499 tc->tc_spi = sav->spi;
500 tc->tc_dst = sav->sah->saidx.dst;
501 tc->tc_proto = sav->sah->saidx.proto;
502 tc->tc_skip = skip;
503 tc->tc_protoff = protoff;
504 tc->tc_sav = sav;
505 KEY_SA_REF(sav);
506
507 /* Crypto operation descriptor */
508 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
509 crp->crp_flags = CRYPTO_F_IMBUF;
510 crp->crp_buf = m;
511 crp->crp_callback = ipcomp_output_cb;
512 crp->crp_opaque = tc;
513 crp->crp_sid = sav->tdb_cryptoid;
514
515 return crypto_dispatch(crp);
516 bad:
517 if (m)
518 m_freem(m);
519 return (error);
520 }
521
522 /*
523 * IPComp output callback from the crypto driver.
524 */
525 static int
526 ipcomp_output_cb(struct cryptop *crp)
527 {
528 char buf[IPSEC_ADDRSTRLEN];
529 struct tdb_crypto *tc;
530 struct ipsecrequest *isr;
531 struct secasvar *sav;
532 struct mbuf *m, *mo;
533 int error, skip, rlen, roff;
534 uint8_t prot;
535 uint16_t cpi;
536 struct ipcomp * ipcomp;
537 IPSEC_DECLARE_LOCK_VARIABLE;
538
539 KASSERT(crp->crp_opaque != NULL);
540 tc = crp->crp_opaque;
541 m = crp->crp_buf;
542 skip = tc->tc_skip;
543 rlen = crp->crp_ilen - skip;
544
545 IPSEC_ACQUIRE_GLOBAL_LOCKS();
546
547 isr = tc->tc_isr;
548 sav = tc->tc_sav;
549 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
550 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
551 IPSECLOG(LOG_DEBUG,
552 "SP is being destroyed while in crypto (id=%u)\n",
553 isr->sp->id);
554 error = ENOENT;
555 goto bad;
556 }
557 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
558 KEY_SA_UNREF(&sav);
559 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
560 if (sav == NULL) {
561 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
562 DPRINTF(("%s: SA expired while in crypto\n", __func__));
563 error = ENOBUFS; /*XXX*/
564 goto bad;
565 }
566 }
567
568 /* Check for crypto errors */
569 if (crp->crp_etype) {
570 /* Reset session ID */
571 if (sav->tdb_cryptoid != 0)
572 sav->tdb_cryptoid = crp->crp_sid;
573
574 if (crp->crp_etype == EAGAIN) {
575 IPSEC_RELEASE_GLOBAL_LOCKS();
576 return crypto_dispatch(crp);
577 }
578 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
579 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
580 error = crp->crp_etype;
581 goto bad;
582 }
583
584 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
585
586 if (rlen > crp->crp_olen) {
587 /* Inject IPCOMP header */
588 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
589 if (mo == NULL) {
590 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
591 DPRINTF(("%s: failed to inject IPCOMP header for "
592 "IPCA %s/%08lx\n", __func__,
593 ipsec_address(&sav->sah->saidx.dst, buf,
594 sizeof(buf)), (u_long) ntohl(sav->spi)));
595 error = ENOBUFS;
596 goto bad;
597 }
598 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
599
600 /* Initialize the IPCOMP header */
601 /* XXX alignment always correct? */
602 switch (sav->sah->saidx.dst.sa.sa_family) {
603 #ifdef INET
604 case AF_INET:
605 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
606 break;
607 #endif /* INET */
608 #ifdef INET6
609 case AF_INET6:
610 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
611 break;
612 #endif
613 }
614 ipcomp->comp_flags = 0;
615
616 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
617 cpi = sav->alg_enc;
618 else
619 cpi = ntohl(sav->spi) & 0xffff;
620 ipcomp->comp_cpi = htons(cpi);
621
622 /* Fix Next Protocol in IPv4/IPv6 header */
623 prot = IPPROTO_IPCOMP;
624 m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
625
626 /* Adjust the length in the IP header */
627 switch (sav->sah->saidx.dst.sa.sa_family) {
628 #ifdef INET
629 case AF_INET:
630 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
631 break;
632 #endif /* INET */
633 #ifdef INET6
634 case AF_INET6:
635 mtod(m, struct ip6_hdr *)->ip6_plen =
636 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
637 break;
638 #endif /* INET6 */
639 default:
640 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
641 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
642 "family %d, IPCA %s/%08lx\n",
643 sav->sah->saidx.dst.sa.sa_family,
644 ipsec_address(&sav->sah->saidx.dst, buf,
645 sizeof(buf)), (u_long) ntohl(sav->spi)));
646 error = EPFNOSUPPORT;
647 goto bad;
648 }
649 } else {
650 /* compression was useless, we have lost time */
651 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
652 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
653 "and compressed size is %d\n", rlen, crp->crp_olen));
654 }
655
656
657 /* Release the crypto descriptor */
658 pool_put(&ipcomp_tdb_crypto_pool, tc);
659 crypto_freereq(crp);
660
661 /* NB: m is reclaimed by ipsec_process_done. */
662 error = ipsec_process_done(m, isr, sav);
663 KEY_SA_UNREF(&sav);
664 KEY_SP_UNREF(&isr->sp);
665 IPSEC_RELEASE_GLOBAL_LOCKS();
666 return error;
667 bad:
668 if (sav)
669 KEY_SA_UNREF(&sav);
670 KEY_SP_UNREF(&isr->sp);
671 IPSEC_RELEASE_GLOBAL_LOCKS();
672 if (m)
673 m_freem(m);
674 pool_put(&ipcomp_tdb_crypto_pool, tc);
675 crypto_freereq(crp);
676 return error;
677 }
678
679 static struct xformsw ipcomp_xformsw = {
680 .xf_type = XF_IPCOMP,
681 .xf_flags = XFT_COMP,
682 .xf_name = "IPcomp",
683 .xf_init = ipcomp_init,
684 .xf_zeroize = ipcomp_zeroize,
685 .xf_input = ipcomp_input,
686 .xf_output = ipcomp_output,
687 .xf_next = NULL,
688 };
689
690 void
691 ipcomp_attach(void)
692 {
693 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
694 pool_init(&ipcomp_tdb_crypto_pool, sizeof(struct tdb_crypto),
695 0, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET);
696 xform_register(&ipcomp_xformsw);
697 }
698