Add journaling restart support, required to produce a robust journaling
[dragonfly.git] / sys / netproto / ipsec / xform_ipcomp.c
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.7 2005/06/10 23:59:31 dillon 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@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 /* IP payload compression protocol (IPComp), see RFC 2393 */
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35
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
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
46 #include <netinet/ip.h>
47 #include <netinet/ip_var.h>
48
49 #include <net/route.h>
50 #include <netproto/ipsec/ipsec.h>
51 #include <netproto/ipsec/xform.h>
52
53 #ifdef INET6
54 #include <netinet/ip6.h>
55 #include <netproto/ipsec/ipsec6.h>
56 #endif
57
58 #include <netproto/ipsec/ipcomp.h>
59 #include <netproto/ipsec/ipcomp_var.h>
60
61 #include <netproto/ipsec/key.h>
62 #include <netproto/ipsec/key_debug.h>
63
64 #include <opencrypto/cryptodev.h>
65 #include <opencrypto/deflate.h>
66 #include <opencrypto/xform.h>
67
68 int     ipcomp_enable = 0;
69 struct  ipcompstat ipcompstat;
70
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, "");
76
77 static int ipcomp_input_cb(struct cryptop *crp);
78 static int ipcomp_output_cb(struct cryptop *crp);
79
80 struct comp_algo *
81 ipcomp_algorithm_lookup(int alg)
82 {
83         if (alg >= IPCOMP_ALG_MAX)
84                 return NULL;
85         switch (alg) {
86         case SADB_X_CALG_DEFLATE:
87                 return &comp_algo_deflate;
88         }
89         return NULL;
90 }
91
92 /*
93  * ipcomp_init() is called when an CPI is being set up.
94  */
95 static int
96 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
97 {
98         struct comp_algo *tcomp;
99         struct cryptoini cric;
100
101         /* NB: algorithm really comes in alg_enc and not alg_comp! */
102         tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
103         if (tcomp == NULL) {
104                 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
105                          sav->alg_comp));
106                 return EINVAL;
107         }
108         sav->alg_comp = sav->alg_enc;           /* set for doing histogram */
109         sav->tdb_xform = xsp;
110         sav->tdb_compalgxform = tcomp;
111
112         /* Initialize crypto session */
113         bzero(&cric, sizeof (cric));
114         cric.cri_alg = sav->tdb_compalgxform->type;
115
116         return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support);
117 }
118
119 /*
120  * ipcomp_zeroize() used when IPCA is deleted
121  */
122 static int
123 ipcomp_zeroize(struct secasvar *sav)
124 {
125         int err;
126
127         err = crypto_freesession(sav->tdb_cryptoid);
128         sav->tdb_cryptoid = 0;
129         return err;
130 }
131
132 /*
133  * ipcomp_input() gets called to uncompress an input packet
134  */
135 static int
136 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
137 {
138         struct tdb_crypto *tc;
139         struct cryptodesc *crdc;
140         struct cryptop *crp;
141         int hlen = IPCOMP_HLENGTH;
142
143         /* Get crypto descriptors */
144         crp = crypto_getreq(1);
145         if (crp == NULL) {
146                 m_freem(m);
147                 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
148                 ipcompstat.ipcomps_crypto++;
149                 return ENOBUFS;
150         }
151         /* Get IPsec-specific opaque pointer */
152         tc = malloc(sizeof (*tc), M_XDATA, 
153                     M_INTWAIT | M_ZERO | M_NULLOK);
154         if (tc == NULL) {
155                 m_freem(m);
156                 crypto_freereq(crp);
157                 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
158                 ipcompstat.ipcomps_crypto++;
159                 return ENOBUFS;
160         }
161         crdc = crp->crp_desc;
162
163         crdc->crd_skip = skip + hlen;
164         crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
165         crdc->crd_inject = skip;
166
167         tc->tc_ptr = 0;
168
169         /* Decompression operation */
170         crdc->crd_alg = sav->tdb_compalgxform->type;
171
172         /* Crypto operation descriptor */
173         crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
174         crp->crp_flags = CRYPTO_F_IMBUF;
175         crp->crp_buf = (caddr_t) m;
176         crp->crp_callback = ipcomp_input_cb;
177         crp->crp_sid = sav->tdb_cryptoid;
178         crp->crp_opaque = (caddr_t) tc;
179
180         /* These are passed as-is to the callback */
181         tc->tc_spi = sav->spi;
182         tc->tc_dst = sav->sah->saidx.dst;
183         tc->tc_proto = sav->sah->saidx.proto;
184         tc->tc_protoff = protoff;
185         tc->tc_skip = skip;
186
187         return crypto_dispatch(crp);
188 }
189
190 #ifdef INET6
191 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do {              \
192         if (saidx->dst.sa.sa_family == AF_INET6) {                           \
193                 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
194         } else {                                                             \
195                 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
196         }                                                                    \
197 } while (0)
198 #else
199 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag)                   \
200         (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
201 #endif
202
203 /*
204  * IPComp input callback from the crypto driver.
205  */
206 static int
207 ipcomp_input_cb(struct cryptop *crp)
208 {
209         struct cryptodesc *crd;
210         struct tdb_crypto *tc;
211         int skip, protoff;
212         struct mtag *mtag;
213         struct mbuf *m;
214         struct secasvar *sav;
215         struct secasindex *saidx;
216         int hlen = IPCOMP_HLENGTH, error, clen;
217         u_int8_t nproto;
218         caddr_t addr;
219
220         crd = crp->crp_desc;
221
222         tc = (struct tdb_crypto *) crp->crp_opaque;
223         KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!"));
224         skip = tc->tc_skip;
225         protoff = tc->tc_protoff;
226         mtag = (struct mtag *) tc->tc_ptr;
227         m = (struct mbuf *) crp->crp_buf;
228
229         crit_enter();
230
231         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
232         if (sav == NULL) {
233                 ipcompstat.ipcomps_notdb++;
234                 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
235                 error = ENOBUFS;                /*XXX*/
236                 goto bad;
237         }
238
239         saidx = &sav->sah->saidx;
240         KASSERT(saidx->dst.sa.sa_family == AF_INET ||
241                 saidx->dst.sa.sa_family == AF_INET6,
242                 ("ah_input_cb: unexpected protocol family %u",
243                  saidx->dst.sa.sa_family));
244
245         /* Check for crypto errors */
246         if (crp->crp_etype) {
247                 /* Reset the session ID */
248                 if (sav->tdb_cryptoid != 0)
249                         sav->tdb_cryptoid = crp->crp_sid;
250
251                 if (crp->crp_etype == EAGAIN) {
252                         KEY_FREESAV(&sav);
253                         crit_exit();
254                         return crypto_dispatch(crp);
255                 }
256
257                 ipcompstat.ipcomps_noxform++;
258                 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
259                 error = crp->crp_etype;
260                 goto bad;
261         }
262         /* Shouldn't happen... */
263         if (m == NULL) {
264                 ipcompstat.ipcomps_crypto++;
265                 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
266                 error = EINVAL;
267                 goto bad;
268         }
269         ipcompstat.ipcomps_hist[sav->alg_comp]++;
270
271         clen = crp->crp_olen;           /* Length of data after processing */
272
273         /* Release the crypto descriptors */
274         free(tc, M_XDATA), tc = NULL;
275         crypto_freereq(crp), crp = NULL;
276
277         /* In case it's not done already, adjust the size of the mbuf chain */
278         m->m_pkthdr.len = clen + hlen + skip;
279
280         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
281                 ipcompstat.ipcomps_hdrops++;            /*XXX*/
282                 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
283                 error = EINVAL;                         /*XXX*/
284                 goto bad;
285         }
286
287         /* Keep the next protocol field */
288         addr = (caddr_t) mtod(m, struct ip *) + skip;
289         nproto = ((struct ipcomp *) addr)->comp_nxt;
290
291         /* Remove the IPCOMP header */
292         error = m_striphdr(m, skip, hlen);
293         if (error) {
294                 ipcompstat.ipcomps_hdrops++;
295                 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
296                          ipsec_address(&sav->sah->saidx.dst),
297                          (u_long) ntohl(sav->spi)));
298                 goto bad;
299         }
300
301         /* Restore the Next Protocol field */
302         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
303
304         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
305
306         KEY_FREESAV(&sav);
307         crit_exit();
308         return error;
309 bad:
310         if (sav)
311                 KEY_FREESAV(&sav);
312         crit_exit();
313         if (m)
314                 m_freem(m);
315         if (tc != NULL)
316                 free(tc, M_XDATA);
317         if (crp)
318                 crypto_freereq(crp);
319         return error;
320 }
321
322 /*
323  * IPComp output routine, called by ipsec[46]_process_packet()
324  */
325 static int
326 ipcomp_output(
327         struct mbuf *m,
328         struct ipsecrequest *isr,
329         struct mbuf **mp,
330         int skip,
331         int protoff
332 )
333 {
334         struct secasvar *sav;
335         struct comp_algo *ipcompx;
336         int error, ralen, hlen, maxpacketsize, roff;
337         u_int8_t prot;
338         struct cryptodesc *crdc;
339         struct cryptop *crp;
340         struct tdb_crypto *tc;
341         struct mbuf *mo;
342         struct ipcomp *ipcomp;
343
344         sav = isr->sav;
345         KASSERT(sav != NULL, ("ipcomp_output: null SA"));
346         ipcompx = sav->tdb_compalgxform;
347         KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
348
349         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
350         hlen = IPCOMP_HLENGTH;
351
352         ipcompstat.ipcomps_output++;
353
354         /* Check for maximum packet size violations. */
355         switch (sav->sah->saidx.dst.sa.sa_family) {
356 #ifdef INET
357         case AF_INET:
358                 maxpacketsize =  IP_MAXPACKET;
359                 break;
360 #endif /* INET */
361 #ifdef INET6
362         case AF_INET6:
363                 maxpacketsize =  IPV6_MAXPACKET;
364                 break;
365 #endif /* INET6 */
366         default:
367                 ipcompstat.ipcomps_nopf++;
368                 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
369                     ", IPCA %s/%08lx\n", 
370                     sav->sah->saidx.dst.sa.sa_family,
371                     ipsec_address(&sav->sah->saidx.dst),
372                     (u_long) ntohl(sav->spi)));
373                 error = EPFNOSUPPORT;
374                 goto bad;
375         }
376         if (skip + hlen + ralen > maxpacketsize) {
377                 ipcompstat.ipcomps_toobig++;
378                 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
379                     "(len %u, max len %u)\n",
380                     ipsec_address(&sav->sah->saidx.dst),
381                     (u_long) ntohl(sav->spi),
382                     skip + hlen + ralen, maxpacketsize));
383                 error = EMSGSIZE;
384                 goto bad;
385         }
386
387         /* Update the counters */
388         ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
389
390         m = m_clone(m);
391         if (m == NULL) {
392                 ipcompstat.ipcomps_hdrops++;
393                 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n", 
394                     ipsec_address(&sav->sah->saidx.dst),
395                     (u_long) ntohl(sav->spi)));
396                 error = ENOBUFS;
397                 goto bad;
398         }
399
400         /* Inject IPCOMP header */
401         mo = m_makespace(m, skip, hlen, &roff);
402         if (mo == NULL) {
403                 ipcompstat.ipcomps_wrap++;
404                 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
405                     "IPCA %s/%08lx\n",
406                     ipsec_address(&sav->sah->saidx.dst),
407                     (u_long) ntohl(sav->spi)));
408                 error = ENOBUFS;
409                 goto bad;
410         }
411         ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
412
413         /* Initialize the IPCOMP header */
414         /* XXX alignment always correct? */
415         switch (sav->sah->saidx.dst.sa.sa_family) {
416 #ifdef INET
417         case AF_INET:
418                 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
419                 break;
420 #endif /* INET */
421 #ifdef INET6
422         case AF_INET6:
423                 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
424                 break;
425 #endif
426         }
427         ipcomp->comp_flags = 0;
428         ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
429
430         /* Fix Next Protocol in IPv4/IPv6 header */
431         prot = IPPROTO_IPCOMP;
432         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
433
434         /* Ok now, we can pass to the crypto processing */
435
436         /* Get crypto descriptors */
437         crp = crypto_getreq(1);
438         if (crp == NULL) {
439                 ipcompstat.ipcomps_crypto++;
440                 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
441                 error = ENOBUFS;
442                 goto bad;
443         }
444         crdc = crp->crp_desc;
445
446         /* Compression descriptor */
447         crdc->crd_skip = skip + hlen;
448         crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
449         crdc->crd_flags = CRD_F_COMP;
450         crdc->crd_inject = skip + hlen;
451
452         /* Compression operation */
453         crdc->crd_alg = ipcompx->type;
454
455         /* IPsec-specific opaque crypto info */
456         tc = malloc(sizeof(struct tdb_crypto),
457                     M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
458         if (tc == NULL) {
459                 ipcompstat.ipcomps_crypto++;
460                 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
461                 crypto_freereq(crp);
462                 error = ENOBUFS;
463                 goto bad;
464         }
465
466         tc->tc_isr = isr;
467         tc->tc_spi = sav->spi;
468         tc->tc_dst = sav->sah->saidx.dst;
469         tc->tc_proto = sav->sah->saidx.proto;
470         tc->tc_skip = skip + hlen;
471
472         /* Crypto operation descriptor */
473         crp->crp_ilen = m->m_pkthdr.len;        /* Total input length */
474         crp->crp_flags = CRYPTO_F_IMBUF;
475         crp->crp_buf = (caddr_t) m;
476         crp->crp_callback = ipcomp_output_cb;
477         crp->crp_opaque = (caddr_t) tc;
478         crp->crp_sid = sav->tdb_cryptoid;
479
480         return crypto_dispatch(crp);
481 bad:
482         if (m)
483                 m_freem(m);
484         return (error);
485 }
486
487 /*
488  * IPComp output callback from the crypto driver.
489  */
490 static int
491 ipcomp_output_cb(struct cryptop *crp)
492 {
493         struct tdb_crypto *tc;
494         struct ipsecrequest *isr;
495         struct secasvar *sav;
496         struct mbuf *m;
497         int error, skip, rlen;
498
499         tc = (struct tdb_crypto *) crp->crp_opaque;
500         KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!"));
501         m = (struct mbuf *) crp->crp_buf;
502         skip = tc->tc_skip;
503         rlen = crp->crp_ilen - skip;
504
505         crit_enter();
506
507         isr = tc->tc_isr;
508         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
509         if (sav == NULL) {
510                 ipcompstat.ipcomps_notdb++;
511                 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
512                 error = ENOBUFS;                /*XXX*/
513                 goto bad;
514         }
515         KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
516
517         /* Check for crypto errors */
518         if (crp->crp_etype) {
519                 /* Reset session ID */
520                 if (sav->tdb_cryptoid != 0)
521                         sav->tdb_cryptoid = crp->crp_sid;
522
523                 if (crp->crp_etype == EAGAIN) {
524                         KEY_FREESAV(&sav);
525                         crit_exit();
526                         return crypto_dispatch(crp);
527                 }
528                 ipcompstat.ipcomps_noxform++;
529                 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
530                 error = crp->crp_etype;
531                 goto bad;
532         }
533         /* Shouldn't happen... */
534         if (m == NULL) {
535                 ipcompstat.ipcomps_crypto++;
536                 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
537                 error = EINVAL;
538                 goto bad;
539         }
540         ipcompstat.ipcomps_hist[sav->alg_comp]++;
541
542         if (rlen > crp->crp_olen) {
543                 /* Adjust the length in the IP header */
544                 switch (sav->sah->saidx.dst.sa.sa_family) {
545 #ifdef INET
546                 case AF_INET:
547                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
548                         break;
549 #endif /* INET */
550 #ifdef INET6
551                 case AF_INET6:
552                         mtod(m, struct ip6_hdr *)->ip6_plen =
553                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
554                         break;
555 #endif /* INET6 */
556                 default:
557                         ipcompstat.ipcomps_nopf++;
558                         DPRINTF(("ipcomp_output: unknown/unsupported protocol "
559                             "family %d, IPCA %s/%08lx\n", 
560                             sav->sah->saidx.dst.sa.sa_family,
561                             ipsec_address(&sav->sah->saidx.dst), 
562                             (u_long) ntohl(sav->spi)));
563                         error = EPFNOSUPPORT;
564                         goto bad;
565                 }
566         } else {
567                 /* compression was useless, we have lost time */
568                 /* XXX add statistic */
569         }
570
571         /* Release the crypto descriptor */
572         free(tc, M_XDATA);
573         crypto_freereq(crp);
574
575         /* NB: m is reclaimed by ipsec_process_done. */
576         error = ipsec_process_done(m, isr);
577         KEY_FREESAV(&sav);
578         crit_exit();
579         return error;
580 bad:
581         if (sav)
582                 KEY_FREESAV(&sav);
583         crit_exit();
584         if (m)
585                 m_freem(m);
586         free(tc, M_XDATA);
587         crypto_freereq(crp);
588         return error;
589 }
590
591 static struct xformsw ipcomp_xformsw = {
592         XF_IPCOMP,              XFT_COMP,               "IPcomp",
593         ipcomp_init,            ipcomp_zeroize,         ipcomp_input,
594         ipcomp_output
595 };
596
597 static void
598 ipcomp_attach(void)
599 {
600         xform_register(&ipcomp_xformsw);
601 }
602 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)