]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netipsec/xform_ipcomp.c
MFV: r360512
[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  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *   notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *   notice, this list of conditions and the following disclaimer in the
17  *   documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *   derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 /* IP payload compression protocol (IPComp), see RFC 2393 */
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/mbuf.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/socket.h>
43 #include <sys/kernel.h>
44 #include <sys/protosw.h>
45 #include <sys/sysctl.h>
46
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip_encap.h>
52
53 #include <net/netisr.h>
54 #include <net/vnet.h>
55 #include <net/if.h>     /* XXXGL: net_epoch should move out there */
56 #include <net/if_var.h> /* XXXGL: net_epoch should move out there */
57
58 #include <netipsec/ipsec.h>
59 #include <netipsec/xform.h>
60
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #include <netipsec/ipsec6.h>
65 #endif
66
67 #include <netipsec/ipcomp.h>
68 #include <netipsec/ipcomp_var.h>
69
70 #include <netipsec/key.h>
71 #include <netipsec/key_debug.h>
72
73 #include <opencrypto/cryptodev.h>
74 #include <opencrypto/deflate.h>
75 #include <opencrypto/xform.h>
76
77 VNET_DEFINE(int, ipcomp_enable) = 1;
78 VNET_PCPUSTAT_DEFINE(struct ipcompstat, ipcompstat);
79 VNET_PCPUSTAT_SYSINIT(ipcompstat);
80
81 #ifdef VIMAGE
82 VNET_PCPUSTAT_SYSUNINIT(ipcompstat);
83 #endif /* VIMAGE */
84
85 SYSCTL_DECL(_net_inet_ipcomp);
86 SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, ipcomp_enable,
87         CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ipcomp_enable), 0, "");
88 SYSCTL_VNET_PCPUSTAT(_net_inet_ipcomp, IPSECCTL_STATS, stats,
89     struct ipcompstat, ipcompstat,
90     "IPCOMP statistics (struct ipcompstat, netipsec/ipcomp_var.h");
91
92 static int ipcomp_input_cb(struct cryptop *crp);
93 static int ipcomp_output_cb(struct cryptop *crp);
94
95 /*
96  * RFC 3173 p 2.2. Non-Expansion Policy:
97  * If the total size of a compressed payload and the IPComp header, as
98  * defined in section 3, is not smaller than the size of the original
99  * payload, the IP datagram MUST be sent in the original non-compressed
100  * form.
101  *
102  * When we use IPComp in tunnel mode, for small packets we will receive
103  * encapsulated IP-IP datagrams without any compression and without IPComp
104  * header.
105  */
106 static int
107 ipcomp_encapcheck(union sockaddr_union *src, union sockaddr_union *dst)
108 {
109         struct secasvar *sav;
110
111         sav = key_allocsa_tunnel(src, dst, IPPROTO_IPCOMP);
112         if (sav == NULL)
113                 return (0);
114         key_freesav(&sav);
115
116         if (src->sa.sa_family == AF_INET)
117                 return (sizeof(struct in_addr) << 4);
118         else
119                 return (sizeof(struct in6_addr) << 4);
120 }
121
122 static int
123 ipcomp_nonexp_input(struct mbuf *m, int off, int proto, void *arg __unused)
124 {
125         int isr;
126
127         NET_EPOCH_ASSERT();
128
129         switch (proto) {
130 #ifdef INET
131         case IPPROTO_IPV4:
132                 isr = NETISR_IP;
133                 break;
134 #endif
135 #ifdef INET6
136         case IPPROTO_IPV6:
137                 isr = NETISR_IPV6;
138                 break;
139 #endif
140         default:
141                 IPCOMPSTAT_INC(ipcomps_nopf);
142                 m_freem(m);
143                 return (IPPROTO_DONE);
144         }
145         m_adj(m, off);
146         IPCOMPSTAT_ADD(ipcomps_ibytes, m->m_pkthdr.len);
147         IPCOMPSTAT_INC(ipcomps_input);
148         netisr_dispatch(isr, m);
149         return (IPPROTO_DONE);
150 }
151
152 /*
153  * ipcomp_init() is called when an CPI is being set up.
154  */
155 static int
156 ipcomp_init(struct secasvar *sav, struct xformsw *xsp)
157 {
158         const struct comp_algo *tcomp;
159         struct crypto_session_params csp;
160
161         /* NB: algorithm really comes in alg_enc and not alg_comp! */
162         tcomp = comp_algorithm_lookup(sav->alg_enc);
163         if (tcomp == NULL) {
164                 DPRINTF(("%s: unsupported compression algorithm %d\n", __func__,
165                          sav->alg_comp));
166                 return EINVAL;
167         }
168         sav->alg_comp = sav->alg_enc;           /* set for doing histogram */
169         sav->tdb_xform = xsp;
170         sav->tdb_compalgxform = tcomp;
171
172         /* Initialize crypto session */
173         memset(&csp, 0, sizeof(csp));
174         csp.csp_mode = CSP_MODE_COMPRESS;
175         csp.csp_cipher_alg = sav->tdb_compalgxform->type;
176
177         return crypto_newsession(&sav->tdb_cryptoid, &csp, V_crypto_support);
178 }
179
180 /*
181  * ipcomp_zeroize() used when IPCA is deleted
182  */
183 static int
184 ipcomp_zeroize(struct secasvar *sav)
185 {
186
187         crypto_freesession(sav->tdb_cryptoid);
188         sav->tdb_cryptoid = NULL;
189         return 0;
190 }
191
192 /*
193  * ipcomp_input() gets called to uncompress an input packet
194  */
195 static int
196 ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff)
197 {
198         struct xform_data *xd;
199         struct cryptop *crp;
200         struct ipcomp *ipcomp;
201         crypto_session_t cryptoid;
202         caddr_t addr;
203         int error, hlen = IPCOMP_HLENGTH;
204
205         /*
206          * Check that the next header of the IPComp is not IPComp again, before
207          * doing any real work.  Given it is not possible to do double
208          * compression it means someone is playing tricks on us.
209          */
210         error = ENOBUFS;
211         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
212                 IPCOMPSTAT_INC(ipcomps_hdrops);         /*XXX*/
213                 DPRINTF(("%s: m_pullup failed\n", __func__));
214                 key_freesav(&sav);
215                 return (error);
216         }
217         addr = (caddr_t) mtod(m, struct ip *) + skip;
218         ipcomp = (struct ipcomp *)addr;
219         if (ipcomp->comp_nxt == IPPROTO_IPCOMP) {
220                 IPCOMPSTAT_INC(ipcomps_pdrops); /* XXX have our own stats? */
221                 DPRINTF(("%s: recursive compression detected\n", __func__));
222                 error = EINVAL;
223                 goto bad;
224         }
225
226         SECASVAR_LOCK(sav);
227         cryptoid = sav->tdb_cryptoid;
228         SECASVAR_UNLOCK(sav);
229
230         /* Get crypto descriptors */
231         crp = crypto_getreq(cryptoid, M_NOWAIT);
232         if (crp == NULL) {
233                 DPRINTF(("%s: no crypto descriptors\n", __func__));
234                 IPCOMPSTAT_INC(ipcomps_crypto);
235                 goto bad;
236         }
237         /* Get IPsec-specific opaque pointer */
238         xd = malloc(sizeof(*xd), M_XDATA, M_NOWAIT | M_ZERO);
239         if (xd == NULL) {
240                 DPRINTF(("%s: cannot allocate xform_data\n", __func__));
241                 IPCOMPSTAT_INC(ipcomps_crypto);
242                 crypto_freereq(crp);
243                 goto bad;
244         }
245
246         /* Decompression operation */
247         crp->crp_op = CRYPTO_OP_DECOMPRESS;
248         crp->crp_payload_start = skip + hlen;
249         crp->crp_payload_length = m->m_pkthdr.len - (skip + hlen);
250
251         /* Crypto operation descriptor */
252         crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
253         crp->crp_flags = CRYPTO_F_CBIFSYNC;
254         crp->crp_mbuf = m;
255         crp->crp_buf_type = CRYPTO_BUF_MBUF;
256         crp->crp_callback = ipcomp_input_cb;
257         crp->crp_opaque = xd;
258
259         /* These are passed as-is to the callback */
260         xd->sav = sav;
261         xd->protoff = protoff;
262         xd->skip = skip;
263         xd->vnet = curvnet;
264         xd->cryptoid = cryptoid;
265
266         SECASVAR_LOCK(sav);
267         crp->crp_session = xd->cryptoid = sav->tdb_cryptoid;
268         SECASVAR_UNLOCK(sav);
269
270         return crypto_dispatch(crp);
271 bad:
272         m_freem(m);
273         key_freesav(&sav);
274         return (error);
275 }
276
277 /*
278  * IPComp input callback from the crypto driver.
279  */
280 static int
281 ipcomp_input_cb(struct cryptop *crp)
282 {
283         IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
284         struct xform_data *xd;
285         struct mbuf *m;
286         struct secasvar *sav;
287         struct secasindex *saidx;
288         caddr_t addr;
289         crypto_session_t cryptoid;
290         int hlen = IPCOMP_HLENGTH, error, clen;
291         int skip, protoff;
292         uint8_t nproto;
293
294         m = crp->crp_mbuf;
295         xd = crp->crp_opaque;
296         CURVNET_SET(xd->vnet);
297         sav = xd->sav;
298         skip = xd->skip;
299         protoff = xd->protoff;
300         cryptoid = xd->cryptoid;
301         saidx = &sav->sah->saidx;
302         IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
303                 saidx->dst.sa.sa_family == AF_INET6,
304                 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
305
306         /* Check for crypto errors */
307         if (crp->crp_etype) {
308                 if (crp->crp_etype == EAGAIN) {
309                         /* Reset the session ID */
310                         if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
311                                 crypto_freesession(cryptoid);
312                         xd->cryptoid = crp->crp_session;
313                         CURVNET_RESTORE();
314                         return (crypto_dispatch(crp));
315                 }
316                 IPCOMPSTAT_INC(ipcomps_noxform);
317                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
318                 error = crp->crp_etype;
319                 goto bad;
320         }
321         /* Shouldn't happen... */
322         if (m == NULL) {
323                 IPCOMPSTAT_INC(ipcomps_crypto);
324                 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
325                 error = EINVAL;
326                 goto bad;
327         }
328         IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
329
330         clen = crp->crp_olen;           /* Length of data after processing */
331
332         /* Release the crypto descriptors */
333         free(xd, M_XDATA), xd = NULL;
334         crypto_freereq(crp), crp = NULL;
335
336         /* In case it's not done already, adjust the size of the mbuf chain */
337         m->m_pkthdr.len = clen + hlen + skip;
338
339         if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) {
340                 IPCOMPSTAT_INC(ipcomps_hdrops);         /*XXX*/
341                 DPRINTF(("%s: m_pullup failed\n", __func__));
342                 error = EINVAL;                         /*XXX*/
343                 goto bad;
344         }
345
346         /* Keep the next protocol field */
347         addr = (caddr_t) mtod(m, struct ip *) + skip;
348         nproto = ((struct ipcomp *) addr)->comp_nxt;
349
350         /* Remove the IPCOMP header */
351         error = m_striphdr(m, skip, hlen);
352         if (error) {
353                 IPCOMPSTAT_INC(ipcomps_hdrops);
354                 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
355                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
356                     (u_long) ntohl(sav->spi)));
357                 goto bad;
358         }
359
360         /* Restore the Next Protocol field */
361         m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
362
363         switch (saidx->dst.sa.sa_family) {
364 #ifdef INET6
365         case AF_INET6:
366                 error = ipsec6_common_input_cb(m, sav, skip, protoff);
367                 break;
368 #endif
369 #ifdef INET
370         case AF_INET:
371                 error = ipsec4_common_input_cb(m, sav, skip, protoff);
372                 break;
373 #endif
374         default:
375                 panic("%s: Unexpected address family: %d saidx=%p", __func__,
376                     saidx->dst.sa.sa_family, saidx);
377         }
378         CURVNET_RESTORE();
379         return error;
380 bad:
381         CURVNET_RESTORE();
382         if (sav != NULL)
383                 key_freesav(&sav);
384         if (m != NULL)
385                 m_freem(m);
386         if (xd != NULL)
387                 free(xd, M_XDATA);
388         if (crp != NULL)
389                 crypto_freereq(crp);
390         return error;
391 }
392
393 /*
394  * IPComp output routine, called by ipsec[46]_perform_request()
395  */
396 static int
397 ipcomp_output(struct mbuf *m, struct secpolicy *sp, struct secasvar *sav,
398     u_int idx, int skip, int protoff)
399 {
400         IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
401         const struct comp_algo *ipcompx;
402         struct cryptop *crp;
403         struct xform_data *xd;
404         crypto_session_t cryptoid;
405         int error, ralen, maxpacketsize;
406
407         IPSEC_ASSERT(sav != NULL, ("null SA"));
408         ipcompx = sav->tdb_compalgxform;
409         IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
410
411         /*
412          * Do not touch the packet in case our payload to compress
413          * is lower than the minimal threshold of the compression
414          * alogrithm.  We will just send out the data uncompressed.
415          * See RFC 3173, 2.2. Non-Expansion Policy.
416          */
417         if (m->m_pkthdr.len <= ipcompx->minlen) {
418                 IPCOMPSTAT_INC(ipcomps_threshold);
419                 return ipsec_process_done(m, sp, sav, idx);
420         }
421
422         ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
423         IPCOMPSTAT_INC(ipcomps_output);
424
425         /* Check for maximum packet size violations. */
426         switch (sav->sah->saidx.dst.sa.sa_family) {
427 #ifdef INET
428         case AF_INET:
429                 maxpacketsize = IP_MAXPACKET;
430                 break;
431 #endif /* INET */
432 #ifdef INET6
433         case AF_INET6:
434                 maxpacketsize = IPV6_MAXPACKET;
435                 break;
436 #endif /* INET6 */
437         default:
438                 IPCOMPSTAT_INC(ipcomps_nopf);
439                 DPRINTF(("%s: unknown/unsupported protocol family %d, "
440                     "IPCA %s/%08lx\n", __func__,
441                     sav->sah->saidx.dst.sa.sa_family,
442                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
443                     (u_long) ntohl(sav->spi)));
444                 error = EPFNOSUPPORT;
445                 goto bad;
446         }
447         if (ralen + skip + IPCOMP_HLENGTH > maxpacketsize) {
448                 IPCOMPSTAT_INC(ipcomps_toobig);
449                 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
450                     "(len %u, max len %u)\n", __func__,
451                     ipsec_address(&sav->sah->saidx.dst, buf, sizeof(buf)),
452                     (u_long) ntohl(sav->spi),
453                     ralen + skip + IPCOMP_HLENGTH, maxpacketsize));
454                 error = EMSGSIZE;
455                 goto bad;
456         }
457
458         /* Update the counters */
459         IPCOMPSTAT_ADD(ipcomps_obytes, m->m_pkthdr.len - skip);
460
461         m = m_unshare(m, M_NOWAIT);
462         if (m == NULL) {
463                 IPCOMPSTAT_INC(ipcomps_hdrops);
464                 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
465                     __func__, ipsec_address(&sav->sah->saidx.dst, buf,
466                     sizeof(buf)), (u_long) ntohl(sav->spi)));
467                 error = ENOBUFS;
468                 goto bad;
469         }
470
471         /* Ok now, we can pass to the crypto processing. */
472         SECASVAR_LOCK(sav);
473         cryptoid = sav->tdb_cryptoid;
474         SECASVAR_UNLOCK(sav);
475
476         /* Get crypto descriptors */
477         crp = crypto_getreq(cryptoid, M_NOWAIT);
478         if (crp == NULL) {
479                 IPCOMPSTAT_INC(ipcomps_crypto);
480                 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
481                 error = ENOBUFS;
482                 goto bad;
483         }
484
485         /* Compression descriptor */
486         crp->crp_op = CRYPTO_OP_COMPRESS;
487         crp->crp_payload_start = skip;
488         crp->crp_payload_length = ralen;
489
490         /* IPsec-specific opaque crypto info */
491         xd =  malloc(sizeof(struct xform_data), M_XDATA, M_NOWAIT | M_ZERO);
492         if (xd == NULL) {
493                 IPCOMPSTAT_INC(ipcomps_crypto);
494                 DPRINTF(("%s: failed to allocate xform_data\n", __func__));
495                 crypto_freereq(crp);
496                 error = ENOBUFS;
497                 goto bad;
498         }
499
500         xd->sp = sp;
501         xd->sav = sav;
502         xd->idx = idx;
503         xd->skip = skip;
504         xd->protoff = protoff;
505         xd->vnet = curvnet;
506         xd->cryptoid = cryptoid;
507
508         /* Crypto operation descriptor */
509         crp->crp_ilen = m->m_pkthdr.len;        /* Total input length */
510         crp->crp_flags = CRYPTO_F_CBIFSYNC;
511         crp->crp_mbuf = m;
512         crp->crp_buf_type = CRYPTO_BUF_MBUF;
513         crp->crp_callback = ipcomp_output_cb;
514         crp->crp_opaque = xd;
515
516         return crypto_dispatch(crp);
517 bad:
518         if (m)
519                 m_freem(m);
520         key_freesav(&sav);
521         key_freesp(&sp);
522         return (error);
523 }
524
525 /*
526  * IPComp output callback from the crypto driver.
527  */
528 static int
529 ipcomp_output_cb(struct cryptop *crp)
530 {
531         IPSEC_DEBUG_DECLARE(char buf[IPSEC_ADDRSTRLEN]);
532         struct xform_data *xd;
533         struct secpolicy *sp;
534         struct secasvar *sav;
535         struct mbuf *m;
536         crypto_session_t cryptoid;
537         u_int idx;
538         int error, skip, protoff;
539
540         m = crp->crp_mbuf;
541         xd = crp->crp_opaque;
542         CURVNET_SET(xd->vnet);
543         idx = xd->idx;
544         sp = xd->sp;
545         sav = xd->sav;
546         skip = xd->skip;
547         protoff = xd->protoff;
548         cryptoid = xd->cryptoid;
549
550         /* Check for crypto errors */
551         if (crp->crp_etype) {
552                 if (crp->crp_etype == EAGAIN) {
553                         /* Reset the session ID */
554                         if (ipsec_updateid(sav, &crp->crp_session, &cryptoid) != 0)
555                                 crypto_freesession(cryptoid);
556                         xd->cryptoid = crp->crp_session;
557                         CURVNET_RESTORE();
558                         return (crypto_dispatch(crp));
559                 }
560                 IPCOMPSTAT_INC(ipcomps_noxform);
561                 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
562                 error = crp->crp_etype;
563                 goto bad;
564         }
565         /* Shouldn't happen... */
566         if (m == NULL) {
567                 IPCOMPSTAT_INC(ipcomps_crypto);
568                 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
569                 error = EINVAL;
570                 goto bad;
571         }
572         IPCOMPSTAT_INC(ipcomps_hist[sav->alg_comp]);
573
574         if (crp->crp_payload_length > crp->crp_olen) {
575                 struct mbuf *mo;
576                 struct ipcomp *ipcomp;
577                 int roff;
578                 uint8_t prot;
579
580                 /* Compression helped, inject IPCOMP header. */
581                 mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff);
582                 if (mo == NULL) {
583                         IPCOMPSTAT_INC(ipcomps_wrap);
584                         DPRINTF(("%s: IPCOMP header inject failed "
585                             "for IPCA %s/%08lx\n",
586                             __func__, ipsec_address(&sav->sah->saidx.dst, buf,
587                             sizeof(buf)), (u_long) ntohl(sav->spi)));
588                         error = ENOBUFS;
589                         goto bad;
590                 }
591                 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
592
593                 /* Initialize the IPCOMP header */
594                 /* XXX alignment always correct? */
595                 switch (sav->sah->saidx.dst.sa.sa_family) {
596 #ifdef INET
597                 case AF_INET:
598                         ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
599                         break;
600 #endif /* INET */
601 #ifdef INET6
602                 case AF_INET6:
603                         ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
604                         break;
605 #endif
606                 }
607                 ipcomp->comp_flags = 0;
608                 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
609
610                 /* Fix Next Protocol in IPv4/IPv6 header */
611                 prot = IPPROTO_IPCOMP;
612                 m_copyback(m, protoff, sizeof(u_int8_t),
613                     (u_char *)&prot);
614
615                 /* Adjust the length in the IP header */
616                 switch (sav->sah->saidx.dst.sa.sa_family) {
617 #ifdef INET
618                 case AF_INET:
619                         mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
620                         break;
621 #endif /* INET */
622 #ifdef INET6
623                 case AF_INET6:
624                         mtod(m, struct ip6_hdr *)->ip6_plen =
625                                 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
626                         break;
627 #endif /* INET6 */
628                 default:
629                         IPCOMPSTAT_INC(ipcomps_nopf);
630                         DPRINTF(("%s: unknown/unsupported protocol "
631                             "family %d, IPCA %s/%08lx\n", __func__,
632                             sav->sah->saidx.dst.sa.sa_family,
633                             ipsec_address(&sav->sah->saidx.dst, buf,
634                                 sizeof(buf)), (u_long) ntohl(sav->spi)));
635                         error = EPFNOSUPPORT;
636                         goto bad;
637                 }
638         } else {
639                 /* Compression was useless, we have lost time. */
640                 IPCOMPSTAT_INC(ipcomps_uncompr);
641                 DPRINTF(("%s: compressions was useless %d <= %d\n",
642                     __func__, crp->crp_payload_length, crp->crp_olen));
643                 /* XXX remember state to not compress the next couple
644                  *     of packets, RFC 3173, 2.2. Non-Expansion Policy */
645         }
646
647         /* Release the crypto descriptor */
648         free(xd, M_XDATA);
649         crypto_freereq(crp);
650
651         /* NB: m is reclaimed by ipsec_process_done. */
652         error = ipsec_process_done(m, sp, sav, idx);
653         CURVNET_RESTORE();
654         return (error);
655 bad:
656         if (m)
657                 m_freem(m);
658         CURVNET_RESTORE();
659         free(xd, M_XDATA);
660         crypto_freereq(crp);
661         key_freesav(&sav);
662         key_freesp(&sp);
663         return (error);
664 }
665
666 #ifdef INET
667 static int
668 ipcomp4_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
669     void *arg __unused)
670 {
671         union sockaddr_union src, dst;
672         const struct ip *ip;
673
674         if (V_ipcomp_enable == 0)
675                 return (0);
676         if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
677                 return (0);
678         bzero(&src, sizeof(src));
679         bzero(&dst, sizeof(dst));
680         src.sa.sa_family = dst.sa.sa_family = AF_INET;
681         src.sin.sin_len = dst.sin.sin_len = sizeof(struct sockaddr_in);
682         ip = mtod(m, const struct ip *);
683         src.sin.sin_addr = ip->ip_src;
684         dst.sin.sin_addr = ip->ip_dst;
685         return (ipcomp_encapcheck(&src, &dst));
686 }
687
688 static const struct encaptab *ipe4_cookie = NULL;
689 static const struct encap_config ipv4_encap_cfg = {
690         .proto = -1,
691         .min_length = sizeof(struct ip),
692         .exact_match = sizeof(in_addr_t) << 4,
693         .check = ipcomp4_nonexp_encapcheck,
694         .input = ipcomp_nonexp_input
695 };
696 #endif
697 #ifdef INET6
698 static int
699 ipcomp6_nonexp_encapcheck(const struct mbuf *m, int off, int proto,
700     void *arg __unused)
701 {
702         union sockaddr_union src, dst;
703         const struct ip6_hdr *ip6;
704
705         if (V_ipcomp_enable == 0)
706                 return (0);
707         if (proto != IPPROTO_IPV4 && proto != IPPROTO_IPV6)
708                 return (0);
709         bzero(&src, sizeof(src));
710         bzero(&dst, sizeof(dst));
711         src.sa.sa_family = dst.sa.sa_family = AF_INET;
712         src.sin6.sin6_len = dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
713         ip6 = mtod(m, const struct ip6_hdr *);
714         src.sin6.sin6_addr = ip6->ip6_src;
715         dst.sin6.sin6_addr = ip6->ip6_dst;
716         if (IN6_IS_SCOPE_LINKLOCAL(&src.sin6.sin6_addr)) {
717                 /* XXX: sa6_recoverscope() */
718                 src.sin6.sin6_scope_id =
719                     ntohs(src.sin6.sin6_addr.s6_addr16[1]);
720                 src.sin6.sin6_addr.s6_addr16[1] = 0;
721         }
722         if (IN6_IS_SCOPE_LINKLOCAL(&dst.sin6.sin6_addr)) {
723                 /* XXX: sa6_recoverscope() */
724                 dst.sin6.sin6_scope_id =
725                     ntohs(dst.sin6.sin6_addr.s6_addr16[1]);
726                 dst.sin6.sin6_addr.s6_addr16[1] = 0;
727         }
728         return (ipcomp_encapcheck(&src, &dst));
729 }
730
731 static const struct encaptab *ipe6_cookie = NULL;
732 static const struct encap_config ipv6_encap_cfg = {
733         .proto = -1,
734         .min_length = sizeof(struct ip6_hdr),
735         .exact_match = sizeof(struct in6_addr) << 4,
736         .check = ipcomp6_nonexp_encapcheck,
737         .input = ipcomp_nonexp_input
738 };
739 #endif
740
741 static struct xformsw ipcomp_xformsw = {
742         .xf_type =      XF_IPCOMP,
743         .xf_name =      "IPcomp",
744         .xf_init =      ipcomp_init,
745         .xf_zeroize =   ipcomp_zeroize,
746         .xf_input =     ipcomp_input,
747         .xf_output =    ipcomp_output,
748 };
749
750 static void
751 ipcomp_attach(void)
752 {
753
754 #ifdef INET
755         ipe4_cookie = ip_encap_attach(&ipv4_encap_cfg, NULL, M_WAITOK);
756 #endif
757 #ifdef INET6
758         ipe6_cookie = ip6_encap_attach(&ipv6_encap_cfg, NULL, M_WAITOK);
759 #endif
760         xform_attach(&ipcomp_xformsw);
761 }
762
763 static void
764 ipcomp_detach(void)
765 {
766
767 #ifdef INET
768         ip_encap_detach(ipe4_cookie);
769 #endif
770 #ifdef INET6
771         ip6_encap_detach(ipe6_cookie);
772 #endif
773         xform_detach(&ipcomp_xformsw);
774 }
775
776 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
777     ipcomp_attach, NULL);
778 SYSUNINIT(ipcomp_xform_uninit, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE,
779     ipcomp_detach, NULL);