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