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