1 /* $FreeBSD: releng/7.3/sys/netipsec/xform_ipcomp.c 201244 2009-12-30 00:03:36Z bz $ */
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3
4 /*-
5 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* IP payload compression protocol (IPComp), see RFC 2393 */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
49
50 #include <net/route.h>
51 #include <netipsec/ipsec.h>
52 #include <netipsec/xform.h>
53
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #include <netipsec/ipsec6.h>
57 #endif
58
59 #include <netipsec/ipcomp.h>
60 #include <netipsec/ipcomp_var.h>
61
62 #include <netipsec/key.h>
63 #include <netipsec/key_debug.h>
64
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/deflate.h>
67 #include <opencrypto/xform.h>
68
69 int ipcomp_enable = 1;
70 struct ipcompstat ipcompstat;
71
72 SYSCTL_DECL(_net_inet_ipcomp);
73 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
74 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
75 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
76 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
77
78 static int ipcomp_input_cb(struct cryptop *crp);
79 static int ipcomp_output_cb(struct cryptop *crp);
80
81 struct comp_algo *
82 ipcomp_algorithm_lookup(int alg)
83 {
84 if (alg >= IPCOMP_ALG_MAX)
85 return NULL;
86 switch (alg) {
87 case SADB_X_CALG_DEFLATE:
88 return &comp_algo_deflate;
89 }
90 return NULL;
91 }
92
93 /*
94 * ipcomp_init() is called when an CPI is being set up.
95 */
96 static int
97 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
98 {
99 struct comp_algo *tcomp;
100 struct cryptoini cric;
101
102 /* NB: algorithm really comes in alg_enc and not alg_comp! */
103 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
104 if (tcomp == NULL) {
105 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
106 sav->alg_comp));
107 return EINVAL;
108 }
109 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
110 sav->tdb_xform = xsp;
111 sav->tdb_compalgxform = tcomp;
112
113 /* Initialize crypto session */
114 bzero(&cric, sizeof (cric));
115 cric.cri_alg = sav->tdb_compalgxform->type;
116
117 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
118 }
119
120 /*
121 * ipcomp_zeroize() used when IPCA is deleted
122 */
123 static int
124 ipcomp_zeroize(struct secasvar *sav)
125 {
126 int err;
127
128 err = crypto_freesession(sav->tdb_cryptoid);
129 sav->tdb_cryptoid = 0;
130 return err;
131 }
132
133 /*
134 * ipcomp_input() gets called to uncompress an input packet
135 */
136 static int
137 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
138 {
139 struct tdb_crypto *tc;
140 struct cryptodesc *crdc;
141 struct cryptop *crp;
142 int hlen = IPCOMP_HLENGTH;
143
144 IPSEC_SPLASSERT_SOFTNET(__func__);
145
146 /* Get crypto descriptors */
147 crp = crypto_getreq(1);
148 if (crp == NULL) {
149 m_freem(m);
150 DPRINTF(("%s: no crypto descriptors\n", __func__));
151 ipcompstat.ipcomps_crypto++;
152 return ENOBUFS;
153 }
154 /* Get IPsec-specific opaque pointer */
155 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
156 if (tc == NULL) {
157 m_freem(m);
158 crypto_freereq(crp);
159 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160 ipcompstat.ipcomps_crypto++;
161 return ENOBUFS;
162 }
163 crdc = crp->crp_desc;
164
165 crdc->crd_skip = skip + hlen;
166 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167 crdc->crd_inject = skip;
168
169 tc->tc_ptr = 0;
170
171 /* Decompression operation */
172 crdc->crd_alg = sav->tdb_compalgxform->type;
173
174 /* Crypto operation descriptor */
175 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
177 crp->crp_buf = (caddr_t) m;
178 crp->crp_callback = ipcomp_input_cb;
179 crp->crp_sid = sav->tdb_cryptoid;
180 crp->crp_opaque = (caddr_t) tc;
181
182 /* These are passed as-is to the callback */
183 tc->tc_spi = sav->spi;
184 tc->tc_dst = sav->sah->saidx.dst;
185 tc->tc_proto = sav->sah->saidx.proto;
186 tc->tc_protoff = protoff;
187 tc->tc_skip = skip;
188
189 return crypto_dispatch(crp);
190 }
191
192 #ifdef INET6
193 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
194 if (saidx->dst.sa.sa_family == AF_INET6) { \
195 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196 } else { \
197 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
198 } \
199 } while (0)
200 #else
201 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
202 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
203 #endif
204
205 /*
206 * IPComp input callback from the crypto driver.
207 */
208 static int
209 ipcomp_input_cb(struct cryptop *crp)
210 {
211 struct cryptodesc *crd;
212 struct tdb_crypto *tc;
213 int skip, protoff;
214 struct mtag *mtag;
215 struct mbuf *m;
216 struct secasvar *sav;
217 struct secasindex *saidx;
218 int hlen = IPCOMP_HLENGTH, error, clen;
219 u_int8_t nproto;
220 caddr_t addr;
221
222 crd = crp->crp_desc;
223
224 tc = (struct tdb_crypto *) crp->crp_opaque;
225 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
226 skip = tc->tc_skip;
227 protoff = tc->tc_protoff;
228 mtag = (struct mtag *) tc->tc_ptr;
229 m = (struct mbuf *) crp->crp_buf;
230
231 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
232 if (sav == NULL) {
233 ipcompstat.ipcomps_notdb++;
234 DPRINTF(("%s: SA expired while in crypto\n", __func__));
235 error = ENOBUFS; /*XXX*/
236 goto bad;
237 }
238
239 saidx = &sav->sah->saidx;
240 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
241 saidx->dst.sa.sa_family == AF_INET6,
242 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
243
244 /* Check for crypto errors */
245 if (crp->crp_etype) {
246 /* Reset the session ID */
247 if (sav->tdb_cryptoid != 0)
248 sav->tdb_cryptoid = crp->crp_sid;
249
250 if (crp->crp_etype == EAGAIN) {
251 KEY_FREESAV(&sav);
252 return crypto_dispatch(crp);
253 }
254 ipcompstat.ipcomps_noxform++;
255 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
256 error = crp->crp_etype;
257 goto bad;
258 }
259 /* Shouldn't happen... */
260 if (m == NULL) {
261 ipcompstat.ipcomps_crypto++;
262 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
263 error = EINVAL;
264 goto bad;
265 }
266 ipcompstat.ipcomps_hist[sav->alg_comp]++;
267
268 clen = crp->crp_olen; /* Length of data after processing */
269
270 /* Release the crypto descriptors */
271 free(tc, M_XDATA), tc = NULL;
272 crypto_freereq(crp), crp = NULL;
273
274 /* In case it's not done already, adjust the size of the mbuf chain */
275 m->m_pkthdr.len = clen + hlen + skip;
276
277 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
278 ipcompstat.ipcomps_hdrops++; /*XXX*/
279 DPRINTF(("%s: m_pullup failed\n", __func__));
280 error = EINVAL; /*XXX*/
281 goto bad;
282 }
283
284 /* Keep the next protocol field */
285 addr = (caddr_t) mtod(m, struct ip *) + skip;
286 nproto = ((struct ipcomp *) addr)->comp_nxt;
287
288 /* Remove the IPCOMP header */
289 error = m_striphdr(m, skip, hlen);
290 if (error) {
291 ipcompstat.ipcomps_hdrops++;
292 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
293 ipsec_address(&sav->sah->saidx.dst),
294 (u_long) ntohl(sav->spi)));
295 goto bad;
296 }
297
298 /* Restore the Next Protocol field */
299 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
300
301 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
302
303 KEY_FREESAV(&sav);
304 return error;
305 bad:
306 if (sav)
307 KEY_FREESAV(&sav);
308 if (m)
309 m_freem(m);
310 if (tc != NULL)
311 free(tc, M_XDATA);
312 if (crp)
313 crypto_freereq(crp);
314 return error;
315 }
316
317 /*
318 * IPComp output routine, called by ipsec[46]_process_packet()
319 */
320 static int
321 ipcomp_output(
322 struct mbuf *m,
323 struct ipsecrequest *isr,
324 struct mbuf **mp,
325 int skip,
326 int protoff
327 )
328 {
329 struct secasvar *sav;
330 struct comp_algo *ipcompx;
331 int error, ralen, maxpacketsize;
332 struct cryptodesc *crdc;
333 struct cryptop *crp;
334 struct tdb_crypto *tc;
335
336 IPSEC_SPLASSERT_SOFTNET(__func__);
337
338 sav = isr->sav;
339 IPSEC_ASSERT(sav != NULL, ("null SA"));
340 ipcompx = sav->tdb_compalgxform;
341 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
342
343 /*
344 * Do not touch the packet in case our payload to compress
345 * is lower than the minimal threshold of the compression
346 * alogrithm. We will just send out the data uncompressed.
347 * See RFC 3173, 2.2. Non-Expansion Policy.
348 */
349 if (m->m_pkthdr.len <= ipcompx->minlen) {
350 ipcompstat.ipcomps_threshold++;
351 return ipsec_process_done(m, isr);
352 }
353
354 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
355 ipcompstat.ipcomps_output++;
356
357 /* Check for maximum packet size violations. */
358 switch (sav->sah->saidx.dst.sa.sa_family) {
359 #ifdef INET
360 case AF_INET:
361 maxpacketsize = IP_MAXPACKET;
362 break;
363 #endif /* INET */
364 #ifdef INET6
365 case AF_INET6:
366 maxpacketsize = IPV6_MAXPACKET;
367 break;
368 #endif /* INET6 */
369 default:
370 ipcompstat.ipcomps_nopf++;
371 DPRINTF(("%s: unknown/unsupported protocol family %d, "
372 "IPCA %s/%08lx\n", __func__,
373 sav->sah->saidx.dst.sa.sa_family,
374 ipsec_address(&sav->sah->saidx.dst),
375 (u_long) ntohl(sav->spi)));
376 error = EPFNOSUPPORT;
377 goto bad;
378 }
379 if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
380 ipcompstat.ipcomps_toobig++;
381 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
382 "(len %u, max len %u)\n", __func__,
383 ipsec_address(&sav->sah->saidx.dst),
384 (u_long) ntohl(sav->spi),
385 ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
386 error = EMSGSIZE;
387 goto bad;
388 }
389
390 /* Update the counters */
391 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
392
393 m = m_unshare(m, M_NOWAIT);
394 if (m == NULL) {
395 ipcompstat.ipcomps_hdrops++;
396 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
397 __func__, ipsec_address(&sav->sah->saidx.dst),
398 (u_long) ntohl(sav->spi)));
399 error = ENOBUFS;
400 goto bad;
401 }
402
403 /* Ok now, we can pass to the crypto processing. */
404
405 /* Get crypto descriptors */
406 crp = crypto_getreq(1);
407 if (crp == NULL) {
408 ipcompstat.ipcomps_crypto++;
409 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
410 error = ENOBUFS;
411 goto bad;
412 }
413 crdc = crp->crp_desc;
414
415 /* Compression descriptor */
416 crdc->crd_skip = skip;
417 crdc->crd_len = ralen;
418 crdc->crd_flags = CRD_F_COMP;
419 crdc->crd_inject = skip;
420
421 /* Compression operation */
422 crdc->crd_alg = ipcompx->type;
423
424 /* IPsec-specific opaque crypto info */
425 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
426 M_XDATA, M_NOWAIT|M_ZERO);
427 if (tc == NULL) {
428 ipcompstat.ipcomps_crypto++;
429 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
430 crypto_freereq(crp);
431 error = ENOBUFS;
432 goto bad;
433 }
434
435 tc->tc_isr = isr;
436 tc->tc_spi = sav->spi;
437 tc->tc_dst = sav->sah->saidx.dst;
438 tc->tc_proto = sav->sah->saidx.proto;
439 tc->tc_protoff = protoff;
440 tc->tc_skip = skip;
441
442 /* Crypto operation descriptor */
443 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
444 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
445 crp->crp_buf = (caddr_t) m;
446 crp->crp_callback = ipcomp_output_cb;
447 crp->crp_opaque = (caddr_t) tc;
448 crp->crp_sid = sav->tdb_cryptoid;
449
450 return crypto_dispatch(crp);
451 bad:
452 if (m)
453 m_freem(m);
454 return (error);
455 }
456
457 /*
458 * IPComp output callback from the crypto driver.
459 */
460 static int
461 ipcomp_output_cb(struct cryptop *crp)
462 {
463 struct tdb_crypto *tc;
464 struct ipsecrequest *isr;
465 struct secasvar *sav;
466 struct mbuf *m;
467 int error, skip;
468
469 tc = (struct tdb_crypto *) crp->crp_opaque;
470 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
471 m = (struct mbuf *) crp->crp_buf;
472 skip = tc->tc_skip;
473
474 isr = tc->tc_isr;
475 IPSECREQUEST_LOCK(isr);
476 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
477 if (sav == NULL) {
478 ipcompstat.ipcomps_notdb++;
479 DPRINTF(("%s: SA expired while in crypto\n", __func__));
480 error = ENOBUFS; /*XXX*/
481 goto bad;
482 }
483 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
484
485 /* Check for crypto errors */
486 if (crp->crp_etype) {
487 /* Reset the session ID */
488 if (sav->tdb_cryptoid != 0)
489 sav->tdb_cryptoid = crp->crp_sid;
490
491 if (crp->crp_etype == EAGAIN) {
492 KEY_FREESAV(&sav);
493 IPSECREQUEST_UNLOCK(isr);
494 return crypto_dispatch(crp);
495 }
496 ipcompstat.ipcomps_noxform++;
497 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
498 error = crp->crp_etype;
499 goto bad;
500 }
501 /* Shouldn't happen... */
502 if (m == NULL) {
503 ipcompstat.ipcomps_crypto++;
504 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
505 error = EINVAL;
506 goto bad;
507 }
508 ipcompstat.ipcomps_hist[sav->alg_comp]++;
509
510 if (crp->crp_ilen - skip > crp->crp_olen) {
511 struct mbuf *mo;
512 struct ipcomp *ipcomp;
513 int roff;
514 uint8_t prot;
515
516 /* Compression helped, inject IPCOMP header. */
517 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
518 if (mo == NULL) {
519 ipcompstat.ipcomps_wrap++;
520 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
521 __func__, ipsec_address(&sav->sah->saidx.dst),
522 (u_long) ntohl(sav->spi)));
523 error = ENOBUFS;
524 goto bad;
525 }
526 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
527
528 /* Initialize the IPCOMP header */
529 /* XXX alignment always correct? */
530 switch (sav->sah->saidx.dst.sa.sa_family) {
531 #ifdef INET
532 case AF_INET:
533 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
534 break;
535 #endif /* INET */
536 #ifdef INET6
537 case AF_INET6:
538 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
539 break;
540 #endif
541 }
542 ipcomp->comp_flags = 0;
543 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
544
545 /* Fix Next Protocol in IPv4/IPv6 header */
546 prot = IPPROTO_IPCOMP;
547 m_copyback(m, tc->tc_protoff, sizeof(u_int8_t),
548 (u_char *)&prot);
549
550 /* Adjust the length in the IP header */
551 switch (sav->sah->saidx.dst.sa.sa_family) {
552 #ifdef INET
553 case AF_INET:
554 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
555 break;
556 #endif /* INET */
557 #ifdef INET6
558 case AF_INET6:
559 mtod(m, struct ip6_hdr *)->ip6_plen =
560 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
561 break;
562 #endif /* INET6 */
563 default:
564 ipcompstat.ipcomps_nopf++;
565 DPRINTF(("%s: unknown/unsupported protocol "
566 "family %d, IPCA %s/%08lx\n", __func__,
567 sav->sah->saidx.dst.sa.sa_family,
568 ipsec_address(&sav->sah->saidx.dst),
569 (u_long) ntohl(sav->spi)));
570 error = EPFNOSUPPORT;
571 goto bad;
572 }
573 } else {
574 /* Compression was useless, we have lost time. */
575 ipcompstat.ipcomps_uncompr++;
576 DPRINTF(("%s: compressions was useless %d - %d <= %d\n",
577 __func__, crp->crp_ilen, skip, crp->crp_olen));
578 /* XXX remember state to not compress the next couple
579 * of packets, RFC 3173, 2.2. Non-Expansion Policy */
580 }
581
582 /* Release the crypto descriptor */
583 free(tc, M_XDATA);
584 crypto_freereq(crp);
585
586 /* NB: m is reclaimed by ipsec_process_done. */
587 error = ipsec_process_done(m, isr);
588 KEY_FREESAV(&sav);
589 IPSECREQUEST_UNLOCK(isr);
590 return error;
591 bad:
592 if (sav)
593 KEY_FREESAV(&sav);
594 IPSECREQUEST_UNLOCK(isr);
595 if (m)
596 m_freem(m);
597 free(tc, M_XDATA);
598 crypto_freereq(crp);
599 return error;
600 }
601
602 static struct xformsw ipcomp_xformsw = {
603 XF_IPCOMP, XFT_COMP, "IPcomp",
604 ipcomp_init, ipcomp_zeroize, ipcomp_input,
605 ipcomp_output
606 };
607
608 static void
609 ipcomp_attach(void)
610 {
611
612 ipcompstat.version = IPCOMPSTAT_VERSION;
613 xform_register(&ipcomp_xformsw);
614 }
615 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
Cache object: 3e60d5aa0066fcb8021522a075bae135
|