]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/netipsec/xform_ipcomp.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / netipsec / xform_ipcomp.c
1 /*      $FreeBSD$       */
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3
4 /*-
5  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *   notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *   notice, this list of conditions and the following disclaimer in the
15  *   documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *   derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /* IP payload compression protocol (IPComp), see RFC 2393 */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
44
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
49
50 #include <net/route.h>
51 #include <net/vnet.h>
52
53 #include <netipsec/ipsec.h>
54 #include <netipsec/xform.h>
55
56 #ifdef INET6
57 #include <netinet/ip6.h>
58 #include <netipsec/ipsec6.h>
59 #endif
60
61 #include <netipsec/ipcomp.h>
62 #include <netipsec/ipcomp_var.h>
63
64 #include <netipsec/key.h>
65 #include <netipsec/key_debug.h>
66
67 #include <opencrypto/cryptodev.h>
68 #include <opencrypto/deflate.h>
69 #include <opencrypto/xform.h>
70
71 VNET_DEFINE(int, ipcomp_enable) = 0;
72 VNET_DEFINE(struct ipcompstat, ipcompstat);
73
74 SYSCTL_DECL(_net_inet_ipcomp);
75 SYSCTL_VNET_INT(_net_inet_ipcomp, OID_AUTO,
76         ipcomp_enable,  CTLFLAG_RW,     &VNET_NAME(ipcomp_enable),      0, "");
77 SYSCTL_VNET_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS,
78         stats,          CTLFLAG_RD,     &VNET_NAME(ipcompstat), ipcompstat, "");
79
80 static int ipcomp_input_cb(struct cryptop *crp);
81 static int ipcomp_output_cb(struct cryptop *crp);
82
83 struct comp_algo *
84 ipcomp_algorithm_lookup(int alg)
85 {
86         if (alg >= IPCOMP_ALG_MAX)
87                 return NULL;
88         switch (alg) {
89         case SADB_X_CALG_DEFLATE:
90                 return &comp_algo_deflate;
91         }
92         return NULL;
93 }
94
95 /*
96  * ipcomp_init() is called when an CPI is being set up.
97  */
98 static int
99 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
100 {
101         struct comp_algo *tcomp;
102         struct cryptoini cric;
103
104         /* NB: algorithm really comes in alg_enc and not alg_comp! */
105         tcomp = ipcomp_algorithm_lookup(sav->alg_enc);
106         if (tcomp == NULL) {
107                 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
108                          sav->alg_comp));
109                 return EINVAL;
110         }
111         sav->alg_comp = sav->alg_enc;           /* set for doing histogram */
112         sav->tdb_xform = xsp;
113         sav->tdb_compalgxform = tcomp;
114
115         /* Initialize crypto session */
116         bzero(&cric, sizeof (cric));
117         cric.cri_alg = sav->tdb_compalgxform->type;
118
119         return crypto_newsession(&sav->tdb_cryptoid, &cric, V_crypto_support);
120 }
121
122 /*
123  * ipcomp_zeroize() used when IPCA is deleted
124  */
125 static int
126 ipcomp_zeroize(struct secasvar *sav)
127 {
128         int err;
129
130         err = crypto_freesession(sav->tdb_cryptoid);
131         sav->tdb_cryptoid = 0;
132         return err;
133 }
134
135 /*
136  * ipcomp_input() gets called to uncompress an input packet
137  */
138 static int
139 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
140 {
141         struct tdb_crypto *tc;
142         struct cryptodesc *crdc;
143         struct cryptop *crp;
144         int hlen = IPCOMP_HLENGTH;
145
146         /* Get crypto descriptors */
147         crp = crypto_getreq(1);
148         if (crp == NULL) {
149                 m_freem(m);
150                 DPRINTF(("%s: no crypto descriptors\n", __func__));
151                 V_ipcompstat.ipcomps_crypto++;
152                 return ENOBUFS;
153         }
154         /* Get IPsec-specific opaque pointer */
155         tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
156         if (tc == NULL) {
157                 m_freem(m);
158                 crypto_freereq(crp);
159                 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160                 V_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 | CRYPTO_F_CBIFSYNC;
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 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         IPSEC_ASSERT(tc != NULL, ("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         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
232         if (sav == NULL) {
233                 V_ipcompstat.ipcomps_notdb++;
234                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
235                 error = ENOBUFS;                /*XXX*/
236                 goto bad;
237         }
238
239         saidx = &sav->sah->saidx;
240         IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
241                 saidx->dst.sa.sa_family == AF_INET6,
242                 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
243
244         /* Check for crypto errors */
245         if (crp->crp_etype) {
246                 /* Reset the session ID */
247                 if (sav->tdb_cryptoid != 0)
248                         sav->tdb_cryptoid = crp->crp_sid;
249
250                 if (crp->crp_etype == EAGAIN) {
251                         KEY_FREESAV(&sav);
252                         error = crypto_dispatch(crp);
253                         return error;
254                 }
255
256                 V_ipcompstat.ipcomps_noxform++;
257                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
258                 error = crp->crp_etype;
259                 goto bad;
260         }
261         /* Shouldn't happen... */
262         if (m == NULL) {
263                 V_ipcompstat.ipcomps_crypto++;
264                 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
265                 error = EINVAL;
266                 goto bad;
267         }
268         V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
269
270         clen = crp->crp_olen;           /* Length of data after processing */
271
272         /* Release the crypto descriptors */
273         free(tc, M_XDATA), tc = NULL;
274         crypto_freereq(crp), crp = NULL;
275
276         /* In case it's not done already, adjust the size of the mbuf chain */
277         m->m_pkthdr.len = clen + hlen + skip;
278
279         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
280                 V_ipcompstat.ipcomps_hdrops++;          /*XXX*/
281                 DPRINTF(("%s: m_pullup failed\n", __func__));
282                 error = EINVAL;                         /*XXX*/
283                 goto bad;
284         }
285
286         /* Keep the next protocol field */
287         addr = (caddr_t) mtod(m, struct ip *) + skip;
288         nproto = ((struct ipcomp *) addr)->comp_nxt;
289
290         /* Remove the IPCOMP header */
291         error = m_striphdr(m, skip, hlen);
292         if (error) {
293                 V_ipcompstat.ipcomps_hdrops++;
294                 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
295                          ipsec_address(&sav->sah->saidx.dst),
296                          (u_long) ntohl(sav->spi)));
297                 goto bad;
298         }
299
300         /* Restore the Next Protocol field */
301         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
302
303         IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
304
305         KEY_FREESAV(&sav);
306         return error;
307 bad:
308         if (sav)
309                 KEY_FREESAV(&sav);
310         if (m)
311                 m_freem(m);
312         if (tc != NULL)
313                 free(tc, M_XDATA);
314         if (crp)
315                 crypto_freereq(crp);
316         return error;
317 }
318
319 /*
320  * IPComp output routine, called by ipsec[46]_process_packet()
321  */
322 static int
323 ipcomp_output(
324         struct mbuf *m,
325         struct ipsecrequest *isr,
326         struct mbuf **mp,
327         int skip,
328         int protoff
329 )
330 {
331         struct secasvar *sav;
332         struct comp_algo *ipcompx;
333         int error, ralen, hlen, maxpacketsize, roff;
334         u_int8_t prot;
335         struct cryptodesc *crdc;
336         struct cryptop *crp;
337         struct tdb_crypto *tc;
338         struct mbuf *mo;
339         struct ipcomp *ipcomp;
340
341         sav = isr->sav;
342         IPSEC_ASSERT(sav != NULL, ("null SA"));
343         ipcompx = sav->tdb_compalgxform;
344         IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
345
346         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
347         hlen = IPCOMP_HLENGTH;
348
349         V_ipcompstat.ipcomps_output++;
350
351         /* Check for maximum packet size violations. */
352         switch (sav->sah->saidx.dst.sa.sa_family) {
353 #ifdef INET
354         case AF_INET:
355                 maxpacketsize =  IP_MAXPACKET;
356                 break;
357 #endif /* INET */
358 #ifdef INET6
359         case AF_INET6:
360                 maxpacketsize =  IPV6_MAXPACKET;
361                 break;
362 #endif /* INET6 */
363         default:
364                 V_ipcompstat.ipcomps_nopf++;
365                 DPRINTF(("%s: unknown/unsupported protocol family %d, "
366                     "IPCA %s/%08lx\n", __func__,
367                     sav->sah->saidx.dst.sa.sa_family,
368                     ipsec_address(&sav->sah->saidx.dst),
369                     (u_long) ntohl(sav->spi)));
370                 error = EPFNOSUPPORT;
371                 goto bad;
372         }
373         if (skip + hlen + ralen > maxpacketsize) {
374                 V_ipcompstat.ipcomps_toobig++;
375                 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
376                     "(len %u, max len %u)\n", __func__,
377                     ipsec_address(&sav->sah->saidx.dst),
378                     (u_long) ntohl(sav->spi),
379                     skip + hlen + ralen, maxpacketsize));
380                 error = EMSGSIZE;
381                 goto bad;
382         }
383
384         /* Update the counters */
385         V_ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
386
387         m = m_unshare(m, M_NOWAIT);
388         if (m == NULL) {
389                 V_ipcompstat.ipcomps_hdrops++;
390                 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
391                     __func__, ipsec_address(&sav->sah->saidx.dst),
392                     (u_long) ntohl(sav->spi)));
393                 error = ENOBUFS;
394                 goto bad;
395         }
396
397         /* Inject IPCOMP header */
398         mo = m_makespace(m, skip, hlen, &roff);
399         if (mo == NULL) {
400                 V_ipcompstat.ipcomps_wrap++;
401                 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
402                     __func__, ipsec_address(&sav->sah->saidx.dst),
403                     (u_long) ntohl(sav->spi)));
404                 error = ENOBUFS;
405                 goto bad;
406         }
407         ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
408
409         /* Initialize the IPCOMP header */
410         /* XXX alignment always correct? */
411         switch (sav->sah->saidx.dst.sa.sa_family) {
412 #ifdef INET
413         case AF_INET:
414                 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
415                 break;
416 #endif /* INET */
417 #ifdef INET6
418         case AF_INET6:
419                 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
420                 break;
421 #endif
422         }
423         ipcomp->comp_flags = 0;
424         ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
425
426         /* Fix Next Protocol in IPv4/IPv6 header */
427         prot = IPPROTO_IPCOMP;
428         m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
429
430         /* Ok now, we can pass to the crypto processing */
431
432         /* Get crypto descriptors */
433         crp = crypto_getreq(1);
434         if (crp == NULL) {
435                 V_ipcompstat.ipcomps_crypto++;
436                 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
437                 error = ENOBUFS;
438                 goto bad;
439         }
440         crdc = crp->crp_desc;
441
442         /* Compression descriptor */
443         crdc->crd_skip = skip + hlen;
444         crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
445         crdc->crd_flags = CRD_F_COMP;
446         crdc->crd_inject = skip + hlen;
447
448         /* Compression operation */
449         crdc->crd_alg = ipcompx->type;
450
451         /* IPsec-specific opaque crypto info */
452         tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
453                 M_XDATA, M_NOWAIT|M_ZERO);
454         if (tc == NULL) {
455                 V_ipcompstat.ipcomps_crypto++;
456                 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
457                 crypto_freereq(crp);
458                 error = ENOBUFS;
459                 goto bad;
460         }
461
462         tc->tc_isr = isr;
463         tc->tc_spi = sav->spi;
464         tc->tc_dst = sav->sah->saidx.dst;
465         tc->tc_proto = sav->sah->saidx.proto;
466         tc->tc_skip = skip + hlen;
467
468         /* Crypto operation descriptor */
469         crp->crp_ilen = m->m_pkthdr.len;        /* Total input length */
470         crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
471         crp->crp_buf = (caddr_t) m;
472         crp->crp_callback = ipcomp_output_cb;
473         crp->crp_opaque = (caddr_t) tc;
474         crp->crp_sid = sav->tdb_cryptoid;
475
476         return crypto_dispatch(crp);
477 bad:
478         if (m)
479                 m_freem(m);
480         return (error);
481 }
482
483 /*
484  * IPComp output callback from the crypto driver.
485  */
486 static int
487 ipcomp_output_cb(struct cryptop *crp)
488 {
489         struct tdb_crypto *tc;
490         struct ipsecrequest *isr;
491         struct secasvar *sav;
492         struct mbuf *m;
493         int error, skip, rlen;
494
495         tc = (struct tdb_crypto *) crp->crp_opaque;
496         IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
497         m = (struct mbuf *) crp->crp_buf;
498         skip = tc->tc_skip;
499         rlen = crp->crp_ilen - skip;
500
501         isr = tc->tc_isr;
502         IPSECREQUEST_LOCK(isr);
503         sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
504         if (sav == NULL) {
505                 V_ipcompstat.ipcomps_notdb++;
506                 DPRINTF(("%s: SA expired while in crypto\n", __func__));
507                 error = ENOBUFS;                /*XXX*/
508                 goto bad;
509         }
510         IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
511
512         /* Check for crypto errors */
513         if (crp->crp_etype) {
514                 /* Reset session ID */
515                 if (sav->tdb_cryptoid != 0)
516                         sav->tdb_cryptoid = crp->crp_sid;
517
518                 if (crp->crp_etype == EAGAIN) {
519                         KEY_FREESAV(&sav);
520                         IPSECREQUEST_UNLOCK(isr);
521                         error = crypto_dispatch(crp);
522                         return error;
523                 }
524                 V_ipcompstat.ipcomps_noxform++;
525                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
526                 error = crp->crp_etype;
527                 goto bad;
528         }
529         /* Shouldn't happen... */
530         if (m == NULL) {
531                 V_ipcompstat.ipcomps_crypto++;
532                 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
533                 error = EINVAL;
534                 goto bad;
535         }
536         V_ipcompstat.ipcomps_hist[sav->alg_comp]++;
537
538         if (rlen > crp->crp_olen) {
539                 /* Adjust the length in the IP header */
540                 switch (sav->sah->saidx.dst.sa.sa_family) {
541 #ifdef INET
542                 case AF_INET:
543                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
544                         break;
545 #endif /* INET */
546 #ifdef INET6
547                 case AF_INET6:
548                         mtod(m, struct ip6_hdr *)->ip6_plen =
549                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
550                         break;
551 #endif /* INET6 */
552                 default:
553                         V_ipcompstat.ipcomps_nopf++;
554                         DPRINTF(("%s: unknown/unsupported protocol "
555                             "family %d, IPCA %s/%08lx\n", __func__,
556                             sav->sah->saidx.dst.sa.sa_family,
557                             ipsec_address(&sav->sah->saidx.dst), 
558                             (u_long) ntohl(sav->spi)));
559                         error = EPFNOSUPPORT;
560                         goto bad;
561                 }
562         } else {
563                 /* compression was useless, we have lost time */
564                 /* XXX add statistic */
565         }
566
567         /* Release the crypto descriptor */
568         free(tc, M_XDATA);
569         crypto_freereq(crp);
570
571         /* NB: m is reclaimed by ipsec_process_done. */
572         error = ipsec_process_done(m, isr);
573         KEY_FREESAV(&sav);
574         IPSECREQUEST_UNLOCK(isr);
575         return error;
576 bad:
577         if (sav)
578                 KEY_FREESAV(&sav);
579         IPSECREQUEST_UNLOCK(isr);
580         if (m)
581                 m_freem(m);
582         free(tc, M_XDATA);
583         crypto_freereq(crp);
584         return error;
585 }
586
587 static struct xformsw ipcomp_xformsw = {
588         XF_IPCOMP,              XFT_COMP,               "IPcomp",
589         ipcomp_init,            ipcomp_zeroize,         ipcomp_input,
590         ipcomp_output
591 };
592
593 static void
594 ipcomp_attach(void)
595 {
596
597         xform_register(&ipcomp_xformsw);
598 }
599
600 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);