Dump entries can have the same timestamp, so accept those as well.
[dragonfly.git] / sys / netproto / ipsec / xform_ipcomp.c
blob8a8ab620518468c910f55b1aa267e7d4da45a503
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.10 2006/10/12 01:32:51 hsu Exp $ */
3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
5 /*
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
10 * are met:
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 */
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/mbuf.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
43 #include <sys/thread2.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
50 #include <net/route.h>
51 #include <netproto/ipsec/ipsec.h>
52 #include <netproto/ipsec/xform.h>
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #include <netproto/ipsec/ipsec6.h>
57 #endif
59 #include <netproto/ipsec/ipcomp.h>
60 #include <netproto/ipsec/ipcomp_var.h>
62 #include <netproto/ipsec/key.h>
63 #include <netproto/ipsec/key_debug.h>
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/deflate.h>
67 #include <opencrypto/xform.h>
69 int ipcomp_enable = 0;
70 struct ipcompstat ipcompstat;
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, "");
78 static int ipcomp_input_cb(struct cryptop *crp);
79 static int ipcomp_output_cb(struct cryptop *crp);
81 struct comp_algo *
82 ipcomp_algorithm_lookup(int alg)
84 if (alg >= IPCOMP_ALG_MAX)
85 return NULL;
86 switch (alg) {
87 case SADB_X_CALG_DEFLATE:
88 return &comp_algo_deflate;
90 return NULL;
94 * ipcomp_init() is called when an CPI is being set up.
96 static int
97 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
99 struct comp_algo *tcomp;
100 struct cryptoini cric;
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(("ipcomp_init: unsupported compression algorithm %d\n",
106 sav->alg_comp));
107 return EINVAL;
109 sav->alg_comp = sav->alg_enc; /* set for doing histogram */
110 sav->tdb_xform = xsp;
111 sav->tdb_compalgxform = tcomp;
113 /* Initialize crypto session */
114 bzero(&cric, sizeof (cric));
115 cric.cri_alg = sav->tdb_compalgxform->type;
117 return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
121 * ipcomp_zeroize() used when IPCA is deleted
123 static int
124 ipcomp_zeroize(struct secasvar *sav)
126 int err;
128 err = crypto_freesession(sav->tdb_cryptoid);
129 sav->tdb_cryptoid = 0;
130 return err;
134 * ipcomp_input() gets called to uncompress an input packet
136 static int
137 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
139 struct tdb_crypto *tc;
140 struct cryptodesc *crdc;
141 struct cryptop *crp;
142 int hlen = IPCOMP_HLENGTH;
144 /* Get crypto descriptors */
145 crp = crypto_getreq(1);
146 if (crp == NULL) {
147 m_freem(m);
148 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
149 ipcompstat.ipcomps_crypto++;
150 return ENOBUFS;
152 /* Get IPsec-specific opaque pointer */
153 tc = kmalloc(sizeof (*tc), M_XDATA,
154 M_INTWAIT | M_ZERO | M_NULLOK);
155 if (tc == NULL) {
156 m_freem(m);
157 crypto_freereq(crp);
158 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
159 ipcompstat.ipcomps_crypto++;
160 return ENOBUFS;
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;
168 tc->tc_ptr = 0;
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;
186 tc->tc_skip = skip;
188 return crypto_dispatch(crp);
191 #ifdef INET6
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); \
195 } else { \
196 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
198 } while (0)
199 #else
200 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
201 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
202 #endif
205 * IPComp input callback from the crypto driver.
207 static int
208 ipcomp_input_cb(struct cryptop *crp)
210 struct cryptodesc *crd;
211 struct tdb_crypto *tc;
212 int skip, protoff;
213 struct mtag *mtag;
214 struct mbuf *m;
215 struct secasvar *sav;
216 struct secasindex *saidx;
217 int hlen = IPCOMP_HLENGTH, error, clen;
218 u_int8_t nproto;
219 caddr_t addr;
221 crd = crp->crp_desc;
223 tc = (struct tdb_crypto *) crp->crp_opaque;
224 KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
225 skip = tc->tc_skip;
226 protoff = tc->tc_protoff;
227 mtag = (struct mtag *) tc->tc_ptr;
228 m = (struct mbuf *) crp->crp_buf;
230 crit_enter();
232 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
233 if (sav == NULL) {
234 ipcompstat.ipcomps_notdb++;
235 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
236 error = ENOBUFS; /*XXX*/
237 goto bad;
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) {
253 KEY_FREESAV(&sav);
254 crit_exit();
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;
261 goto bad;
263 /* Shouldn't happen... */
264 if (m == NULL) {
265 ipcompstat.ipcomps_crypto++;
266 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
267 error = EINVAL;
268 goto bad;
270 ipcompstat.ipcomps_hist[sav->alg_comp]++;
272 clen = crp->crp_olen; /* Length of data after processing */
274 /* Release the crypto descriptors */
275 kfree(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*/
285 goto bad;
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);
294 if (error) {
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)));
299 goto bad;
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);
307 KEY_FREESAV(&sav);
308 crit_exit();
309 return error;
310 bad:
311 if (sav)
312 KEY_FREESAV(&sav);
313 crit_exit();
314 if (m)
315 m_freem(m);
316 if (tc != NULL)
317 kfree(tc, M_XDATA);
318 if (crp)
319 crypto_freereq(crp);
320 return error;
324 * IPComp output routine, called by ipsec[46]_process_packet()
326 static int
327 ipcomp_output(
328 struct mbuf *m,
329 struct ipsecrequest *isr,
330 struct mbuf **mp,
331 int skip,
332 int protoff
335 struct secasvar *sav;
336 struct comp_algo *ipcompx;
337 int error, ralen, hlen, maxpacketsize, roff;
338 u_int8_t prot;
339 struct cryptodesc *crdc;
340 struct cryptop *crp;
341 struct tdb_crypto *tc;
342 struct mbuf *mo;
343 struct ipcomp *ipcomp;
345 sav = isr->sav;
346 KASSERT(sav != NULL, ("ipcomp_output: null SA"));
347 ipcompx = sav->tdb_compalgxform;
348 KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
350 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
351 hlen = IPCOMP_HLENGTH;
353 ipcompstat.ipcomps_output++;
355 /* Check for maximum packet size violations. */
356 switch (sav->sah->saidx.dst.sa.sa_family) {
357 #ifdef INET
358 case AF_INET:
359 maxpacketsize = IP_MAXPACKET;
360 break;
361 #endif /* INET */
362 #ifdef INET6
363 case AF_INET6:
364 maxpacketsize = IPV6_MAXPACKET;
365 break;
366 #endif /* INET6 */
367 default:
368 ipcompstat.ipcomps_nopf++;
369 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
370 ", IPCA %s/%08lx\n",
371 sav->sah->saidx.dst.sa.sa_family,
372 ipsec_address(&sav->sah->saidx.dst),
373 (u_long) ntohl(sav->spi)));
374 error = EPFNOSUPPORT;
375 goto bad;
377 if (skip + hlen + ralen > maxpacketsize) {
378 ipcompstat.ipcomps_toobig++;
379 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
380 "(len %u, max len %u)\n",
381 ipsec_address(&sav->sah->saidx.dst),
382 (u_long) ntohl(sav->spi),
383 skip + hlen + ralen, maxpacketsize));
384 error = EMSGSIZE;
385 goto bad;
388 /* Update the counters */
389 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
391 m = m_clone(m);
392 if (m == NULL) {
393 ipcompstat.ipcomps_hdrops++;
394 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
395 ipsec_address(&sav->sah->saidx.dst),
396 (u_long) ntohl(sav->spi)));
397 error = ENOBUFS;
398 goto bad;
401 /* Inject IPCOMP header */
402 mo = m_makespace(m, skip, hlen, &roff);
403 if (mo == NULL) {
404 ipcompstat.ipcomps_wrap++;
405 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
406 "IPCA %s/%08lx\n",
407 ipsec_address(&sav->sah->saidx.dst),
408 (u_long) ntohl(sav->spi)));
409 error = ENOBUFS;
410 goto bad;
412 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
414 /* Initialize the IPCOMP header */
415 /* XXX alignment always correct? */
416 switch (sav->sah->saidx.dst.sa.sa_family) {
417 #ifdef INET
418 case AF_INET:
419 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
420 break;
421 #endif /* INET */
422 #ifdef INET6
423 case AF_INET6:
424 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
425 break;
426 #endif
428 ipcomp->comp_flags = 0;
429 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
431 /* Fix Next Protocol in IPv4/IPv6 header */
432 prot = IPPROTO_IPCOMP;
433 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
435 /* Ok now, we can pass to the crypto processing */
437 /* Get crypto descriptors */
438 crp = crypto_getreq(1);
439 if (crp == NULL) {
440 ipcompstat.ipcomps_crypto++;
441 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
442 error = ENOBUFS;
443 goto bad;
445 crdc = crp->crp_desc;
447 /* Compression descriptor */
448 crdc->crd_skip = skip + hlen;
449 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
450 crdc->crd_flags = CRD_F_COMP;
451 crdc->crd_inject = skip + hlen;
453 /* Compression operation */
454 crdc->crd_alg = ipcompx->type;
456 /* IPsec-specific opaque crypto info */
457 tc = kmalloc(sizeof(struct tdb_crypto),
458 M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
459 if (tc == NULL) {
460 ipcompstat.ipcomps_crypto++;
461 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
462 crypto_freereq(crp);
463 error = ENOBUFS;
464 goto bad;
467 tc->tc_isr = isr;
468 tc->tc_spi = sav->spi;
469 tc->tc_dst = sav->sah->saidx.dst;
470 tc->tc_proto = sav->sah->saidx.proto;
471 tc->tc_skip = skip + hlen;
473 /* Crypto operation descriptor */
474 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
475 crp->crp_flags = CRYPTO_F_IMBUF;
476 crp->crp_buf = (caddr_t) m;
477 crp->crp_callback = ipcomp_output_cb;
478 crp->crp_opaque = (caddr_t) tc;
479 crp->crp_sid = sav->tdb_cryptoid;
481 return crypto_dispatch(crp);
482 bad:
483 if (m)
484 m_freem(m);
485 return (error);
489 * IPComp output callback from the crypto driver.
491 static int
492 ipcomp_output_cb(struct cryptop *crp)
494 struct tdb_crypto *tc;
495 struct ipsecrequest *isr;
496 struct secasvar *sav;
497 struct mbuf *m;
498 int error, skip, rlen;
500 tc = (struct tdb_crypto *) crp->crp_opaque;
501 KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
502 m = (struct mbuf *) crp->crp_buf;
503 skip = tc->tc_skip;
504 rlen = crp->crp_ilen - skip;
506 crit_enter();
508 isr = tc->tc_isr;
509 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
510 if (sav == NULL) {
511 ipcompstat.ipcomps_notdb++;
512 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
513 error = ENOBUFS; /*XXX*/
514 goto bad;
516 KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
518 /* Check for crypto errors */
519 if (crp->crp_etype) {
520 /* Reset session ID */
521 if (sav->tdb_cryptoid != 0)
522 sav->tdb_cryptoid = crp->crp_sid;
524 if (crp->crp_etype == EAGAIN) {
525 KEY_FREESAV(&sav);
526 crit_exit();
527 return crypto_dispatch(crp);
529 ipcompstat.ipcomps_noxform++;
530 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
531 error = crp->crp_etype;
532 goto bad;
534 /* Shouldn't happen... */
535 if (m == NULL) {
536 ipcompstat.ipcomps_crypto++;
537 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
538 error = EINVAL;
539 goto bad;
541 ipcompstat.ipcomps_hist[sav->alg_comp]++;
543 if (rlen > crp->crp_olen) {
544 /* Adjust the length in the IP header */
545 switch (sav->sah->saidx.dst.sa.sa_family) {
546 #ifdef INET
547 case AF_INET:
548 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
549 break;
550 #endif /* INET */
551 #ifdef INET6
552 case AF_INET6:
553 mtod(m, struct ip6_hdr *)->ip6_plen =
554 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
555 break;
556 #endif /* INET6 */
557 default:
558 ipcompstat.ipcomps_nopf++;
559 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
560 "family %d, IPCA %s/%08lx\n",
561 sav->sah->saidx.dst.sa.sa_family,
562 ipsec_address(&sav->sah->saidx.dst),
563 (u_long) ntohl(sav->spi)));
564 error = EPFNOSUPPORT;
565 goto bad;
567 } else {
568 /* compression was useless, we have lost time */
569 /* XXX add statistic */
572 /* Release the crypto descriptor */
573 kfree(tc, M_XDATA);
574 crypto_freereq(crp);
576 /* NB: m is reclaimed by ipsec_process_done. */
577 error = ipsec_process_done(m, isr);
578 KEY_FREESAV(&sav);
579 crit_exit();
580 return error;
581 bad:
582 if (sav)
583 KEY_FREESAV(&sav);
584 crit_exit();
585 if (m)
586 m_freem(m);
587 kfree(tc, M_XDATA);
588 crypto_freereq(crp);
589 return error;
592 static struct xformsw ipcomp_xformsw = {
593 XF_IPCOMP, XFT_COMP, "IPcomp",
594 ipcomp_init, ipcomp_zeroize, ipcomp_input,
595 ipcomp_output
598 static void
599 ipcomp_attach(void)
601 xform_register(&ipcomp_xformsw);
603 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)