xform_ipcomp.c revision 1.55 1 /* $NetBSD: xform_ipcomp.c,v 1.55 2018/02/14 09:13:03 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.55 2018/02/14 09:13:03 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 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
279 KEY_SA_UNREF(&sav);
280 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi,
281 sport, dport);
282 if (sav == NULL) {
283 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
284 DPRINTF(("%s: SA expired while in crypto\n", __func__));
285 error = ENOBUFS; /*XXX*/
286 goto bad;
287 }
288 }
289
290 saidx = &sav->sah->saidx;
291 KASSERTMSG(saidx->dst.sa.sa_family == AF_INET ||
292 saidx->dst.sa.sa_family == AF_INET6,
293 "unexpected protocol family %u", saidx->dst.sa.sa_family);
294
295 /* Check for crypto errors */
296 if (crp->crp_etype) {
297 /* Reset the session ID */
298 if (sav->tdb_cryptoid != 0)
299 sav->tdb_cryptoid = crp->crp_sid;
300
301 if (crp->crp_etype == EAGAIN) {
302 KEY_SA_UNREF(&sav);
303 IPSEC_RELEASE_GLOBAL_LOCKS();
304 return crypto_dispatch(crp);
305 }
306
307 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
308 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
309 error = crp->crp_etype;
310 goto bad;
311 }
312
313 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
314
315 /* Update the counters */
316 IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen);
317
318
319 clen = crp->crp_olen; /* Length of data after processing */
320
321 /* Release the crypto descriptors */
322 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
323 tc = NULL;
324 crypto_freereq(crp), crp = NULL;
325
326 /* In case it's not done already, adjust the size of the mbuf chain */
327 m->m_pkthdr.len = clen + hlen + skip;
328
329 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
330 IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/
331 DPRINTF(("%s: m_pullup failed\n", __func__));
332 error = EINVAL; /*XXX*/
333 goto bad;
334 }
335
336 /* Keep the next protocol field */
337 addr = (uint8_t*) mtod(m, struct ip *) + skip;
338 nproto = ((struct ipcomp *) addr)->comp_nxt;
339 switch (nproto) {
340 case IPPROTO_IPCOMP:
341 case IPPROTO_AH:
342 case IPPROTO_ESP:
343 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
344 DPRINTF(("%s: nested ipcomp, IPCA %s/%08lx\n", __func__,
345 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
346 (u_long) ntohl(sav->spi)));
347 error = EINVAL;
348 goto bad;
349 default:
350 break;
351 }
352
353 /* Remove the IPCOMP header */
354 error = m_striphdr(m, skip, hlen);
355 if (error) {
356 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
357 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
358 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
359 (u_long) ntohl(sav->spi)));
360 goto bad;
361 }
362
363 /* Restore the Next Protocol field */
364 m_copyback(m, protoff, sizeof(uint8_t), (uint8_t *) &nproto);
365
366 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff);
367
368 KEY_SA_UNREF(&sav);
369 IPSEC_RELEASE_GLOBAL_LOCKS();
370 return error;
371 bad:
372 if (sav)
373 KEY_SA_UNREF(&sav);
374 IPSEC_RELEASE_GLOBAL_LOCKS();
375 if (m)
376 m_freem(m);
377 if (tc != NULL)
378 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
379 if (crp)
380 crypto_freereq(crp);
381 return error;
382 }
383
384 /*
385 * IPComp output routine, called by ipsec[46]_process_packet()
386 */
387 static int
388 ipcomp_output(
389 struct mbuf *m,
390 const struct ipsecrequest *isr,
391 struct secasvar *sav,
392 struct mbuf **mp,
393 int skip,
394 int protoff
395 )
396 {
397 char buf[IPSEC_ADDRSTRLEN];
398 const struct comp_algo *ipcompx;
399 int error, ralen, hlen, maxpacketsize;
400 struct cryptodesc *crdc;
401 struct cryptop *crp;
402 struct tdb_crypto *tc;
403
404 IPSEC_SPLASSERT_SOFTNET(__func__);
405 KASSERT(sav != NULL);
406 KASSERT(sav->tdb_compalgxform != NULL);
407 ipcompx = sav->tdb_compalgxform;
408
409 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
410
411 /* Don't process the packet if it is too short */
412 if (ralen < ipcompx->minlen) {
413 IPCOMP_STATINC(IPCOMP_STAT_MINLEN);
414 return ipsec_process_done(m, isr, sav);
415 }
416
417 hlen = IPCOMP_HLENGTH;
418
419 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT);
420
421 /* Check for maximum packet size violations. */
422 switch (sav->sah->saidx.dst.sa.sa_family) {
423 #ifdef INET
424 case AF_INET:
425 maxpacketsize = IP_MAXPACKET;
426 break;
427 #endif /* INET */
428 #ifdef INET6
429 case AF_INET6:
430 maxpacketsize = IPV6_MAXPACKET;
431 break;
432 #endif /* INET6 */
433 default:
434 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
435 DPRINTF(("%s: unknown/unsupported protocol family %d"
436 ", IPCA %s/%08lx\n", __func__,
437 sav->sah->saidx.dst.sa.sa_family,
438 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
439 (u_long) ntohl(sav->spi)));
440 error = EPFNOSUPPORT;
441 goto bad;
442 }
443 if (skip + hlen + ralen > maxpacketsize) {
444 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG);
445 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
446 "(len %u, max len %u)\n", __func__,
447 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
448 (u_long) ntohl(sav->spi),
449 skip + hlen + ralen, maxpacketsize));
450 error = EMSGSIZE;
451 goto bad;
452 }
453
454 /* Update the counters */
455 IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip);
456
457 m = m_clone(m);
458 if (m == NULL) {
459 IPCOMP_STATINC(IPCOMP_STAT_HDROPS);
460 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
461 __func__,
462 ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
463 (u_long) ntohl(sav->spi)));
464 error = ENOBUFS;
465 goto bad;
466 }
467
468 /* Ok now, we can pass to the crypto processing */
469
470 /* Get crypto descriptors */
471 crp = crypto_getreq(1);
472 if (crp == NULL) {
473 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
474 DPRINTF(("%s: failed to acquire crypto descriptor\n",
475 __func__));
476 error = ENOBUFS;
477 goto bad;
478 }
479 crdc = crp->crp_desc;
480
481 /* Compression descriptor */
482 crdc->crd_skip = skip;
483 crdc->crd_len = m->m_pkthdr.len - skip;
484 crdc->crd_flags = CRD_F_COMP;
485 crdc->crd_inject = skip;
486
487 /* Compression operation */
488 crdc->crd_alg = ipcompx->type;
489
490 /* IPsec-specific opaque crypto info */
491 tc = pool_cache_get(ipcomp_tdb_crypto_pool_cache, PR_NOWAIT);
492 if (tc == NULL) {
493 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO);
494 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
495 crypto_freereq(crp);
496 error = ENOBUFS;
497 goto bad;
498 }
499
500 {
501 int s = pserialize_read_enter();
502
503 /*
504 * Take another reference to the SP and the SA for opencrypto callback.
505 */
506 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD ||
507 sav->state == SADB_SASTATE_DEAD)) {
508 pserialize_read_exit(s);
509 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
510 crypto_freereq(crp);
511 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
512 error = ENOENT;
513 goto bad;
514 }
515 KEY_SP_REF(isr->sp);
516 KEY_SA_REF(sav);
517 pserialize_read_exit(s);
518 }
519
520 tc->tc_isr = isr;
521 tc->tc_spi = sav->spi;
522 tc->tc_dst = sav->sah->saidx.dst;
523 tc->tc_proto = sav->sah->saidx.proto;
524 tc->tc_skip = skip;
525 tc->tc_protoff = protoff;
526 tc->tc_sav = sav;
527
528 /* Crypto operation descriptor */
529 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
530 crp->crp_flags = CRYPTO_F_IMBUF;
531 crp->crp_buf = m;
532 crp->crp_callback = ipcomp_output_cb;
533 crp->crp_opaque = tc;
534 crp->crp_sid = sav->tdb_cryptoid;
535
536 return crypto_dispatch(crp);
537 bad:
538 if (m)
539 m_freem(m);
540 return (error);
541 }
542
543 /*
544 * IPComp output callback from the crypto driver.
545 */
546 static int
547 ipcomp_output_cb(struct cryptop *crp)
548 {
549 char buf[IPSEC_ADDRSTRLEN];
550 struct tdb_crypto *tc;
551 const struct ipsecrequest *isr;
552 struct secasvar *sav;
553 struct mbuf *m, *mo;
554 int error, skip, rlen, roff;
555 uint8_t prot;
556 uint16_t cpi;
557 struct ipcomp * ipcomp;
558 IPSEC_DECLARE_LOCK_VARIABLE;
559
560 KASSERT(crp->crp_opaque != NULL);
561 tc = crp->crp_opaque;
562 m = crp->crp_buf;
563 skip = tc->tc_skip;
564 rlen = crp->crp_ilen - skip;
565
566 IPSEC_ACQUIRE_GLOBAL_LOCKS();
567
568 isr = tc->tc_isr;
569 sav = tc->tc_sav;
570 if (__predict_false(isr->sp->state == IPSEC_SPSTATE_DEAD)) {
571 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
572 IPSECLOG(LOG_DEBUG,
573 "SP is being destroyed while in crypto (id=%u)\n",
574 isr->sp->id);
575 error = ENOENT;
576 goto bad;
577 }
578 if (__predict_false(!SADB_SASTATE_USABLE_P(sav))) {
579 KEY_SA_UNREF(&sav);
580 sav = KEY_LOOKUP_SA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0);
581 if (sav == NULL) {
582 IPCOMP_STATINC(IPCOMP_STAT_NOTDB);
583 DPRINTF(("%s: SA expired while in crypto\n", __func__));
584 error = ENOBUFS; /*XXX*/
585 goto bad;
586 }
587 }
588
589 /* Check for crypto errors */
590 if (crp->crp_etype) {
591 /* Reset session ID */
592 if (sav->tdb_cryptoid != 0)
593 sav->tdb_cryptoid = crp->crp_sid;
594
595 if (crp->crp_etype == EAGAIN) {
596 IPSEC_RELEASE_GLOBAL_LOCKS();
597 return crypto_dispatch(crp);
598 }
599 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM);
600 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
601 error = crp->crp_etype;
602 goto bad;
603 }
604
605 IPCOMP_STATINC(IPCOMP_STAT_HIST + ipcomp_stats[sav->alg_comp]);
606
607 if (rlen > crp->crp_olen) {
608 /* Inject IPCOMP header */
609 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
610 if (mo == NULL) {
611 IPCOMP_STATINC(IPCOMP_STAT_WRAP);
612 DPRINTF(("%s: failed to inject IPCOMP header for "
613 "IPCA %s/%08lx\n", __func__,
614 ipsec_address(&sav->sah->saidx.dst, buf,
615 sizeof(buf)), (u_long) ntohl(sav->spi)));
616 error = ENOBUFS;
617 goto bad;
618 }
619 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
620
621 /* Initialize the IPCOMP header */
622 /* XXX alignment always correct? */
623 switch (sav->sah->saidx.dst.sa.sa_family) {
624 #ifdef INET
625 case AF_INET:
626 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
627 break;
628 #endif /* INET */
629 #ifdef INET6
630 case AF_INET6:
631 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
632 break;
633 #endif
634 }
635 ipcomp->comp_flags = 0;
636
637 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
638 cpi = sav->alg_enc;
639 else
640 cpi = ntohl(sav->spi) & 0xffff;
641 ipcomp->comp_cpi = htons(cpi);
642
643 /* Fix Next Protocol in IPv4/IPv6 header */
644 prot = IPPROTO_IPCOMP;
645 m_copyback(m, tc->tc_protoff, sizeof(uint8_t), (u_char *)&prot);
646
647 /* Adjust the length in the IP header */
648 switch (sav->sah->saidx.dst.sa.sa_family) {
649 #ifdef INET
650 case AF_INET:
651 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
652 break;
653 #endif /* INET */
654 #ifdef INET6
655 case AF_INET6:
656 mtod(m, struct ip6_hdr *)->ip6_plen =
657 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
658 break;
659 #endif /* INET6 */
660 default:
661 IPCOMP_STATINC(IPCOMP_STAT_NOPF);
662 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
663 "family %d, IPCA %s/%08lx\n",
664 sav->sah->saidx.dst.sa.sa_family,
665 ipsec_address(&sav->sah->saidx.dst, buf,
666 sizeof(buf)), (u_long) ntohl(sav->spi)));
667 error = EPFNOSUPPORT;
668 goto bad;
669 }
670 } else {
671 /* compression was useless, we have lost time */
672 IPCOMP_STATINC(IPCOMP_STAT_USELESS);
673 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
674 "and compressed size is %d\n", rlen, crp->crp_olen));
675 }
676
677
678 /* Release the crypto descriptor */
679 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
680 crypto_freereq(crp);
681
682 /* NB: m is reclaimed by ipsec_process_done. */
683 error = ipsec_process_done(m, isr, sav);
684 KEY_SA_UNREF(&sav);
685 KEY_SP_UNREF(&isr->sp);
686 IPSEC_RELEASE_GLOBAL_LOCKS();
687 return error;
688 bad:
689 if (sav)
690 KEY_SA_UNREF(&sav);
691 KEY_SP_UNREF(&isr->sp);
692 IPSEC_RELEASE_GLOBAL_LOCKS();
693 if (m)
694 m_freem(m);
695 pool_cache_put(ipcomp_tdb_crypto_pool_cache, tc);
696 crypto_freereq(crp);
697 return error;
698 }
699
700 static struct xformsw ipcomp_xformsw = {
701 .xf_type = XF_IPCOMP,
702 .xf_flags = XFT_COMP,
703 .xf_name = "IPcomp",
704 .xf_init = ipcomp_init,
705 .xf_zeroize = ipcomp_zeroize,
706 .xf_input = ipcomp_input,
707 .xf_output = ipcomp_output,
708 .xf_next = NULL,
709 };
710
711 void
712 ipcomp_attach(void)
713 {
714 ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS);
715 ipcomp_tdb_crypto_pool_cache = pool_cache_init(sizeof(struct tdb_crypto),
716 coherency_unit, 0, 0, "ipcomp_tdb_crypto", NULL, IPL_SOFTNET,
717 NULL, NULL, NULL);
718 xform_register(&ipcomp_xformsw);
719 }
720