Update includes now that the Fast IPSec code has moved to netproto/ipsec.
[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.5 2004/10/15 22:59:10 hsu 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         SPLASSERT(net, "ipcomp_input");
144
145         /* Get crypto descriptors */
146         crp = crypto_getreq(1);
147         if (crp == NULL) {
148                 m_freem(m);
149                 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
150                 ipcompstat.ipcomps_crypto++;
151                 return ENOBUFS;
152         }
153         /* Get IPsec-specific opaque pointer */
154         tc = malloc(sizeof (*tc), M_XDATA, 
155                     M_INTWAIT | M_ZERO | M_NULLOK);
156         if (tc == NULL) {
157                 m_freem(m);
158                 crypto_freereq(crp);
159                 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
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;
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 s, 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         KASSERT(tc != NULL, ("ipcomp_input_cb: 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         s = splnet();
232
233         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
234         if (sav == NULL) {
235                 ipcompstat.ipcomps_notdb++;
236                 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
237                 error = ENOBUFS;                /*XXX*/
238                 goto bad;
239         }
240
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));
246
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;
252
253                 if (crp->crp_etype == EAGAIN) {
254                         KEY_FREESAV(&sav);
255                         splx(s);
256                         return crypto_dispatch(crp);
257                 }
258
259                 ipcompstat.ipcomps_noxform++;
260                 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype));
261                 error = crp->crp_etype;
262                 goto bad;
263         }
264         /* Shouldn't happen... */
265         if (m == NULL) {
266                 ipcompstat.ipcomps_crypto++;
267                 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
268                 error = EINVAL;
269                 goto bad;
270         }
271         ipcompstat.ipcomps_hist[sav->alg_comp]++;
272
273         clen = crp->crp_olen;           /* Length of data after processing */
274
275         /* Release the crypto descriptors */
276         free(tc, M_XDATA), tc = NULL;
277         crypto_freereq(crp), crp = NULL;
278
279         /* In case it's not done already, adjust the size of the mbuf chain */
280         m->m_pkthdr.len = clen + hlen + skip;
281
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*/
286                 goto bad;
287         }
288
289         /* Keep the next protocol field */
290         addr = (caddr_t) mtod(m, struct ip *) + skip;
291         nproto = ((struct ipcomp *) addr)->comp_nxt;
292
293         /* Remove the IPCOMP header */
294         error = m_striphdr(m, skip, hlen);
295         if (error) {
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)));
300                 goto bad;
301         }
302
303         /* Restore the Next Protocol field */
304         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
305
306         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
307
308         KEY_FREESAV(&sav);
309         splx(s);
310         return error;
311 bad:
312         if (sav)
313                 KEY_FREESAV(&sav);
314         splx(s);
315         if (m)
316                 m_freem(m);
317         if (tc != NULL)
318                 free(tc, M_XDATA);
319         if (crp)
320                 crypto_freereq(crp);
321         return error;
322 }
323
324 /*
325  * IPComp output routine, called by ipsec[46]_process_packet()
326  */
327 static int
328 ipcomp_output(
329         struct mbuf *m,
330         struct ipsecrequest *isr,
331         struct mbuf **mp,
332         int skip,
333         int protoff
334 )
335 {
336         struct secasvar *sav;
337         struct comp_algo *ipcompx;
338         int error, ralen, hlen, maxpacketsize, roff;
339         u_int8_t prot;
340         struct cryptodesc *crdc;
341         struct cryptop *crp;
342         struct tdb_crypto *tc;
343         struct mbuf *mo;
344         struct ipcomp *ipcomp;
345
346         SPLASSERT(net, "ipcomp_output");
347
348         sav = isr->sav;
349         KASSERT(sav != NULL, ("ipcomp_output: null SA"));
350         ipcompx = sav->tdb_compalgxform;
351         KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform"));
352
353         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
354         hlen = IPCOMP_HLENGTH;
355
356         ipcompstat.ipcomps_output++;
357
358         /* Check for maximum packet size violations. */
359         switch (sav->sah->saidx.dst.sa.sa_family) {
360 #ifdef INET
361         case AF_INET:
362                 maxpacketsize =  IP_MAXPACKET;
363                 break;
364 #endif /* INET */
365 #ifdef INET6
366         case AF_INET6:
367                 maxpacketsize =  IPV6_MAXPACKET;
368                 break;
369 #endif /* INET6 */
370         default:
371                 ipcompstat.ipcomps_nopf++;
372                 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
373                     ", IPCA %s/%08lx\n", 
374                     sav->sah->saidx.dst.sa.sa_family,
375                     ipsec_address(&sav->sah->saidx.dst),
376                     (u_long) ntohl(sav->spi)));
377                 error = EPFNOSUPPORT;
378                 goto bad;
379         }
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));
387                 error = EMSGSIZE;
388                 goto bad;
389         }
390
391         /* Update the counters */
392         ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
393
394         m = m_clone(m);
395         if (m == NULL) {
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)));
400                 error = ENOBUFS;
401                 goto bad;
402         }
403
404         /* Inject IPCOMP header */
405         mo = m_makespace(m, skip, hlen, &roff);
406         if (mo == NULL) {
407                 ipcompstat.ipcomps_wrap++;
408                 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
409                     "IPCA %s/%08lx\n",
410                     ipsec_address(&sav->sah->saidx.dst),
411                     (u_long) ntohl(sav->spi)));
412                 error = ENOBUFS;
413                 goto bad;
414         }
415         ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
416
417         /* Initialize the IPCOMP header */
418         /* XXX alignment always correct? */
419         switch (sav->sah->saidx.dst.sa.sa_family) {
420 #ifdef INET
421         case AF_INET:
422                 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
423                 break;
424 #endif /* INET */
425 #ifdef INET6
426         case AF_INET6:
427                 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
428                 break;
429 #endif
430         }
431         ipcomp->comp_flags = 0;
432         ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
433
434         /* Fix Next Protocol in IPv4/IPv6 header */
435         prot = IPPROTO_IPCOMP;
436         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
437
438         /* Ok now, we can pass to the crypto processing */
439
440         /* Get crypto descriptors */
441         crp = crypto_getreq(1);
442         if (crp == NULL) {
443                 ipcompstat.ipcomps_crypto++;
444                 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
445                 error = ENOBUFS;
446                 goto bad;
447         }
448         crdc = crp->crp_desc;
449
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;
455
456         /* Compression operation */
457         crdc->crd_alg = ipcompx->type;
458
459         /* IPsec-specific opaque crypto info */
460         tc = malloc(sizeof(struct tdb_crypto),
461                     M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK);
462         if (tc == NULL) {
463                 ipcompstat.ipcomps_crypto++;
464                 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
465                 crypto_freereq(crp);
466                 error = ENOBUFS;
467                 goto bad;
468         }
469
470         tc->tc_isr = isr;
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;
475
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;
483
484         return crypto_dispatch(crp);
485 bad:
486         if (m)
487                 m_freem(m);
488         return (error);
489 }
490
491 /*
492  * IPComp output callback from the crypto driver.
493  */
494 static int
495 ipcomp_output_cb(struct cryptop *crp)
496 {
497         struct tdb_crypto *tc;
498         struct ipsecrequest *isr;
499         struct secasvar *sav;
500         struct mbuf *m;
501         int s, error, skip, rlen;
502
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;
506         skip = tc->tc_skip;
507         rlen = crp->crp_ilen - skip;
508
509         s = splnet();
510
511         isr = tc->tc_isr;
512         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
513         if (sav == NULL) {
514                 ipcompstat.ipcomps_notdb++;
515                 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
516                 error = ENOBUFS;                /*XXX*/
517                 goto bad;
518         }
519         KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n"));
520
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;
526
527                 if (crp->crp_etype == EAGAIN) {
528                         KEY_FREESAV(&sav);
529                         splx(s);
530                         return crypto_dispatch(crp);
531                 }
532                 ipcompstat.ipcomps_noxform++;
533                 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype));
534                 error = crp->crp_etype;
535                 goto bad;
536         }
537         /* Shouldn't happen... */
538         if (m == NULL) {
539                 ipcompstat.ipcomps_crypto++;
540                 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
541                 error = EINVAL;
542                 goto bad;
543         }
544         ipcompstat.ipcomps_hist[sav->alg_comp]++;
545
546         if (rlen > crp->crp_olen) {
547                 /* Adjust the length in the IP header */
548                 switch (sav->sah->saidx.dst.sa.sa_family) {
549 #ifdef INET
550                 case AF_INET:
551                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
552                         break;
553 #endif /* INET */
554 #ifdef INET6
555                 case AF_INET6:
556                         mtod(m, struct ip6_hdr *)->ip6_plen =
557                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
558                         break;
559 #endif /* INET6 */
560                 default:
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;
568                         goto bad;
569                 }
570         } else {
571                 /* compression was useless, we have lost time */
572                 /* XXX add statistic */
573         }
574
575         /* Release the crypto descriptor */
576         free(tc, M_XDATA);
577         crypto_freereq(crp);
578
579         /* NB: m is reclaimed by ipsec_process_done. */
580         error = ipsec_process_done(m, isr);
581         KEY_FREESAV(&sav);
582         splx(s);
583         return error;
584 bad:
585         if (sav)
586                 KEY_FREESAV(&sav);
587         splx(s);
588         if (m)
589                 m_freem(m);
590         free(tc, M_XDATA);
591         crypto_freereq(crp);
592         return error;
593 }
594
595 static struct xformsw ipcomp_xformsw = {
596         XF_IPCOMP,              XFT_COMP,               "IPcomp",
597         ipcomp_init,            ipcomp_zeroize,         ipcomp_input,
598         ipcomp_output
599 };
600
601 static void
602 ipcomp_attach(void)
603 {
604         xform_register(&ipcomp_xformsw);
605 }
606 SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL)