xform_ipcomp.c revision 1.12 1 /* $NetBSD: xform_ipcomp.c,v 1.12 2007/03/04 19:54:49 degroote 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.12 2007/03/04 19:54:49 degroote Exp $");
34
35 /* IP payload compression protocol (IPComp), see RFC 2393 */
36 #include "opt_inet.h"
37 #ifdef __FreeBSD__
38 #include "opt_inet6.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
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/xform.h>
57
58 #ifdef INET6
59 #include <netinet/ip6.h>
60 #include <netipsec/ipsec6.h>
61 #endif
62
63 #include <netipsec/ipcomp.h>
64 #include <netipsec/ipcomp_var.h>
65
66 #include <netipsec/key.h>
67 #include <netipsec/key_debug.h>
68
69 #include <netipsec/ipsec_osdep.h>
70
71 #include <opencrypto/cryptodev.h>
72 #include <opencrypto/deflate.h>
73 #include <opencrypto/xform.h>
74
75 int ipcomp_enable = 1;
76 struct ipcompstat ipcompstat;
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 struct comp_algo *
90 ipcomp_algorithm_lookup(int alg)
91 {
92 if (alg >= IPCOMP_ALG_MAX)
93 return NULL;
94 switch (alg) {
95 case SADB_X_CALG_DEFLATE:
96 return &comp_algo_deflate;
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, struct xformsw *xsp)
106 {
107 struct comp_algo *tcomp;
108 struct cryptoini cric;
109
110 /* NB: algorithm really comes in alg_enc and not alg_comp! */
111 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
112 if (tcomp == NULL) {
113 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
114 sav->alg_comp));
115 return EINVAL;
116 }
117 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
118 sav->tdb_xform = xsp;
119 sav->tdb_compalgxform = tcomp;
120
121 /* Initialize crypto session */
122 bzero(&cric, sizeof (cric));
123 cric.cri_alg = sav->tdb_compalgxform->type;
124
125 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
126 }
127
128 /*
129 * ipcomp_zeroize() used when IPCA is deleted
130 */
131 static int
132 ipcomp_zeroize(struct secasvar *sav)
133 {
134 int err;
135
136 err = crypto_freesession(sav->tdb_cryptoid);
137 sav->tdb_cryptoid = 0;
138 return err;
139 }
140
141 /*
142 * ipcomp_input() gets called to uncompress an input packet
143 */
144 static int
145 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
146 {
147 struct tdb_crypto *tc;
148 struct cryptodesc *crdc;
149 struct cryptop *crp;
150 int hlen = IPCOMP_HLENGTH;
151
152 IPSEC_SPLASSERT_SOFTNET("ipcomp_input");
153
154 /* Get crypto descriptors */
155 crp = crypto_getreq(1);
156 if (crp == NULL) {
157 m_freem(m);
158 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
159 ipcompstat.ipcomps_crypto++;
160 return ENOBUFS;
161 }
162 /* Get IPsec-specific opaque pointer */
163 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
164 if (tc == NULL) {
165 m_freem(m);
166 crypto_freereq(crp);
167 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
168 ipcompstat.ipcomps_crypto++;
169 return ENOBUFS;
170 }
171 crdc = crp->crp_desc;
172
173 crdc->crd_skip = skip + hlen;
174 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
175 crdc->crd_inject = skip;
176
177 tc->tc_ptr = 0;
178
179 /* Decompression operation */
180 crdc->crd_alg = sav->tdb_compalgxform->type;
181
182 /* Crypto operation descriptor */
183 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
184 crp->crp_flags = CRYPTO_F_IMBUF;
185 crp->crp_buf = (void *) m;
186 crp->crp_callback = ipcomp_input_cb;
187 crp->crp_sid = sav->tdb_cryptoid;
188 crp->crp_opaque = (void *) tc;
189
190 /* These are passed as-is to the callback */
191 tc->tc_spi = sav->spi;
192 tc->tc_dst = sav->sah->saidx.dst;
193 tc->tc_proto = sav->sah->saidx.proto;
194 tc->tc_protoff = protoff;
195 tc->tc_skip = skip;
196
197 return crypto_dispatch(crp);
198 }
199
200 #ifdef INET6
201 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
202 if (saidx->dst.sa.sa_family == AF_INET6) { \
203 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
204 } else { \
205 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
206 } \
207 } while (0)
208 #else
209 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
210 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
211 #endif
212
213 /*
214 * IPComp input callback from the crypto driver.
215 */
216 static int
217 ipcomp_input_cb(struct cryptop *crp)
218 {
219 struct cryptodesc *crd;
220 struct tdb_crypto *tc;
221 int skip, protoff;
222 struct mtag *mtag;
223 struct mbuf *m;
224 struct secasvar *sav;
225 struct secasindex *saidx;
226 int s, hlen = IPCOMP_HLENGTH, error, clen;
227 u_int8_t nproto;
228 void *addr;
229
230 crd = crp->crp_desc;
231
232 tc = (struct tdb_crypto *) crp->crp_opaque;
233 IPSEC_ASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
234 skip = tc->tc_skip;
235 protoff = tc->tc_protoff;
236 mtag = (struct mtag *) tc->tc_ptr;
237 m = (struct mbuf *) crp->crp_buf;
238
239 s = splsoftnet();
240
241 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
242 if (sav == NULL) {
243 ipcompstat.ipcomps_notdb++;
244 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
245 error = ENOBUFS; /*XXX*/
246 goto bad;
247 }
248
249 saidx = &sav->sah->saidx;
250 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
251 saidx->dst.sa.sa_family == AF_INET6,
252 ("ah_input_cb: unexpected protocol family %u",
253 saidx->dst.sa.sa_family));
254
255 /* Check for crypto errors */
256 if (crp->crp_etype) {
257 /* Reset the session ID */
258 if (sav->tdb_cryptoid != 0)
259 sav->tdb_cryptoid = crp->crp_sid;
260
261 if (crp->crp_etype == EAGAIN) {
262 KEY_FREESAV(&sav);
263 splx(s);
264 return crypto_dispatch(crp);
265 }
266
267 ipcompstat.ipcomps_noxform++;
268 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
269 error = crp->crp_etype;
270 goto bad;
271 }
272 /* Shouldn't happen... */
273 if (m == NULL) {
274 ipcompstat.ipcomps_crypto++;
275 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
276 error = EINVAL;
277 goto bad;
278 }
279 ipcompstat.ipcomps_hist[sav->alg_comp]++;
280
281 clen = crp->crp_olen; /* Length of data after processing */
282
283 /* Release the crypto descriptors */
284 free(tc, M_XDATA), tc = NULL;
285 crypto_freereq(crp), crp = NULL;
286
287 /* In case it's not done already, adjust the size of the mbuf chain */
288 m->m_pkthdr.len = clen + hlen + skip;
289
290 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
291 ipcompstat.ipcomps_hdrops++; /*XXX*/
292 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
293 error = EINVAL; /*XXX*/
294 goto bad;
295 }
296
297 /* Keep the next protocol field */
298 addr = mtod(m, struct ip *) + skip;
299 nproto = ((struct ipcomp *) addr)->comp_nxt;
300
301 /* Remove the IPCOMP header */
302 error = m_striphdr(m, skip, hlen);
303 if (error) {
304 ipcompstat.ipcomps_hdrops++;
305 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
306 ipsec_address(&sav->sah->saidx.dst),
307 (u_long) ntohl(sav->spi)));
308 goto bad;
309 }
310
311 /* Restore the Next Protocol field */
312 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
313
314 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
315
316 KEY_FREESAV(&sav);
317 splx(s);
318 return error;
319 bad:
320 if (sav)
321 KEY_FREESAV(&sav);
322 splx(s);
323 if (m)
324 m_freem(m);
325 if (tc != NULL)
326 free(tc, M_XDATA);
327 if (crp)
328 crypto_freereq(crp);
329 return error;
330 }
331
332 /*
333 * IPComp output routine, called by ipsec[46]_process_packet()
334 */
335 static int
336 ipcomp_output(
337 struct mbuf *m,
338 struct ipsecrequest *isr,
339 struct mbuf **mp,
340 int skip,
341 int protoff
342 )
343 {
344 struct secasvar *sav;
345 struct comp_algo *ipcompx;
346 int error, ralen, hlen, maxpacketsize;
347 struct cryptodesc *crdc;
348 struct cryptop *crp;
349 struct tdb_crypto *tc;
350
351 IPSEC_SPLASSERT_SOFTNET("ipcomp_output");
352 sav = isr->sav;
353 IPSEC_ASSERT(sav != NULL, ("ipcomp_output: null SA"));
354 ipcompx = sav->tdb_compalgxform;
355 IPSEC_ASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
356
357 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
358
359 /* Don't process the packet if it is too short */
360 if (ralen < ipcompx->minlen) {
361 ipcompstat.ipcomps_minlen++;
362 return ipsec_process_done(m,isr);
363 }
364
365 hlen = IPCOMP_HLENGTH;
366
367 ipcompstat.ipcomps_output++;
368
369 /* Check for maximum packet size violations. */
370 switch (sav->sah->saidx.dst.sa.sa_family) {
371 #ifdef INET
372 case AF_INET:
373 maxpacketsize = IP_MAXPACKET;
374 break;
375 #endif /* INET */
376 #ifdef INET6
377 case AF_INET6:
378 maxpacketsize = IPV6_MAXPACKET;
379 break;
380 #endif /* INET6 */
381 default:
382 ipcompstat.ipcomps_nopf++;
383 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
384 ", IPCA %s/%08lx\n",
385 sav->sah->saidx.dst.sa.sa_family,
386 ipsec_address(&sav->sah->saidx.dst),
387 (u_long) ntohl(sav->spi)));
388 error = EPFNOSUPPORT;
389 goto bad;
390 }
391 if (skip + hlen + ralen > maxpacketsize) {
392 ipcompstat.ipcomps_toobig++;
393 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
394 "(len %u, max len %u)\n",
395 ipsec_address(&sav->sah->saidx.dst),
396 (u_long) ntohl(sav->spi),
397 skip + hlen + ralen, maxpacketsize));
398 error = EMSGSIZE;
399 goto bad;
400 }
401
402 /* Update the counters */
403 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
404
405 m = m_clone(m);
406 if (m == NULL) {
407 ipcompstat.ipcomps_hdrops++;
408 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
409 ipsec_address(&sav->sah->saidx.dst),
410 (u_long) ntohl(sav->spi)));
411 error = ENOBUFS;
412 goto bad;
413 }
414
415 /* Ok now, we can pass to the crypto processing */
416
417 /* Get crypto descriptors */
418 crp = crypto_getreq(1);
419 if (crp == NULL) {
420 ipcompstat.ipcomps_crypto++;
421 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
422 error = ENOBUFS;
423 goto bad;
424 }
425 crdc = crp->crp_desc;
426
427 /* Compression descriptor */
428 crdc->crd_skip = skip;
429 crdc->crd_len = m->m_pkthdr.len - skip;
430 crdc->crd_flags = CRD_F_COMP;
431 crdc->crd_inject = skip;
432
433 /* Compression operation */
434 crdc->crd_alg = ipcompx->type;
435
436 /* IPsec-specific opaque crypto info */
437 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
438 M_XDATA, M_NOWAIT|M_ZERO);
439 if (tc == NULL) {
440 ipcompstat.ipcomps_crypto++;
441 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
442 crypto_freereq(crp);
443 error = ENOBUFS;
444 goto bad;
445 }
446
447 tc->tc_isr = isr;
448 tc->tc_spi = sav->spi;
449 tc->tc_dst = sav->sah->saidx.dst;
450 tc->tc_proto = sav->sah->saidx.proto;
451 tc->tc_skip = skip;
452 tc->tc_protoff = protoff;
453
454 /* Crypto operation descriptor */
455 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
456 crp->crp_flags = CRYPTO_F_IMBUF;
457 crp->crp_buf = (void *) m;
458 crp->crp_callback = ipcomp_output_cb;
459 crp->crp_opaque = (void *) tc;
460 crp->crp_sid = sav->tdb_cryptoid;
461
462 return crypto_dispatch(crp);
463 bad:
464 if (m)
465 m_freem(m);
466 return (error);
467 }
468
469 /*
470 * IPComp output callback from the crypto driver.
471 */
472 static int
473 ipcomp_output_cb(struct cryptop *crp)
474 {
475 struct tdb_crypto *tc;
476 struct ipsecrequest *isr;
477 struct secasvar *sav;
478 struct mbuf *m, *mo;
479 int s, error, skip, rlen, roff;
480 u_int8_t prot;
481 u_int16_t cpi;
482 struct ipcomp * ipcomp;
483
484
485 tc = (struct tdb_crypto *) crp->crp_opaque;
486 IPSEC_ASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
487 m = (struct mbuf *) crp->crp_buf;
488 skip = tc->tc_skip;
489 rlen = crp->crp_ilen - skip;
490
491 s = splsoftnet();
492
493 isr = tc->tc_isr;
494 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
495 if (sav == NULL) {
496 ipcompstat.ipcomps_notdb++;
497 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
498 error = ENOBUFS; /*XXX*/
499 goto bad;
500 }
501 IPSEC_ASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
502
503 /* Check for crypto errors */
504 if (crp->crp_etype) {
505 /* Reset session ID */
506 if (sav->tdb_cryptoid != 0)
507 sav->tdb_cryptoid = crp->crp_sid;
508
509 if (crp->crp_etype == EAGAIN) {
510 KEY_FREESAV(&sav);
511 splx(s);
512 return crypto_dispatch(crp);
513 }
514 ipcompstat.ipcomps_noxform++;
515 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
516 error = crp->crp_etype;
517 goto bad;
518 }
519 /* Shouldn't happen... */
520 if (m == NULL) {
521 ipcompstat.ipcomps_crypto++;
522 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
523 error = EINVAL;
524 goto bad;
525 }
526 ipcompstat.ipcomps_hist[sav->alg_comp]++;
527
528 if (rlen > crp->crp_olen) {
529 /* Inject IPCOMP header */
530 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
531 if (mo == NULL) {
532 ipcompstat.ipcomps_wrap++;
533 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
534 "IPCA %s/%08lx\n",
535 ipsec_address(&sav->sah->saidx.dst),
536 (u_long) ntohl(sav->spi)));
537 error = ENOBUFS;
538 goto bad;
539 }
540 ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff);
541
542 /* Initialize the IPCOMP header */
543 /* XXX alignment always correct? */
544 switch (sav->sah->saidx.dst.sa.sa_family) {
545 #ifdef INET
546 case AF_INET:
547 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
548 break;
549 #endif /* INET */
550 #ifdef INET6
551 case AF_INET6:
552 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
553 break;
554 #endif
555 }
556 ipcomp->comp_flags = 0;
557
558 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
559 cpi = sav->alg_enc;
560 else
561 cpi = ntohl(sav->spi) & 0xffff;
562 ipcomp->comp_cpi = htons(cpi);
563
564 /* Fix Next Protocol in IPv4/IPv6 header */
565 prot = IPPROTO_IPCOMP;
566 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), (u_char *)&prot);
567
568 /* Adjust the length in the IP header */
569 switch (sav->sah->saidx.dst.sa.sa_family) {
570 #ifdef INET
571 case AF_INET:
572 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
573 break;
574 #endif /* INET */
575 #ifdef INET6
576 case AF_INET6:
577 mtod(m, struct ip6_hdr *)->ip6_plen =
578 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
579 break;
580 #endif /* INET6 */
581 default:
582 ipcompstat.ipcomps_nopf++;
583 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
584 "family %d, IPCA %s/%08lx\n",
585 sav->sah->saidx.dst.sa.sa_family,
586 ipsec_address(&sav->sah->saidx.dst),
587 (u_long) ntohl(sav->spi)));
588 error = EPFNOSUPPORT;
589 goto bad;
590 }
591 } else {
592 /* compression was useless, we have lost time */
593 /* XXX add statistic */
594 }
595
596 /* Release the crypto descriptor */
597 free(tc, M_XDATA);
598 crypto_freereq(crp);
599
600 /* NB: m is reclaimed by ipsec_process_done. */
601 error = ipsec_process_done(m, isr);
602 KEY_FREESAV(&sav);
603 splx(s);
604 return error;
605 bad:
606 if (sav)
607 KEY_FREESAV(&sav);
608 splx(s);
609 if (m)
610 m_freem(m);
611 free(tc, M_XDATA);
612 crypto_freereq(crp);
613 return error;
614 }
615
616 static struct xformsw ipcomp_xformsw = {
617 XF_IPCOMP, XFT_COMP, "IPcomp",
618 ipcomp_init, ipcomp_zeroize, ipcomp_input,
619 ipcomp_output,
620 NULL,
621 };
622
623 INITFN void
624 ipcomp_attach(void)
625 {
626 xform_register(&ipcomp_xformsw);
627 }
628
629 #ifdef __FreeBSD__
630 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)
631 #endif /* __FreeBSD__ */
632