1 /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.2 2003/02/26 00:14:06 sam Exp $ */
2 /* $DragonFly: src/sys/netproto/ipsec/xform_ipcomp.c,v 1.10 2006/10/12 01:32:51 hsu Exp $ */
3 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
6 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 /* IP payload compression protocol (IPComp), see RFC 2393 */
34 #include "opt_inet6.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/socket.h>
40 #include <sys/kernel.h>
41 #include <sys/protosw.h>
42 #include <sys/sysctl.h>
43 #include <sys/thread2.h>
45 #include <netinet/in.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
50 #include <net/route.h>
51 #include <netproto/ipsec/ipsec.h>
52 #include <netproto/ipsec/xform.h>
55 #include <netinet/ip6.h>
56 #include <netproto/ipsec/ipsec6.h>
59 #include <netproto/ipsec/ipcomp.h>
60 #include <netproto/ipsec/ipcomp_var.h>
62 #include <netproto/ipsec/key.h>
63 #include <netproto/ipsec/key_debug.h>
65 #include <opencrypto/cryptodev.h>
66 #include <opencrypto/deflate.h>
67 #include <opencrypto/xform.h>
69 int ipcomp_enable
= 0;
70 struct ipcompstat ipcompstat
;
72 SYSCTL_DECL(_net_inet_ipcomp
);
73 SYSCTL_INT(_net_inet_ipcomp
, OID_AUTO
,
74 ipcomp_enable
, CTLFLAG_RW
, &ipcomp_enable
, 0, "");
75 SYSCTL_STRUCT(_net_inet_ipcomp
, IPSECCTL_STATS
,
76 stats
, CTLFLAG_RD
, &ipcompstat
, ipcompstat
, "");
78 static int ipcomp_input_cb(struct cryptop
*crp
);
79 static int ipcomp_output_cb(struct cryptop
*crp
);
82 ipcomp_algorithm_lookup(int alg
)
84 if (alg
>= IPCOMP_ALG_MAX
)
87 case SADB_X_CALG_DEFLATE
:
88 return &comp_algo_deflate
;
94 * ipcomp_init() is called when an CPI is being set up.
97 ipcomp_init(struct secasvar
*sav
, struct xformsw
*xsp
)
99 struct comp_algo
*tcomp
;
100 struct cryptoini cric
;
102 /* NB: algorithm really comes in alg_enc and not alg_comp! */
103 tcomp
= ipcomp_algorithm_lookup(sav
->alg_enc
);
105 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
109 sav
->alg_comp
= sav
->alg_enc
; /* set for doing histogram */
110 sav
->tdb_xform
= xsp
;
111 sav
->tdb_compalgxform
= tcomp
;
113 /* Initialize crypto session */
114 bzero(&cric
, sizeof (cric
));
115 cric
.cri_alg
= sav
->tdb_compalgxform
->type
;
117 return crypto_newsession(&sav
->tdb_cryptoid
, &cric
, crypto_support
);
121 * ipcomp_zeroize() used when IPCA is deleted
124 ipcomp_zeroize(struct secasvar
*sav
)
128 err
= crypto_freesession(sav
->tdb_cryptoid
);
129 sav
->tdb_cryptoid
= 0;
134 * ipcomp_input() gets called to uncompress an input packet
137 ipcomp_input(struct mbuf
*m
, struct secasvar
*sav
, int skip
, int protoff
)
139 struct tdb_crypto
*tc
;
140 struct cryptodesc
*crdc
;
142 int hlen
= IPCOMP_HLENGTH
;
144 /* Get crypto descriptors */
145 crp
= crypto_getreq(1);
148 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
149 ipcompstat
.ipcomps_crypto
++;
152 /* Get IPsec-specific opaque pointer */
153 tc
= kmalloc(sizeof (*tc
), M_XDATA
,
154 M_INTWAIT
| M_ZERO
| M_NULLOK
);
158 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
159 ipcompstat
.ipcomps_crypto
++;
162 crdc
= crp
->crp_desc
;
164 crdc
->crd_skip
= skip
+ hlen
;
165 crdc
->crd_len
= m
->m_pkthdr
.len
- (skip
+ hlen
);
166 crdc
->crd_inject
= skip
;
170 /* Decompression operation */
171 crdc
->crd_alg
= sav
->tdb_compalgxform
->type
;
173 /* Crypto operation descriptor */
174 crp
->crp_ilen
= m
->m_pkthdr
.len
- (skip
+ hlen
);
175 crp
->crp_flags
= CRYPTO_F_IMBUF
;
176 crp
->crp_buf
= (caddr_t
) m
;
177 crp
->crp_callback
= ipcomp_input_cb
;
178 crp
->crp_sid
= sav
->tdb_cryptoid
;
179 crp
->crp_opaque
= (caddr_t
) tc
;
181 /* These are passed as-is to the callback */
182 tc
->tc_spi
= sav
->spi
;
183 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
184 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
185 tc
->tc_protoff
= protoff
;
188 return crypto_dispatch(crp
);
192 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
193 if (saidx->dst.sa.sa_family == AF_INET6) { \
194 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
200 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
201 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
205 * IPComp input callback from the crypto driver.
208 ipcomp_input_cb(struct cryptop
*crp
)
210 struct cryptodesc
*crd
;
211 struct tdb_crypto
*tc
;
215 struct secasvar
*sav
;
216 struct secasindex
*saidx
;
217 int hlen
= IPCOMP_HLENGTH
, error
, clen
;
223 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
224 KASSERT(tc
!= NULL
, ("ipcomp_input_cb: null opaque crypto data area!"));
226 protoff
= tc
->tc_protoff
;
227 mtag
= (struct mtag
*) tc
->tc_ptr
;
228 m
= (struct mbuf
*) crp
->crp_buf
;
232 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
);
234 ipcompstat
.ipcomps_notdb
++;
235 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
236 error
= ENOBUFS
; /*XXX*/
240 saidx
= &sav
->sah
->saidx
;
241 KASSERT(saidx
->dst
.sa
.sa_family
== AF_INET
||
242 saidx
->dst
.sa
.sa_family
== AF_INET6
,
243 ("ah_input_cb: unexpected protocol family %u",
244 saidx
->dst
.sa
.sa_family
));
246 /* Check for crypto errors */
247 if (crp
->crp_etype
) {
248 /* Reset the session ID */
249 if (sav
->tdb_cryptoid
!= 0)
250 sav
->tdb_cryptoid
= crp
->crp_sid
;
252 if (crp
->crp_etype
== EAGAIN
) {
255 return crypto_dispatch(crp
);
258 ipcompstat
.ipcomps_noxform
++;
259 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp
->crp_etype
));
260 error
= crp
->crp_etype
;
263 /* Shouldn't happen... */
265 ipcompstat
.ipcomps_crypto
++;
266 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
270 ipcompstat
.ipcomps_hist
[sav
->alg_comp
]++;
272 clen
= crp
->crp_olen
; /* Length of data after processing */
274 /* Release the crypto descriptors */
275 kfree(tc
, M_XDATA
), tc
= NULL
;
276 crypto_freereq(crp
), crp
= NULL
;
278 /* In case it's not done already, adjust the size of the mbuf chain */
279 m
->m_pkthdr
.len
= clen
+ hlen
+ skip
;
281 if (m
->m_len
< skip
+ hlen
&& (m
= m_pullup(m
, skip
+ hlen
)) == 0) {
282 ipcompstat
.ipcomps_hdrops
++; /*XXX*/
283 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
284 error
= EINVAL
; /*XXX*/
288 /* Keep the next protocol field */
289 addr
= (caddr_t
) mtod(m
, struct ip
*) + skip
;
290 nproto
= ((struct ipcomp
*) addr
)->comp_nxt
;
292 /* Remove the IPCOMP header */
293 error
= m_striphdr(m
, skip
, hlen
);
295 ipcompstat
.ipcomps_hdrops
++;
296 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
297 ipsec_address(&sav
->sah
->saidx
.dst
),
298 (u_long
) ntohl(sav
->spi
)));
302 /* Restore the Next Protocol field */
303 m_copyback(m
, protoff
, sizeof (u_int8_t
), (u_int8_t
*) &nproto
);
305 IPSEC_COMMON_INPUT_CB(m
, sav
, skip
, protoff
, NULL
);
324 * IPComp output routine, called by ipsec[46]_process_packet()
329 struct ipsecrequest
*isr
,
335 struct secasvar
*sav
;
336 struct comp_algo
*ipcompx
;
337 int error
, ralen
, hlen
, maxpacketsize
, roff
;
339 struct cryptodesc
*crdc
;
341 struct tdb_crypto
*tc
;
343 struct ipcomp
*ipcomp
;
346 KASSERT(sav
!= NULL
, ("ipcomp_output: null SA"));
347 ipcompx
= sav
->tdb_compalgxform
;
348 KASSERT(ipcompx
!= NULL
, ("ipcomp_output: null compression xform"));
350 ralen
= m
->m_pkthdr
.len
- skip
; /* Raw payload length before comp. */
351 hlen
= IPCOMP_HLENGTH
;
353 ipcompstat
.ipcomps_output
++;
355 /* Check for maximum packet size violations. */
356 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
359 maxpacketsize
= IP_MAXPACKET
;
364 maxpacketsize
= IPV6_MAXPACKET
;
368 ipcompstat
.ipcomps_nopf
++;
369 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
371 sav
->sah
->saidx
.dst
.sa
.sa_family
,
372 ipsec_address(&sav
->sah
->saidx
.dst
),
373 (u_long
) ntohl(sav
->spi
)));
374 error
= EPFNOSUPPORT
;
377 if (skip
+ hlen
+ ralen
> maxpacketsize
) {
378 ipcompstat
.ipcomps_toobig
++;
379 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
380 "(len %u, max len %u)\n",
381 ipsec_address(&sav
->sah
->saidx
.dst
),
382 (u_long
) ntohl(sav
->spi
),
383 skip
+ hlen
+ ralen
, maxpacketsize
));
388 /* Update the counters */
389 ipcompstat
.ipcomps_obytes
+= m
->m_pkthdr
.len
- skip
;
393 ipcompstat
.ipcomps_hdrops
++;
394 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
395 ipsec_address(&sav
->sah
->saidx
.dst
),
396 (u_long
) ntohl(sav
->spi
)));
401 /* Inject IPCOMP header */
402 mo
= m_makespace(m
, skip
, hlen
, &roff
);
404 ipcompstat
.ipcomps_wrap
++;
405 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
407 ipsec_address(&sav
->sah
->saidx
.dst
),
408 (u_long
) ntohl(sav
->spi
)));
412 ipcomp
= (struct ipcomp
*)(mtod(mo
, caddr_t
) + roff
);
414 /* Initialize the IPCOMP header */
415 /* XXX alignment always correct? */
416 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
419 ipcomp
->comp_nxt
= mtod(m
, struct ip
*)->ip_p
;
424 ipcomp
->comp_nxt
= mtod(m
, struct ip6_hdr
*)->ip6_nxt
;
428 ipcomp
->comp_flags
= 0;
429 ipcomp
->comp_cpi
= htons((u_int16_t
) ntohl(sav
->spi
));
431 /* Fix Next Protocol in IPv4/IPv6 header */
432 prot
= IPPROTO_IPCOMP
;
433 m_copyback(m
, protoff
, sizeof(u_int8_t
), (u_char
*) &prot
);
435 /* Ok now, we can pass to the crypto processing */
437 /* Get crypto descriptors */
438 crp
= crypto_getreq(1);
440 ipcompstat
.ipcomps_crypto
++;
441 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
445 crdc
= crp
->crp_desc
;
447 /* Compression descriptor */
448 crdc
->crd_skip
= skip
+ hlen
;
449 crdc
->crd_len
= m
->m_pkthdr
.len
- (skip
+ hlen
);
450 crdc
->crd_flags
= CRD_F_COMP
;
451 crdc
->crd_inject
= skip
+ hlen
;
453 /* Compression operation */
454 crdc
->crd_alg
= ipcompx
->type
;
456 /* IPsec-specific opaque crypto info */
457 tc
= kmalloc(sizeof(struct tdb_crypto
),
458 M_XDATA
, M_INTWAIT
| M_ZERO
| M_NULLOK
);
460 ipcompstat
.ipcomps_crypto
++;
461 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
468 tc
->tc_spi
= sav
->spi
;
469 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
470 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
471 tc
->tc_skip
= skip
+ hlen
;
473 /* Crypto operation descriptor */
474 crp
->crp_ilen
= m
->m_pkthdr
.len
; /* Total input length */
475 crp
->crp_flags
= CRYPTO_F_IMBUF
;
476 crp
->crp_buf
= (caddr_t
) m
;
477 crp
->crp_callback
= ipcomp_output_cb
;
478 crp
->crp_opaque
= (caddr_t
) tc
;
479 crp
->crp_sid
= sav
->tdb_cryptoid
;
481 return crypto_dispatch(crp
);
489 * IPComp output callback from the crypto driver.
492 ipcomp_output_cb(struct cryptop
*crp
)
494 struct tdb_crypto
*tc
;
495 struct ipsecrequest
*isr
;
496 struct secasvar
*sav
;
498 int error
, skip
, rlen
;
500 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
501 KASSERT(tc
!= NULL
, ("ipcomp_output_cb: null opaque data area!"));
502 m
= (struct mbuf
*) crp
->crp_buf
;
504 rlen
= crp
->crp_ilen
- skip
;
509 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
);
511 ipcompstat
.ipcomps_notdb
++;
512 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
513 error
= ENOBUFS
; /*XXX*/
516 KASSERT(isr
->sav
== sav
, ("ipcomp_output_cb: SA changed\n"));
518 /* Check for crypto errors */
519 if (crp
->crp_etype
) {
520 /* Reset session ID */
521 if (sav
->tdb_cryptoid
!= 0)
522 sav
->tdb_cryptoid
= crp
->crp_sid
;
524 if (crp
->crp_etype
== EAGAIN
) {
527 return crypto_dispatch(crp
);
529 ipcompstat
.ipcomps_noxform
++;
530 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp
->crp_etype
));
531 error
= crp
->crp_etype
;
534 /* Shouldn't happen... */
536 ipcompstat
.ipcomps_crypto
++;
537 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
541 ipcompstat
.ipcomps_hist
[sav
->alg_comp
]++;
543 if (rlen
> crp
->crp_olen
) {
544 /* Adjust the length in the IP header */
545 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
548 mtod(m
, struct ip
*)->ip_len
= htons(m
->m_pkthdr
.len
);
553 mtod(m
, struct ip6_hdr
*)->ip6_plen
=
554 htons(m
->m_pkthdr
.len
) - sizeof(struct ip6_hdr
);
558 ipcompstat
.ipcomps_nopf
++;
559 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
560 "family %d, IPCA %s/%08lx\n",
561 sav
->sah
->saidx
.dst
.sa
.sa_family
,
562 ipsec_address(&sav
->sah
->saidx
.dst
),
563 (u_long
) ntohl(sav
->spi
)));
564 error
= EPFNOSUPPORT
;
568 /* compression was useless, we have lost time */
569 /* XXX add statistic */
572 /* Release the crypto descriptor */
576 /* NB: m is reclaimed by ipsec_process_done. */
577 error
= ipsec_process_done(m
, isr
);
592 static struct xformsw ipcomp_xformsw
= {
593 XF_IPCOMP
, XFT_COMP
, "IPcomp",
594 ipcomp_init
, ipcomp_zeroize
, ipcomp_input
,
601 xform_register(&ipcomp_xformsw
);
603 SYSINIT(ipcomp_xform_init
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, ipcomp_attach
, NULL
)