1 /* $NetBSD: xform_ipcomp.c,v 1.18 2008/04/23 06:09:05 thorpej Exp $ */
2 /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.1 2003/01/24 05:11:36 sam 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.18 2008/04/23 06:09:05 thorpej Exp $");
35 /* IP payload compression protocol (IPComp), see RFC 2393 */
38 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/protosw.h>
47 #include <sys/sysctl.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
54 #include <net/route.h>
55 #include <netipsec/ipsec.h>
56 #include <netipsec/ipsec_private.h>
57 #include <netipsec/xform.h>
60 #include <netinet/ip6.h>
61 #include <netipsec/ipsec6.h>
64 #include <netipsec/ipcomp.h>
65 #include <netipsec/ipcomp_var.h>
67 #include <netipsec/key.h>
68 #include <netipsec/key_debug.h>
70 #include <netipsec/ipsec_osdep.h>
72 #include <opencrypto/cryptodev.h>
73 #include <opencrypto/deflate.h>
74 #include <opencrypto/xform.h>
76 percpu_t
*ipcompstat_percpu
;
78 int ipcomp_enable
= 1;
81 SYSCTL_DECL(_net_inet_ipcomp
);
82 SYSCTL_INT(_net_inet_ipcomp
, OID_AUTO
,
83 ipcomp_enable
, CTLFLAG_RW
, &ipcomp_enable
, 0, "");
84 SYSCTL_STRUCT(_net_inet_ipcomp
, IPSECCTL_STATS
,
85 stats
, CTLFLAG_RD
, &ipcompstat
, ipcompstat
, "");
86 #endif /* __FreeBSD__ */
88 static int ipcomp_input_cb(struct cryptop
*crp
);
89 static int ipcomp_output_cb(struct cryptop
*crp
);
92 ipcomp_algorithm_lookup(int alg
)
94 if (alg
>= IPCOMP_ALG_MAX
)
97 case SADB_X_CALG_DEFLATE
:
98 return &comp_algo_deflate
;
104 * ipcomp_init() is called when an CPI is being set up.
107 ipcomp_init(struct secasvar
*sav
, struct xformsw
*xsp
)
109 struct comp_algo
*tcomp
;
110 struct cryptoini cric
;
113 /* NB: algorithm really comes in alg_enc and not alg_comp! */
114 tcomp
= ipcomp_algorithm_lookup(sav
->alg_enc
);
116 DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n",
120 sav
->alg_comp
= sav
->alg_enc
; /* set for doing histogram */
121 sav
->tdb_xform
= xsp
;
122 sav
->tdb_compalgxform
= tcomp
;
124 /* Initialize crypto session */
125 memset(&cric
, 0, sizeof (cric
));
126 cric
.cri_alg
= sav
->tdb_compalgxform
->type
;
128 mutex_spin_enter(&crypto_mtx
);
129 ses
= crypto_newsession(&sav
->tdb_cryptoid
, &cric
, crypto_support
);
130 mutex_spin_exit(&crypto_mtx
);
135 * ipcomp_zeroize() used when IPCA is deleted
138 ipcomp_zeroize(struct secasvar
*sav
)
142 mutex_spin_enter(&crypto_mtx
);
143 err
= crypto_freesession(sav
->tdb_cryptoid
);
144 mutex_spin_exit(&crypto_mtx
);
145 sav
->tdb_cryptoid
= 0;
150 * ipcomp_input() gets called to uncompress an input packet
153 ipcomp_input(struct mbuf
*m
, struct secasvar
*sav
, int skip
, int protoff
)
155 struct tdb_crypto
*tc
;
156 struct cryptodesc
*crdc
;
158 int hlen
= IPCOMP_HLENGTH
;
160 IPSEC_SPLASSERT_SOFTNET("ipcomp_input");
162 /* Get crypto descriptors */
163 crp
= crypto_getreq(1);
166 DPRINTF(("ipcomp_input: no crypto descriptors\n"));
167 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
170 /* Get IPsec-specific opaque pointer */
171 tc
= (struct tdb_crypto
*) malloc(sizeof (*tc
), M_XDATA
, M_NOWAIT
|M_ZERO
);
175 DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n"));
176 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
179 crdc
= crp
->crp_desc
;
181 crdc
->crd_skip
= skip
+ hlen
;
182 crdc
->crd_len
= m
->m_pkthdr
.len
- (skip
+ hlen
);
183 crdc
->crd_inject
= skip
;
187 /* Decompression operation */
188 crdc
->crd_alg
= sav
->tdb_compalgxform
->type
;
190 /* Crypto operation descriptor */
191 crp
->crp_ilen
= m
->m_pkthdr
.len
- (skip
+ hlen
);
192 crp
->crp_flags
= CRYPTO_F_IMBUF
;
194 crp
->crp_callback
= ipcomp_input_cb
;
195 crp
->crp_sid
= sav
->tdb_cryptoid
;
196 crp
->crp_opaque
= tc
;
198 /* These are passed as-is to the callback */
199 tc
->tc_spi
= sav
->spi
;
200 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
201 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
202 tc
->tc_protoff
= protoff
;
205 return crypto_dispatch(crp
);
209 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
210 if (saidx->dst.sa.sa_family == AF_INET6) { \
211 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
213 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
217 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
218 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
222 * IPComp input callback from the crypto driver.
225 ipcomp_input_cb(struct cryptop
*crp
)
227 struct cryptodesc
*crd
;
228 struct tdb_crypto
*tc
;
232 struct secasvar
*sav
;
233 struct secasindex
*saidx
;
234 int s
, hlen
= IPCOMP_HLENGTH
, error
, clen
;
240 struct m_tag
* tag
= NULL
;
245 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
246 IPSEC_ASSERT(tc
!= NULL
, ("ipcomp_input_cb: null opaque crypto data area!"));
248 protoff
= tc
->tc_protoff
;
249 mtag
= (struct mtag
*) tc
->tc_ptr
;
250 m
= (struct mbuf
*) crp
->crp_buf
;
253 /* find the source port for NAT-T */
254 if ((tag
= m_tag_find(m
, PACKET_TAG_IPSEC_NAT_T_PORTS
, NULL
))) {
255 sport
= ((u_int16_t
*)(tag
+ 1))[0];
256 dport
= ((u_int16_t
*)(tag
+ 1))[1];
262 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
, sport
, dport
);
264 IPCOMP_STATINC(IPCOMP_STAT_NOTDB
);
265 DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n"));
266 error
= ENOBUFS
; /*XXX*/
270 saidx
= &sav
->sah
->saidx
;
271 IPSEC_ASSERT(saidx
->dst
.sa
.sa_family
== AF_INET
||
272 saidx
->dst
.sa
.sa_family
== AF_INET6
,
273 ("ah_input_cb: unexpected protocol family %u",
274 saidx
->dst
.sa
.sa_family
));
276 /* Check for crypto errors */
277 if (crp
->crp_etype
) {
278 /* Reset the session ID */
279 if (sav
->tdb_cryptoid
!= 0)
280 sav
->tdb_cryptoid
= crp
->crp_sid
;
282 if (crp
->crp_etype
== EAGAIN
) {
285 return crypto_dispatch(crp
);
288 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM
);
289 DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp
->crp_etype
));
290 error
= crp
->crp_etype
;
293 /* Shouldn't happen... */
295 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
296 DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n"));
300 IPCOMP_STATINC(IPCOMP_STAT_HIST
+ sav
->alg_comp
);
302 clen
= crp
->crp_olen
; /* Length of data after processing */
304 /* Release the crypto descriptors */
305 free(tc
, M_XDATA
), tc
= NULL
;
306 crypto_freereq(crp
), crp
= NULL
;
308 /* In case it's not done already, adjust the size of the mbuf chain */
309 m
->m_pkthdr
.len
= clen
+ hlen
+ skip
;
311 if (m
->m_len
< skip
+ hlen
&& (m
= m_pullup(m
, skip
+ hlen
)) == 0) {
312 IPCOMP_STATINC(IPCOMP_STAT_HDROPS
); /*XXX*/
313 DPRINTF(("ipcomp_input_cb: m_pullup failed\n"));
314 error
= EINVAL
; /*XXX*/
318 /* Keep the next protocol field */
319 addr
= (uint8_t*) mtod(m
, struct ip
*) + skip
;
320 nproto
= ((struct ipcomp
*) addr
)->comp_nxt
;
322 /* Remove the IPCOMP header */
323 error
= m_striphdr(m
, skip
, hlen
);
325 IPCOMP_STATINC(IPCOMP_STAT_HDROPS
);
326 DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n",
327 ipsec_address(&sav
->sah
->saidx
.dst
),
328 (u_long
) ntohl(sav
->spi
)));
332 /* Restore the Next Protocol field */
333 m_copyback(m
, protoff
, sizeof (u_int8_t
), (u_int8_t
*) &nproto
);
335 IPSEC_COMMON_INPUT_CB(m
, sav
, skip
, protoff
, NULL
);
354 * IPComp output routine, called by ipsec[46]_process_packet()
359 struct ipsecrequest
*isr
,
365 struct secasvar
*sav
;
366 struct comp_algo
*ipcompx
;
367 int error
, ralen
, hlen
, maxpacketsize
;
368 struct cryptodesc
*crdc
;
370 struct tdb_crypto
*tc
;
372 IPSEC_SPLASSERT_SOFTNET("ipcomp_output");
374 IPSEC_ASSERT(sav
!= NULL
, ("ipcomp_output: null SA"));
375 ipcompx
= sav
->tdb_compalgxform
;
376 IPSEC_ASSERT(ipcompx
!= NULL
, ("ipcomp_output: null compression xform"));
378 ralen
= m
->m_pkthdr
.len
- skip
; /* Raw payload length before comp. */
380 /* Don't process the packet if it is too short */
381 if (ralen
< ipcompx
->minlen
) {
382 IPCOMP_STATINC(IPCOMP_STAT_MINLEN
);
383 return ipsec_process_done(m
,isr
);
386 hlen
= IPCOMP_HLENGTH
;
388 IPCOMP_STATINC(IPCOMP_STAT_OUTPUT
);
390 /* Check for maximum packet size violations. */
391 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
394 maxpacketsize
= IP_MAXPACKET
;
399 maxpacketsize
= IPV6_MAXPACKET
;
403 IPCOMP_STATINC(IPCOMP_STAT_NOPF
);
404 DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d"
406 sav
->sah
->saidx
.dst
.sa
.sa_family
,
407 ipsec_address(&sav
->sah
->saidx
.dst
),
408 (u_long
) ntohl(sav
->spi
)));
409 error
= EPFNOSUPPORT
;
412 if (skip
+ hlen
+ ralen
> maxpacketsize
) {
413 IPCOMP_STATINC(IPCOMP_STAT_TOOBIG
);
414 DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big "
415 "(len %u, max len %u)\n",
416 ipsec_address(&sav
->sah
->saidx
.dst
),
417 (u_long
) ntohl(sav
->spi
),
418 skip
+ hlen
+ ralen
, maxpacketsize
));
423 /* Update the counters */
424 IPCOMP_STATADD(IPCOMP_STAT_OBYTES
, m
->m_pkthdr
.len
- skip
);
428 IPCOMP_STATINC(IPCOMP_STAT_HDROPS
);
429 DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n",
430 ipsec_address(&sav
->sah
->saidx
.dst
),
431 (u_long
) ntohl(sav
->spi
)));
436 /* Ok now, we can pass to the crypto processing */
438 /* Get crypto descriptors */
439 crp
= crypto_getreq(1);
441 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
442 DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n"));
446 crdc
= crp
->crp_desc
;
448 /* Compression descriptor */
449 crdc
->crd_skip
= skip
;
450 crdc
->crd_len
= m
->m_pkthdr
.len
- skip
;
451 crdc
->crd_flags
= CRD_F_COMP
;
452 crdc
->crd_inject
= skip
;
454 /* Compression operation */
455 crdc
->crd_alg
= ipcompx
->type
;
457 /* IPsec-specific opaque crypto info */
458 tc
= (struct tdb_crypto
*) malloc(sizeof(struct tdb_crypto
),
459 M_XDATA
, M_NOWAIT
|M_ZERO
);
461 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
462 DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n"));
469 tc
->tc_spi
= sav
->spi
;
470 tc
->tc_dst
= sav
->sah
->saidx
.dst
;
471 tc
->tc_proto
= sav
->sah
->saidx
.proto
;
473 tc
->tc_protoff
= protoff
;
475 /* Crypto operation descriptor */
476 crp
->crp_ilen
= m
->m_pkthdr
.len
; /* Total input length */
477 crp
->crp_flags
= CRYPTO_F_IMBUF
;
479 crp
->crp_callback
= ipcomp_output_cb
;
480 crp
->crp_opaque
= tc
;
481 crp
->crp_sid
= sav
->tdb_cryptoid
;
483 return crypto_dispatch(crp
);
491 * IPComp output callback from the crypto driver.
494 ipcomp_output_cb(struct cryptop
*crp
)
496 struct tdb_crypto
*tc
;
497 struct ipsecrequest
*isr
;
498 struct secasvar
*sav
;
500 int s
, error
, skip
, rlen
, roff
;
503 struct ipcomp
* ipcomp
;
506 tc
= (struct tdb_crypto
*) crp
->crp_opaque
;
507 IPSEC_ASSERT(tc
!= NULL
, ("ipcomp_output_cb: null opaque data area!"));
508 m
= (struct mbuf
*) crp
->crp_buf
;
510 rlen
= crp
->crp_ilen
- skip
;
515 sav
= KEY_ALLOCSA(&tc
->tc_dst
, tc
->tc_proto
, tc
->tc_spi
, 0, 0);
517 IPCOMP_STATINC(IPCOMP_STAT_NOTDB
);
518 DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n"));
519 error
= ENOBUFS
; /*XXX*/
522 IPSEC_ASSERT(isr
->sav
== sav
, ("ipcomp_output_cb: SA changed\n"));
524 /* Check for crypto errors */
525 if (crp
->crp_etype
) {
526 /* Reset session ID */
527 if (sav
->tdb_cryptoid
!= 0)
528 sav
->tdb_cryptoid
= crp
->crp_sid
;
530 if (crp
->crp_etype
== EAGAIN
) {
533 return crypto_dispatch(crp
);
535 IPCOMP_STATINC(IPCOMP_STAT_NOXFORM
);
536 DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp
->crp_etype
));
537 error
= crp
->crp_etype
;
540 /* Shouldn't happen... */
542 IPCOMP_STATINC(IPCOMP_STAT_CRYPTO
);
543 DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n"));
547 IPCOMP_STATINC(IPCOMP_STAT_HIST
+ sav
->alg_comp
);
549 if (rlen
> crp
->crp_olen
) {
550 /* Inject IPCOMP header */
551 mo
= m_makespace(m
, skip
, IPCOMP_HLENGTH
, &roff
);
553 IPCOMP_STATINC(IPCOMP_STAT_WRAP
);
554 DPRINTF(("ipcomp_output: failed to inject IPCOMP header for "
556 ipsec_address(&sav
->sah
->saidx
.dst
),
557 (u_long
) ntohl(sav
->spi
)));
561 ipcomp
= (struct ipcomp
*)(mtod(mo
, char *) + roff
);
563 /* Initialize the IPCOMP header */
564 /* XXX alignment always correct? */
565 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
568 ipcomp
->comp_nxt
= mtod(m
, struct ip
*)->ip_p
;
573 ipcomp
->comp_nxt
= mtod(m
, struct ip6_hdr
*)->ip6_nxt
;
577 ipcomp
->comp_flags
= 0;
579 if ((sav
->flags
& SADB_X_EXT_RAWCPI
) == 0)
582 cpi
= ntohl(sav
->spi
) & 0xffff;
583 ipcomp
->comp_cpi
= htons(cpi
);
585 /* Fix Next Protocol in IPv4/IPv6 header */
586 prot
= IPPROTO_IPCOMP
;
587 m_copyback(m
, tc
->tc_protoff
, sizeof(u_int8_t
), (u_char
*)&prot
);
589 /* Adjust the length in the IP header */
590 switch (sav
->sah
->saidx
.dst
.sa
.sa_family
) {
593 mtod(m
, struct ip
*)->ip_len
= htons(m
->m_pkthdr
.len
);
598 mtod(m
, struct ip6_hdr
*)->ip6_plen
=
599 htons(m
->m_pkthdr
.len
) - sizeof(struct ip6_hdr
);
603 IPCOMP_STATINC(IPCOMP_STAT_NOPF
);
604 DPRINTF(("ipcomp_output: unknown/unsupported protocol "
605 "family %d, IPCA %s/%08lx\n",
606 sav
->sah
->saidx
.dst
.sa
.sa_family
,
607 ipsec_address(&sav
->sah
->saidx
.dst
),
608 (u_long
) ntohl(sav
->spi
)));
609 error
= EPFNOSUPPORT
;
613 /* compression was useless, we have lost time */
614 IPCOMP_STATINC(IPCOMP_STAT_USELESS
);
615 DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d"
616 "and compressed size is %d\n", rlen
, crp
->crp_olen
));
620 /* Release the crypto descriptor */
624 /* NB: m is reclaimed by ipsec_process_done. */
625 error
= ipsec_process_done(m
, isr
);
640 static struct xformsw ipcomp_xformsw
= {
641 XF_IPCOMP
, XFT_COMP
, "IPcomp",
642 ipcomp_init
, ipcomp_zeroize
, ipcomp_input
,
650 ipcompstat_percpu
= percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS
);
651 xform_register(&ipcomp_xformsw
);
655 SYSINIT(ipcomp_xform_init
, SI_SUB_DRIVERS
, SI_ORDER_FIRST
, ipcomp_attach
, NULL
)
656 #endif /* __FreeBSD__ */