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.3 2003/08/07 21:17:37 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 = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
158 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
159 ipcompstat.ipcomps_crypto++;
162 crdc = crp->crp_desc;
164 crdc->crd_skip = skip + hlen;
165 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
166 crdc->crd_inject = skip;
170 /* Decompression operation */
171 crdc->crd_alg = sav->tdb_compalgxform->type;
173 /* Crypto operation descriptor */
174 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
175 crp->crp_flags = CRYPTO_F_IMBUF;
176 crp->crp_buf = (caddr_t) m;
177 crp->crp_callback = ipcomp_input_cb;
178 crp->crp_sid = sav->tdb_cryptoid;
179 crp->crp_opaque = (caddr_t) tc;
181 /* These are passed as-is to the callback */
182 tc->tc_spi = sav->spi;
183 tc->tc_dst = sav->sah->saidx.dst;
184 tc->tc_proto = sav->sah->saidx.proto;
185 tc->tc_protoff = protoff;
188 return crypto_dispatch(crp);
192 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
193 if (saidx->dst.sa.sa_family == AF_INET6) { \
194 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
200 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
201 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
205 * IPComp input callback from the crypto driver.
208 ipcomp_input_cb(struct cryptop *crp)
210 struct cryptodesc *crd;
211 struct tdb_crypto *tc;
215 struct secasvar *sav;
216 struct secasindex *saidx;
217 int s, hlen = IPCOMP_HLENGTH, error, clen;
223 tc = (struct tdb_crypto *) crp->crp_opaque;
224 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
226 protoff = tc->tc_protoff;
227 mtag = (struct mtag *) tc->tc_ptr;
228 m = (struct mbuf *) crp->crp_buf;
232 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
234 ipcompstat.ipcomps_notdb++;
235 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
236 error = ENOBUFS; /*XXX*/
240 saidx = &sav->sah->saidx;
241 KASSERT(saidx->dst.sa.sa_family == AF_INET ||
242 saidx->dst.sa.sa_family == AF_INET6,
243 ("ah_input_cb: unexpected protocol family %u",
244 saidx->dst.sa.sa_family));
246 /* Check for crypto errors */
247 if (crp->crp_etype) {
248 /* Reset the session ID */
249 if (sav->tdb_cryptoid != 0)
250 sav->tdb_cryptoid = crp->crp_sid;
252 if (crp->crp_etype == EAGAIN) {
255 return crypto_dispatch(crp);
258 ipcompstat.ipcomps_noxform++;
259 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
260 error = crp->crp_etype;
263 /* Shouldn't happen... */
265 ipcompstat.ipcomps_crypto++;
266 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
270 ipcompstat.ipcomps_hist[sav->alg_comp]++;
272 clen = crp->crp_olen; /* Length of data after processing */
274 /* Release the crypto descriptors */
275 free(tc, M_XDATA), tc = NULL;
276 crypto_freereq(crp), crp = NULL;
278 /* In case it's not done already, adjust the size of the mbuf chain */
279 m->m_pkthdr.len = clen + hlen + skip;
281 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
282 ipcompstat.ipcomps_hdrops++; /*XXX*/
283 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
284 error = EINVAL; /*XXX*/
288 /* Keep the next protocol field */
289 addr = (caddr_t) mtod(m, struct ip *) + skip;
290 nproto = ((struct ipcomp *) addr)->comp_nxt;
292 /* Remove the IPCOMP header */
293 error = m_striphdr(m, skip, hlen);
295 ipcompstat.ipcomps_hdrops++;
296 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
297 ipsec_address(&sav->sah->saidx.dst),
298 (u_long) ntohl(sav->spi)));
302 /* Restore the Next Protocol field */
303 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
305 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
324 * IPComp output routine, called by ipsec[46]_process_packet()
329 struct ipsecrequest *isr,
335 struct secasvar *sav;
336 struct comp_algo *ipcompx;
337 int error, ralen, hlen, maxpacketsize, roff;
339 struct cryptodesc *crdc;
341 struct tdb_crypto *tc;
343 struct ipcomp *ipcomp;
345 SPLASSERT(net, "ipcomp_output");
348 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
349 ipcompx = sav->tdb_compalgxform;
350 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
352 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
353 hlen = IPCOMP_HLENGTH;
355 ipcompstat.ipcomps_output++;
357 /* Check for maximum packet size violations. */
358 switch (sav->sah->saidx.dst.sa.sa_family) {
361 maxpacketsize = IP_MAXPACKET;
366 maxpacketsize = IPV6_MAXPACKET;
370 ipcompstat.ipcomps_nopf++;
371 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
373 sav->sah->saidx.dst.sa.sa_family,
374 ipsec_address(&sav->sah->saidx.dst),
375 (u_long) ntohl(sav->spi)));
376 error = EPFNOSUPPORT;
379 if (skip + hlen + ralen > maxpacketsize) {
380 ipcompstat.ipcomps_toobig++;
381 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
382 "(len %u, max len %u)\n",
383 ipsec_address(&sav->sah->saidx.dst),
384 (u_long) ntohl(sav->spi),
385 skip + hlen + ralen, maxpacketsize));
390 /* Update the counters */
391 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
395 ipcompstat.ipcomps_hdrops++;
396 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
397 ipsec_address(&sav->sah->saidx.dst),
398 (u_long) ntohl(sav->spi)));
403 /* Inject IPCOMP header */
404 mo = m_makespace(m, skip, hlen, &roff);
406 ipcompstat.ipcomps_wrap++;
407 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
409 ipsec_address(&sav->sah->saidx.dst),
410 (u_long) ntohl(sav->spi)));
414 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
416 /* Initialize the IPCOMP header */
417 /* XXX alignment always correct? */
418 switch (sav->sah->saidx.dst.sa.sa_family) {
421 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
426 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
430 ipcomp->comp_flags = 0;
431 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
433 /* Fix Next Protocol in IPv4/IPv6 header */
434 prot = IPPROTO_IPCOMP;
435 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
437 /* Ok now, we can pass to the crypto processing */
439 /* Get crypto descriptors */
440 crp = crypto_getreq(1);
442 ipcompstat.ipcomps_crypto++;
443 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
447 crdc = crp->crp_desc;
449 /* Compression descriptor */
450 crdc->crd_skip = skip + hlen;
451 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
452 crdc->crd_flags = CRD_F_COMP;
453 crdc->crd_inject = skip + hlen;
455 /* Compression operation */
456 crdc->crd_alg = ipcompx->type;
458 /* IPsec-specific opaque crypto info */
459 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
460 M_XDATA, M_NOWAIT|M_ZERO);
462 ipcompstat.ipcomps_crypto++;
463 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
470 tc->tc_spi = sav->spi;
471 tc->tc_dst = sav->sah->saidx.dst;
472 tc->tc_proto = sav->sah->saidx.proto;
473 tc->tc_skip = skip + hlen;
475 /* Crypto operation descriptor */
476 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
477 crp->crp_flags = CRYPTO_F_IMBUF;
478 crp->crp_buf = (caddr_t) m;
479 crp->crp_callback = ipcomp_output_cb;
480 crp->crp_opaque = (caddr_t) tc;
481 crp->crp_sid = sav->tdb_cryptoid;
483 return crypto_dispatch(crp);
491 * IPComp output callback from the crypto driver.
494 ipcomp_output_cb(struct cryptop *crp)
496 struct tdb_crypto *tc;
497 struct ipsecrequest *isr;
498 struct secasvar *sav;
500 int s, error, skip, rlen;
502 tc = (struct tdb_crypto *) crp->crp_opaque;
503 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
504 m = (struct mbuf *) crp->crp_buf;
506 rlen = crp->crp_ilen - skip;
511 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
513 ipcompstat.ipcomps_notdb++;
514 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
515 error = ENOBUFS; /*XXX*/
518 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
520 /* Check for crypto errors */
521 if (crp->crp_etype) {
522 /* Reset session ID */
523 if (sav->tdb_cryptoid != 0)
524 sav->tdb_cryptoid = crp->crp_sid;
526 if (crp->crp_etype == EAGAIN) {
529 return crypto_dispatch(crp);
531 ipcompstat.ipcomps_noxform++;
532 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
533 error = crp->crp_etype;
536 /* Shouldn't happen... */
538 ipcompstat.ipcomps_crypto++;
539 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
543 ipcompstat.ipcomps_hist[sav->alg_comp]++;
545 if (rlen > crp->crp_olen) {
546 /* Adjust the length in the IP header */
547 switch (sav->sah->saidx.dst.sa.sa_family) {
550 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
555 mtod(m, struct ip6_hdr *)->ip6_plen =
556 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
560 ipcompstat.ipcomps_nopf++;
561 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
562 "family %d, IPCA %s/%08lx\n",
563 sav->sah->saidx.dst.sa.sa_family,
564 ipsec_address(&sav->sah->saidx.dst),
565 (u_long) ntohl(sav->spi)));
566 error = EPFNOSUPPORT;
570 /* compression was useless, we have lost time */
571 /* XXX add statistic */
574 /* Release the crypto descriptor */
578 /* NB: m is reclaimed by ipsec_process_done. */
579 error = ipsec_process_done(m, isr);
594 static struct xformsw ipcomp_xformsw = {
595 XF_IPCOMP, XFT_COMP, "IPcomp",
596 ipcomp_init, ipcomp_zeroize, ipcomp_input,
603 xform_register(&ipcomp_xformsw);
605 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)