xform_ipcomp.c revision 1.9 1 /* $NetBSD: xform_ipcomp.c,v 1.9 2007/02/10 09:43:05 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.9 2007/02/10 09:43:05 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 = (caddr_t) m;
186 crp->crp_callback = ipcomp_input_cb;
187 crp->crp_sid = sav->tdb_cryptoid;
188 crp->crp_opaque = (caddr_t) 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 caddr_t 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 = (caddr_t) 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 + hlen;
452
453 /* Crypto operation descriptor */
454 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
455 crp->crp_flags = CRYPTO_F_IMBUF;
456 crp->crp_buf = (caddr_t) m;
457 crp->crp_callback = ipcomp_output_cb;
458 crp->crp_opaque = (caddr_t) tc;
459 crp->crp_sid = sav->tdb_cryptoid;
460
461 return crypto_dispatch(crp);
462 bad:
463 if (m)
464 m_freem(m);
465 return (error);
466 }
467
468 /*
469 * IPComp output callback from the crypto driver.
470 */
471 static int
472 ipcomp_output_cb(struct cryptop *crp)
473 {
474 struct tdb_crypto *tc;
475 struct ipsecrequest *isr;
476 struct secasvar *sav;
477 struct mbuf *m, *mo;
478 int s, error, skip, rlen, roff;
479 u_int8_t prot;
480 u_int16_t cpi;
481 struct ipcomp * ipcomp;
482
483
484 tc = (struct tdb_crypto *) crp->crp_opaque;
485 IPSEC_ASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
486 m = (struct mbuf *) crp->crp_buf;
487 skip = tc->tc_skip;
488 rlen = crp->crp_ilen - skip;
489
490 s = splsoftnet();
491
492 isr = tc->tc_isr;
493 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
494 if (sav == NULL) {
495 ipcompstat.ipcomps_notdb++;
496 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
497 error = ENOBUFS; /*XXX*/
498 goto bad;
499 }
500 IPSEC_ASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
501
502 /* Check for crypto errors */
503 if (crp->crp_etype) {
504 /* Reset session ID */
505 if (sav->tdb_cryptoid != 0)
506 sav->tdb_cryptoid = crp->crp_sid;
507
508 if (crp->crp_etype == EAGAIN) {
509 KEY_FREESAV(&sav);
510 splx(s);
511 return crypto_dispatch(crp);
512 }
513 ipcompstat.ipcomps_noxform++;
514 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
515 error = crp->crp_etype;
516 goto bad;
517 }
518 /* Shouldn't happen... */
519 if (m == NULL) {
520 ipcompstat.ipcomps_crypto++;
521 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
522 error = EINVAL;
523 goto bad;
524 }
525 ipcompstat.ipcomps_hist[sav->alg_comp]++;
526
527 if (rlen > crp->crp_olen) {
528 /* Inject IPCOMP header */
529 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
530 if (mo == NULL) {
531 ipcompstat.ipcomps_wrap++;
532 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
533 "IPCA %s/%08lx\n",
534 ipsec_address(&sav->sah->saidx.dst),
535 (u_long) ntohl(sav->spi)));
536 error = ENOBUFS;
537 goto bad;
538 }
539 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
540
541 /* Initialize the IPCOMP header */
542 /* XXX alignment always correct? */
543 switch (sav->sah->saidx.dst.sa.sa_family) {
544 #ifdef INET
545 case AF_INET:
546 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
547 break;
548 #endif /* INET */
549 #ifdef INET6
550 case AF_INET6:
551 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
552 break;
553 #endif
554 }
555 ipcomp->comp_flags = 0;
556
557 if ((sav->flags & SADB_X_EXT_RAWCPI) == 0)
558 cpi = sav->alg_enc;
559 else
560 cpi = ntohl(sav->spi) & 0xffff;
561 ipcomp->comp_cpi = htons(cpi);
562
563 /* Fix Next Protocol in IPv4/IPv6 header */
564 prot = IPPROTO_IPCOMP;
565 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), (u_char *)&prot);
566
567 /* Adjust the length in the IP header */
568 switch (sav->sah->saidx.dst.sa.sa_family) {
569 #ifdef INET
570 case AF_INET:
571 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
572 break;
573 #endif /* INET */
574 #ifdef INET6
575 case AF_INET6:
576 mtod(m, struct ip6_hdr *)->ip6_plen =
577 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
578 break;
579 #endif /* INET6 */
580 default:
581 ipcompstat.ipcomps_nopf++;
582 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
583 "family %d, IPCA %s/%08lx\n",
584 sav->sah->saidx.dst.sa.sa_family,
585 ipsec_address(&sav->sah->saidx.dst),
586 (u_long) ntohl(sav->spi)));
587 error = EPFNOSUPPORT;
588 goto bad;
589 }
590 } else {
591 /* compression was useless, we have lost time */
592 /* XXX add statistic */
593 }
594
595 /* Release the crypto descriptor */
596 free(tc, M_XDATA);
597 crypto_freereq(crp);
598
599 /* NB: m is reclaimed by ipsec_process_done. */
600 error = ipsec_process_done(m, isr);
601 KEY_FREESAV(&sav);
602 splx(s);
603 return error;
604 bad:
605 if (sav)
606 KEY_FREESAV(&sav);
607 splx(s);
608 if (m)
609 m_freem(m);
610 free(tc, M_XDATA);
611 crypto_freereq(crp);
612 return error;
613 }
614
615 static struct xformsw ipcomp_xformsw = {
616 XF_IPCOMP, XFT_COMP, "IPcomp",
617 ipcomp_init, ipcomp_zeroize, ipcomp_input,
618 ipcomp_output,
619 NULL,
620 };
621
622 INITFN void
623 ipcomp_attach(void)
624 {
625 xform_register(&ipcomp_xformsw);
626 }
627
628 #ifdef __FreeBSD__
629 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)
630 #endif /* __FreeBSD__ */
631