1 /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.2 2003/02/26 00:14:06 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/xform_ipcomp.c,v 1.4 2004/04/22 05:09:48 dillon Exp $ */
3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
6 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
32 /* IP payload compression protocol (IPComp), see RFC 2393 */
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
49 #include <net/route.h>
54 #include <netinet/ip6.h>
59 #include "ipcomp_var.h"
62 #include "key_debug.h"
64 #include <opencrypto/cryptodev.h>
65 #include <opencrypto/deflate.h>
66 #include <opencrypto/xform.h>
68 int ipcomp_enable = 0;
69 struct ipcompstat ipcompstat;
71 SYSCTL_DECL(_net_inet_ipcomp);
72 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO,
73 ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "");
74 SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
75 stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "");
77 static int ipcomp_input_cb(struct cryptop *crp);
78 static int ipcomp_output_cb(struct cryptop *crp);
81 ipcomp_algorithm_lookup(int alg)
83 if (alg >= IPCOMP_ALG_MAX)
86 case SADB_X_CALG_DEFLATE:
87 return &comp_algo_deflate;
93 * ipcomp_init() is called when an CPI is being set up.
96 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
98 struct comp_algo *tcomp;
99 struct cryptoini cric;
101 /* NB: algorithm really comes in alg_enc and not alg_comp! */
102 tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
104 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
108 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
109 sav->tdb_xform = xsp;
110 sav->tdb_compalgxform = tcomp;
112 /* Initialize crypto session */
113 bzero(&cric, sizeof (cric));
114 cric.cri_alg = sav->tdb_compalgxform->type;
116 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
120 * ipcomp_zeroize() used when IPCA is deleted
123 ipcomp_zeroize(struct secasvar *sav)
127 err = crypto_freesession(sav->tdb_cryptoid);
128 sav->tdb_cryptoid = 0;
133 * ipcomp_input() gets called to uncompress an input packet
136 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
138 struct tdb_crypto *tc;
139 struct cryptodesc *crdc;
141 int hlen = IPCOMP_HLENGTH;
143 SPLASSERT(net, "ipcomp_input");
145 /* Get crypto descriptors */
146 crp = crypto_getreq(1);
149 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
150 ipcompstat.ipcomps_crypto++;
153 /* Get IPsec-specific opaque pointer */
154 tc = malloc(sizeof (*tc), M_XDATA,
155 M_INTWAIT | M_ZERO | M_NULLOK);
159 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
160 ipcompstat.ipcomps_crypto++;
163 crdc = crp->crp_desc;
165 crdc->crd_skip = skip + hlen;
166 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167 crdc->crd_inject = skip;
171 /* Decompression operation */
172 crdc->crd_alg = sav->tdb_compalgxform->type;
174 /* Crypto operation descriptor */
175 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176 crp->crp_flags = CRYPTO_F_IMBUF;
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;
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;
189 return crypto_dispatch(crp);
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); \
197 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
201 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
202 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
206 * IPComp input callback from the crypto driver.
209 ipcomp_input_cb(struct cryptop *crp)
211 struct cryptodesc *crd;
212 struct tdb_crypto *tc;
216 struct secasvar *sav;
217 struct secasindex *saidx;
218 int s, hlen = IPCOMP_HLENGTH, error, clen;
224 tc = (struct tdb_crypto *) crp->crp_opaque;
225 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
227 protoff = tc->tc_protoff;
228 mtag = (struct mtag *) tc->tc_ptr;
229 m = (struct mbuf *) crp->crp_buf;
233 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
235 ipcompstat.ipcomps_notdb++;
236 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
237 error = ENOBUFS; /*XXX*/
241 saidx = &sav->sah->saidx;
242 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
243 saidx->dst.sa.sa_family == AF_INET6,
244 ("ah_input_cb: unexpected protocol family %u",
245 saidx->dst.sa.sa_family));
247 /* Check for crypto errors */
248 if (crp->crp_etype) {
249 /* Reset the session ID */
250 if (sav->tdb_cryptoid != 0)
251 sav->tdb_cryptoid = crp->crp_sid;
253 if (crp->crp_etype == EAGAIN) {
256 return crypto_dispatch(crp);
259 ipcompstat.ipcomps_noxform++;
260 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
261 error = crp->crp_etype;
264 /* Shouldn't happen... */
266 ipcompstat.ipcomps_crypto++;
267 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
271 ipcompstat.ipcomps_hist[sav->alg_comp]++;
273 clen = crp->crp_olen; /* Length of data after processing */
275 /* Release the crypto descriptors */
276 free(tc, M_XDATA), tc = NULL;
277 crypto_freereq(crp), crp = NULL;
279 /* In case it's not done already, adjust the size of the mbuf chain */
280 m->m_pkthdr.len = clen + hlen + skip;
282 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
283 ipcompstat.ipcomps_hdrops++; /*XXX*/
284 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
285 error = EINVAL; /*XXX*/
289 /* Keep the next protocol field */
290 addr = (caddr_t) mtod(m, struct ip *) + skip;
291 nproto = ((struct ipcomp *) addr)->comp_nxt;
293 /* Remove the IPCOMP header */
294 error = m_striphdr(m, skip, hlen);
296 ipcompstat.ipcomps_hdrops++;
297 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
298 ipsec_address(&sav->sah->saidx.dst),
299 (u_long) ntohl(sav->spi)));
303 /* Restore the Next Protocol field */
304 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
306 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
325 * IPComp output routine, called by ipsec[46]_process_packet()
330 struct ipsecrequest *isr,
336 struct secasvar *sav;
337 struct comp_algo *ipcompx;
338 int error, ralen, hlen, maxpacketsize, roff;
340 struct cryptodesc *crdc;
342 struct tdb_crypto *tc;
344 struct ipcomp *ipcomp;
346 SPLASSERT(net, "ipcomp_output");
349 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
350 ipcompx = sav->tdb_compalgxform;
351 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
353 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
354 hlen = IPCOMP_HLENGTH;
356 ipcompstat.ipcomps_output++;
358 /* Check for maximum packet size violations. */
359 switch (sav->sah->saidx.dst.sa.sa_family) {
362 maxpacketsize = IP_MAXPACKET;
367 maxpacketsize = IPV6_MAXPACKET;
371 ipcompstat.ipcomps_nopf++;
372 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
374 sav->sah->saidx.dst.sa.sa_family,
375 ipsec_address(&sav->sah->saidx.dst),
376 (u_long) ntohl(sav->spi)));
377 error = EPFNOSUPPORT;
380 if (skip + hlen + ralen > maxpacketsize) {
381 ipcompstat.ipcomps_toobig++;
382 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
383 "(len %u, max len %u)\n",
384 ipsec_address(&sav->sah->saidx.dst),
385 (u_long) ntohl(sav->spi),
386 skip + hlen + ralen, maxpacketsize));
391 /* Update the counters */
392 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
396 ipcompstat.ipcomps_hdrops++;
397 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
398 ipsec_address(&sav->sah->saidx.dst),
399 (u_long) ntohl(sav->spi)));
404 /* Inject IPCOMP header */
405 mo = m_makespace(m, skip, hlen, &roff);
407 ipcompstat.ipcomps_wrap++;
408 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
410 ipsec_address(&sav->sah->saidx.dst),
411 (u_long) ntohl(sav->spi)));
415 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
417 /* Initialize the IPCOMP header */
418 /* XXX alignment always correct? */
419 switch (sav->sah->saidx.dst.sa.sa_family) {
422 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
427 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
431 ipcomp->comp_flags = 0;
432 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
434 /* Fix Next Protocol in IPv4/IPv6 header */
435 prot = IPPROTO_IPCOMP;
436 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
438 /* Ok now, we can pass to the crypto processing */
440 /* Get crypto descriptors */
441 crp = crypto_getreq(1);
443 ipcompstat.ipcomps_crypto++;
444 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
448 crdc = crp->crp_desc;
450 /* Compression descriptor */
451 crdc->crd_skip = skip + hlen;
452 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
453 crdc->crd_flags = CRD_F_COMP;
454 crdc->crd_inject = skip + hlen;
456 /* Compression operation */
457 crdc->crd_alg = ipcompx->type;
459 /* IPsec-specific opaque crypto info */
460 tc = malloc(sizeof(struct tdb_crypto),
461 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
463 ipcompstat.ipcomps_crypto++;
464 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
471 tc->tc_spi = sav->spi;
472 tc->tc_dst = sav->sah->saidx.dst;
473 tc->tc_proto = sav->sah->saidx.proto;
474 tc->tc_skip = skip + hlen;
476 /* Crypto operation descriptor */
477 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
478 crp->crp_flags = CRYPTO_F_IMBUF;
479 crp->crp_buf = (caddr_t) m;
480 crp->crp_callback = ipcomp_output_cb;
481 crp->crp_opaque = (caddr_t) tc;
482 crp->crp_sid = sav->tdb_cryptoid;
484 return crypto_dispatch(crp);
492 * IPComp output callback from the crypto driver.
495 ipcomp_output_cb(struct cryptop *crp)
497 struct tdb_crypto *tc;
498 struct ipsecrequest *isr;
499 struct secasvar *sav;
501 int s, error, skip, rlen;
503 tc = (struct tdb_crypto *) crp->crp_opaque;
504 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
505 m = (struct mbuf *) crp->crp_buf;
507 rlen = crp->crp_ilen - skip;
512 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
514 ipcompstat.ipcomps_notdb++;
515 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
516 error = ENOBUFS; /*XXX*/
519 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
521 /* Check for crypto errors */
522 if (crp->crp_etype) {
523 /* Reset session ID */
524 if (sav->tdb_cryptoid != 0)
525 sav->tdb_cryptoid = crp->crp_sid;
527 if (crp->crp_etype == EAGAIN) {
530 return crypto_dispatch(crp);
532 ipcompstat.ipcomps_noxform++;
533 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
534 error = crp->crp_etype;
537 /* Shouldn't happen... */
539 ipcompstat.ipcomps_crypto++;
540 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
544 ipcompstat.ipcomps_hist[sav->alg_comp]++;
546 if (rlen > crp->crp_olen) {
547 /* Adjust the length in the IP header */
548 switch (sav->sah->saidx.dst.sa.sa_family) {
551 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
556 mtod(m, struct ip6_hdr *)->ip6_plen =
557 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
561 ipcompstat.ipcomps_nopf++;
562 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
563 "family %d, IPCA %s/%08lx\n",
564 sav->sah->saidx.dst.sa.sa_family,
565 ipsec_address(&sav->sah->saidx.dst),
566 (u_long) ntohl(sav->spi)));
567 error = EPFNOSUPPORT;
571 /* compression was useless, we have lost time */
572 /* XXX add statistic */
575 /* Release the crypto descriptor */
579 /* NB: m is reclaimed by ipsec_process_done. */
580 error = ipsec_process_done(m, isr);
595 static struct xformsw ipcomp_xformsw = {
596 XF_IPCOMP, XFT_COMP, "IPcomp",
597 ipcomp_init, ipcomp_zeroize, ipcomp_input,
604 xform_register(&ipcomp_xformsw);
606 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)