| Commit | Line | Data |
|---|---|---|
| 984263bc | 1 | /* $FreeBSD: src/sys/netipsec/xform_ipcomp.c,v 1.1.4.2 2003/02/26 00:14:06 sam Exp $ */ |
| 896d25fc | 2 | /* $DragonFly: src/sys/netproto/ipsec/xform_ipcomp.c,v 1.10 2006/10/12 01:32:51 hsu Exp $ */ |
| 984263bc MD |
3 | /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */ |
| 4 | ||
| 5 | /* | |
| 6 | * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org) | |
| 7 | * | |
| 8 | * Redistribution and use in source and binary forms, with or without | |
| 9 | * modification, are permitted provided that the following conditions | |
| 10 | * are met: | |
| 11 | * | |
| 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. | |
| 19 | * | |
| 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. | |
| 30 | */ | |
| 31 | ||
| 32 | /* IP payload compression protocol (IPComp), see RFC 2393 */ | |
| 33 | #include "opt_inet.h" | |
| 34 | #include "opt_inet6.h" | |
| 35 | ||
| 36 | #include <sys/param.h> | |
| 37 | #include <sys/systm.h> | |
| 38 | #include <sys/mbuf.h> | |
| 39 | #include <sys/socket.h> | |
| 40 | #include <sys/kernel.h> | |
| 41 | #include <sys/protosw.h> | |
| 42 | #include <sys/sysctl.h> | |
| 221b8f43 | 43 | #include <sys/thread2.h> |
| 984263bc MD |
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> | |
| bf844ffa JH |
51 | #include <netproto/ipsec/ipsec.h> |
| 52 | #include <netproto/ipsec/xform.h> | |
| 984263bc MD |
53 | |
| 54 | #ifdef INET6 | |
| 55 | #include <netinet/ip6.h> | |
| bf844ffa | 56 | #include <netproto/ipsec/ipsec6.h> |
| 984263bc MD |
57 | #endif |
| 58 | ||
| bf844ffa JH |
59 | #include <netproto/ipsec/ipcomp.h> |
| 60 | #include <netproto/ipsec/ipcomp_var.h> | |
| 984263bc | 61 | |
| bf844ffa JH |
62 | #include <netproto/ipsec/key.h> |
| 63 | #include <netproto/ipsec/key_debug.h> | |
| 984263bc MD |
64 | |
| 65 | #include <opencrypto/cryptodev.h> | |
| 66 | #include <opencrypto/deflate.h> | |
| 67 | #include <opencrypto/xform.h> | |
| 68 | ||
| 69 | int ipcomp_enable = 0; | |
| 70 | struct ipcompstat ipcompstat; | |
| 71 | ||
| 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, ""); | |
| 77 | ||
| 78 | static int ipcomp_input_cb(struct cryptop *crp); | |
| 79 | static int ipcomp_output_cb(struct cryptop *crp); | |
| 80 | ||
| 81 | struct comp_algo * | |
| 82 | ipcomp_algorithm_lookup(int alg) | |
| 83 | { | |
| 84 | if (alg >= IPCOMP_ALG_MAX) | |
| 85 | return NULL; | |
| 86 | switch (alg) { | |
| 87 | case SADB_X_CALG_DEFLATE: | |
| 88 | return &comp_algo_deflate; | |
| 89 | } | |
| 90 | return NULL; | |
| 91 | } | |
| 92 | ||
| 93 | /* | |
| 94 | * ipcomp_init() is called when an CPI is being set up. | |
| 95 | */ | |
| 96 | static int | |
| 97 | ipcomp_init(struct secasvar *sav, struct xformsw *xsp) | |
| 98 | { | |
| 99 | struct comp_algo *tcomp; | |
| 100 | struct cryptoini cric; | |
| 101 | ||
| 102 | /* NB: algorithm really comes in alg_enc and not alg_comp! */ | |
| 103 | tcomp = ipcomp_algorithm_lookup(sav->alg_enc); | |
| 104 | if (tcomp == NULL) { | |
| 105 | DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n", | |
| 106 | sav->alg_comp)); | |
| 107 | return EINVAL; | |
| 108 | } | |
| 109 | sav->alg_comp = sav->alg_enc; /* set for doing histogram */ | |
| 110 | sav->tdb_xform = xsp; | |
| 111 | sav->tdb_compalgxform = tcomp; | |
| 112 | ||
| 113 | /* Initialize crypto session */ | |
| 114 | bzero(&cric, sizeof (cric)); | |
| 115 | cric.cri_alg = sav->tdb_compalgxform->type; | |
| 116 | ||
| 117 | return crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support); | |
| 118 | } | |
| 119 | ||
| 120 | /* | |
| 121 | * ipcomp_zeroize() used when IPCA is deleted | |
| 122 | */ | |
| 123 | static int | |
| 124 | ipcomp_zeroize(struct secasvar *sav) | |
| 125 | { | |
| 126 | int err; | |
| 127 | ||
| 128 | err = crypto_freesession(sav->tdb_cryptoid); | |
| 129 | sav->tdb_cryptoid = 0; | |
| 130 | return err; | |
| 131 | } | |
| 132 | ||
| 133 | /* | |
| 134 | * ipcomp_input() gets called to uncompress an input packet | |
| 135 | */ | |
| 136 | static int | |
| 137 | ipcomp_input(struct mbuf *m, struct secasvar *sav, int skip, int protoff) | |
| 138 | { | |
| 139 | struct tdb_crypto *tc; | |
| 140 | struct cryptodesc *crdc; | |
| 141 | struct cryptop *crp; | |
| 1327d18c MD |
142 | struct ipcomp *ipcomp; |
| 143 | caddr_t addr; | |
| 984263bc MD |
144 | int hlen = IPCOMP_HLENGTH; |
| 145 | ||
| 1327d18c MD |
146 | /* |
| 147 | * Check that the next header of the IPComp is not IPComp again, before | |
| 148 | * doing any real work. Given it is not possible to do double | |
| 149 | * compression it means someone is playing tricks on us. | |
| 150 | */ | |
| 151 | if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == NULL) { | |
| 152 | ipcompstat.ipcomps_hdrops++; /*XXX*/ | |
| 153 | DPRINTF(("%s: m_pullup failed\n", __func__)); | |
| 154 | return (ENOBUFS); | |
| 155 | } | |
| 156 | addr = (caddr_t) mtod(m, struct ip *) + skip; | |
| 157 | ipcomp = (struct ipcomp *)addr; | |
| 158 | if (ipcomp->comp_nxt == IPPROTO_IPCOMP) { | |
| 159 | m_freem(m); | |
| 160 | ipcompstat.ipcomps_pdrops++; /* XXX have our own stats? */ | |
| 161 | DPRINTF(("%s: recursive compression detected\n", __func__)); | |
| 162 | return (EINVAL); | |
| 163 | } | |
| 164 | ||
| 984263bc MD |
165 | /* Get crypto descriptors */ |
| 166 | crp = crypto_getreq(1); | |
| 167 | if (crp == NULL) { | |
| 168 | m_freem(m); | |
| 169 | DPRINTF(("ipcomp_input: no crypto descriptors\n")); | |
| 170 | ipcompstat.ipcomps_crypto++; | |
| 171 | return ENOBUFS; | |
| 172 | } | |
| 173 | /* Get IPsec-specific opaque pointer */ | |
| efda3bd0 | 174 | tc = kmalloc(sizeof (*tc), M_XDATA, |
| 062eab50 | 175 | M_INTWAIT | M_ZERO | M_NULLOK); |
| 984263bc MD |
176 | if (tc == NULL) { |
| 177 | m_freem(m); | |
| 178 | crypto_freereq(crp); | |
| 179 | DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n")); | |
| 180 | ipcompstat.ipcomps_crypto++; | |
| 181 | return ENOBUFS; | |
| 182 | } | |
| 183 | crdc = crp->crp_desc; | |
| 184 | ||
| 185 | crdc->crd_skip = skip + hlen; | |
| 186 | crdc->crd_len = m->m_pkthdr.len - (skip + hlen); | |
| 187 | crdc->crd_inject = skip; | |
| 188 | ||
| 189 | tc->tc_ptr = 0; | |
| 190 | ||
| 191 | /* Decompression operation */ | |
| 192 | crdc->crd_alg = sav->tdb_compalgxform->type; | |
| 193 | ||
| 194 | /* Crypto operation descriptor */ | |
| 195 | crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); | |
| 196 | crp->crp_flags = CRYPTO_F_IMBUF; | |
| 197 | crp->crp_buf = (caddr_t) m; | |
| 198 | crp->crp_callback = ipcomp_input_cb; | |
| 199 | crp->crp_sid = sav->tdb_cryptoid; | |
| 200 | crp->crp_opaque = (caddr_t) tc; | |
| 201 | ||
| 202 | /* These are passed as-is to the callback */ | |
| 203 | tc->tc_spi = sav->spi; | |
| 204 | tc->tc_dst = sav->sah->saidx.dst; | |
| 205 | tc->tc_proto = sav->sah->saidx.proto; | |
| 206 | tc->tc_protoff = protoff; | |
| 207 | tc->tc_skip = skip; | |
| 208 | ||
| 209 | return crypto_dispatch(crp); | |
| 210 | } | |
| 211 | ||
| 212 | #ifdef INET6 | |
| 213 | #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ | |
| 214 | if (saidx->dst.sa.sa_family == AF_INET6) { \ | |
| 215 | error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ | |
| 216 | } else { \ | |
| 217 | error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ | |
| 218 | } \ | |
| 219 | } while (0) | |
| 220 | #else | |
| 221 | #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ | |
| 222 | (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) | |
| 223 | #endif | |
| 224 | ||
| 225 | /* | |
| 226 | * IPComp input callback from the crypto driver. | |
| 227 | */ | |
| 228 | static int | |
| 229 | ipcomp_input_cb(struct cryptop *crp) | |
| 230 | { | |
| 231 | struct cryptodesc *crd; | |
| 232 | struct tdb_crypto *tc; | |
| 233 | int skip, protoff; | |
| 234 | struct mtag *mtag; | |
| 235 | struct mbuf *m; | |
| 236 | struct secasvar *sav; | |
| 237 | struct secasindex *saidx; | |
| cb753a0e | 238 | int hlen = IPCOMP_HLENGTH, error, clen; |
| 984263bc MD |
239 | u_int8_t nproto; |
| 240 | caddr_t addr; | |
| 241 | ||
| 242 | crd = crp->crp_desc; | |
| 243 | ||
| 244 | tc = (struct tdb_crypto *) crp->crp_opaque; | |
| 245 | KASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!")); | |
| 246 | skip = tc->tc_skip; | |
| 247 | protoff = tc->tc_protoff; | |
| 248 | mtag = (struct mtag *) tc->tc_ptr; | |
| 249 | m = (struct mbuf *) crp->crp_buf; | |
| 250 | ||
| cb753a0e | 251 | crit_enter(); |
| 984263bc MD |
252 | |
| 253 | sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); | |
| 254 | if (sav == NULL) { | |
| 255 | ipcompstat.ipcomps_notdb++; | |
| 256 | DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n")); | |
| 257 | error = ENOBUFS; /*XXX*/ | |
| 258 | goto bad; | |
| 259 | } | |
| 260 | ||
| 261 | saidx = &sav->sah->saidx; | |
| 262 | KASSERT(saidx->dst.sa.sa_family == AF_INET || | |
| 263 | saidx->dst.sa.sa_family == AF_INET6, | |
| 264 | ("ah_input_cb: unexpected protocol family %u", | |
| 265 | saidx->dst.sa.sa_family)); | |
| 266 | ||
| 267 | /* Check for crypto errors */ | |
| 268 | if (crp->crp_etype) { | |
| 269 | /* Reset the session ID */ | |
| 270 | if (sav->tdb_cryptoid != 0) | |
| 271 | sav->tdb_cryptoid = crp->crp_sid; | |
| 272 | ||
| 273 | if (crp->crp_etype == EAGAIN) { | |
| 274 | KEY_FREESAV(&sav); | |
| cb753a0e | 275 | crit_exit(); |
| 984263bc MD |
276 | return crypto_dispatch(crp); |
| 277 | } | |
| 278 | ||
| 279 | ipcompstat.ipcomps_noxform++; | |
| 280 | DPRINTF(("ipcomp_input_cb: crypto error %d\n", crp->crp_etype)); | |
| 281 | error = crp->crp_etype; | |
| 282 | goto bad; | |
| 283 | } | |
| 284 | /* Shouldn't happen... */ | |
| 285 | if (m == NULL) { | |
| 286 | ipcompstat.ipcomps_crypto++; | |
| 287 | DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n")); | |
| 288 | error = EINVAL; | |
| 289 | goto bad; | |
| 290 | } | |
| 291 | ipcompstat.ipcomps_hist[sav->alg_comp]++; | |
| 292 | ||
| 293 | clen = crp->crp_olen; /* Length of data after processing */ | |
| 294 | ||
| 295 | /* Release the crypto descriptors */ | |
| efda3bd0 | 296 | kfree(tc, M_XDATA), tc = NULL; |
| 984263bc MD |
297 | crypto_freereq(crp), crp = NULL; |
| 298 | ||
| 299 | /* In case it's not done already, adjust the size of the mbuf chain */ | |
| 300 | m->m_pkthdr.len = clen + hlen + skip; | |
| 301 | ||
| 302 | if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { | |
| 303 | ipcompstat.ipcomps_hdrops++; /*XXX*/ | |
| 304 | DPRINTF(("ipcomp_input_cb: m_pullup failed\n")); | |
| 305 | error = EINVAL; /*XXX*/ | |
| 306 | goto bad; | |
| 307 | } | |
| 308 | ||
| 309 | /* Keep the next protocol field */ | |
| 310 | addr = (caddr_t) mtod(m, struct ip *) + skip; | |
| 311 | nproto = ((struct ipcomp *) addr)->comp_nxt; | |
| 312 | ||
| 313 | /* Remove the IPCOMP header */ | |
| 314 | error = m_striphdr(m, skip, hlen); | |
| 315 | if (error) { | |
| 316 | ipcompstat.ipcomps_hdrops++; | |
| 317 | DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n", | |
| 318 | ipsec_address(&sav->sah->saidx.dst), | |
| 319 | (u_long) ntohl(sav->spi))); | |
| 320 | goto bad; | |
| 321 | } | |
| 322 | ||
| 323 | /* Restore the Next Protocol field */ | |
| 324 | m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); | |
| 325 | ||
| 326 | IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL); | |
| 327 | ||
| 328 | KEY_FREESAV(&sav); | |
| cb753a0e | 329 | crit_exit(); |
| 984263bc MD |
330 | return error; |
| 331 | bad: | |
| 332 | if (sav) | |
| 333 | KEY_FREESAV(&sav); | |
| cb753a0e | 334 | crit_exit(); |
| 984263bc MD |
335 | if (m) |
| 336 | m_freem(m); | |
| 337 | if (tc != NULL) | |
| efda3bd0 | 338 | kfree(tc, M_XDATA); |
| 984263bc MD |
339 | if (crp) |
| 340 | crypto_freereq(crp); | |
| 341 | return error; | |
| 342 | } | |
| 343 | ||
| 344 | /* | |
| 345 | * IPComp output routine, called by ipsec[46]_process_packet() | |
| 346 | */ | |
| 347 | static int | |
| 348 | ipcomp_output( | |
| 349 | struct mbuf *m, | |
| 350 | struct ipsecrequest *isr, | |
| 351 | struct mbuf **mp, | |
| 352 | int skip, | |
| 353 | int protoff | |
| 354 | ) | |
| 355 | { | |
| 356 | struct secasvar *sav; | |
| 357 | struct comp_algo *ipcompx; | |
| 358 | int error, ralen, hlen, maxpacketsize, roff; | |
| 359 | u_int8_t prot; | |
| 360 | struct cryptodesc *crdc; | |
| 361 | struct cryptop *crp; | |
| 362 | struct tdb_crypto *tc; | |
| 363 | struct mbuf *mo; | |
| 364 | struct ipcomp *ipcomp; | |
| 365 | ||
| 984263bc MD |
366 | sav = isr->sav; |
| 367 | KASSERT(sav != NULL, ("ipcomp_output: null SA")); | |
| 368 | ipcompx = sav->tdb_compalgxform; | |
| 369 | KASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform")); | |
| 370 | ||
| 371 | ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ | |
| 372 | hlen = IPCOMP_HLENGTH; | |
| 373 | ||
| 374 | 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 | ipcompstat.ipcomps_nopf++; | |
| 390 | DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d" | |
| 391 | ", IPCA %s/%08lx\n", | |
| 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 (skip + hlen + ralen > maxpacketsize) { | |
| 399 | ipcompstat.ipcomps_toobig++; | |
| 400 | DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big " | |
| 401 | "(len %u, max len %u)\n", | |
| 402 | ipsec_address(&sav->sah->saidx.dst), | |
| 403 | (u_long) ntohl(sav->spi), | |
| 404 | skip + hlen + ralen, maxpacketsize)); | |
| 405 | error = EMSGSIZE; | |
| 406 | goto bad; | |
| 407 | } | |
| 408 | ||
| 409 | /* Update the counters */ | |
| 410 | ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip; | |
| 411 | ||
| 412 | m = m_clone(m); | |
| 413 | if (m == NULL) { | |
| 414 | ipcompstat.ipcomps_hdrops++; | |
| 415 | DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n", | |
| 416 | ipsec_address(&sav->sah->saidx.dst), | |
| 417 | (u_long) ntohl(sav->spi))); | |
| 418 | error = ENOBUFS; | |
| 419 | goto bad; | |
| 420 | } | |
| 421 | ||
| 422 | /* Inject IPCOMP header */ | |
| 423 | mo = m_makespace(m, skip, hlen, &roff); | |
| 424 | if (mo == NULL) { | |
| 425 | ipcompstat.ipcomps_wrap++; | |
| 426 | DPRINTF(("ipcomp_output: failed to inject IPCOMP header for " | |
| 427 | "IPCA %s/%08lx\n", | |
| 428 | ipsec_address(&sav->sah->saidx.dst), | |
| 429 | (u_long) ntohl(sav->spi))); | |
| 430 | error = ENOBUFS; | |
| 431 | goto bad; | |
| 432 | } | |
| 433 | ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff); | |
| 434 | ||
| 435 | /* Initialize the IPCOMP header */ | |
| 436 | /* XXX alignment always correct? */ | |
| 437 | switch (sav->sah->saidx.dst.sa.sa_family) { | |
| 438 | #ifdef INET | |
| 439 | case AF_INET: | |
| 440 | ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; | |
| 441 | break; | |
| 442 | #endif /* INET */ | |
| 443 | #ifdef INET6 | |
| 444 | case AF_INET6: | |
| 445 | ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; | |
| 446 | break; | |
| 447 | #endif | |
| 448 | } | |
| 449 | ipcomp->comp_flags = 0; | |
| 450 | ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi)); | |
| 451 | ||
| 452 | /* Fix Next Protocol in IPv4/IPv6 header */ | |
| 453 | prot = IPPROTO_IPCOMP; | |
| 454 | m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot); | |
| 455 | ||
| 456 | /* Ok now, we can pass to the crypto processing */ | |
| 457 | ||
| 458 | /* Get crypto descriptors */ | |
| 459 | crp = crypto_getreq(1); | |
| 460 | if (crp == NULL) { | |
| 461 | ipcompstat.ipcomps_crypto++; | |
| 462 | DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n")); | |
| 463 | error = ENOBUFS; | |
| 464 | goto bad; | |
| 465 | } | |
| 466 | crdc = crp->crp_desc; | |
| 467 | ||
| 468 | /* Compression descriptor */ | |
| 469 | crdc->crd_skip = skip + hlen; | |
| 470 | crdc->crd_len = m->m_pkthdr.len - (skip + hlen); | |
| 471 | crdc->crd_flags = CRD_F_COMP; | |
| 472 | crdc->crd_inject = skip + hlen; | |
| 473 | ||
| 474 | /* Compression operation */ | |
| 475 | crdc->crd_alg = ipcompx->type; | |
| 476 | ||
| 477 | /* IPsec-specific opaque crypto info */ | |
| 896d25fc | 478 | tc = kmalloc(sizeof(struct tdb_crypto), |
| 062eab50 | 479 | M_XDATA, M_INTWAIT | M_ZERO | M_NULLOK); |
| 984263bc MD |
480 | if (tc == NULL) { |
| 481 | ipcompstat.ipcomps_crypto++; | |
| 482 | DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n")); | |
| 483 | crypto_freereq(crp); | |
| 484 | error = ENOBUFS; | |
| 485 | goto bad; | |
| 486 | } | |
| 487 | ||
| 488 | tc->tc_isr = isr; | |
| 489 | tc->tc_spi = sav->spi; | |
| 490 | tc->tc_dst = sav->sah->saidx.dst; | |
| 491 | tc->tc_proto = sav->sah->saidx.proto; | |
| 492 | tc->tc_skip = skip + hlen; | |
| 493 | ||
| 494 | /* Crypto operation descriptor */ | |
| 495 | crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ | |
| 496 | crp->crp_flags = CRYPTO_F_IMBUF; | |
| 497 | crp->crp_buf = (caddr_t) m; | |
| 498 | crp->crp_callback = ipcomp_output_cb; | |
| 499 | crp->crp_opaque = (caddr_t) tc; | |
| 500 | crp->crp_sid = sav->tdb_cryptoid; | |
| 501 | ||
| 502 | return crypto_dispatch(crp); | |
| 503 | bad: | |
| 504 | if (m) | |
| 505 | m_freem(m); | |
| 506 | return (error); | |
| 507 | } | |
| 508 | ||
| 509 | /* | |
| 510 | * IPComp output callback from the crypto driver. | |
| 511 | */ | |
| 512 | static int | |
| 513 | ipcomp_output_cb(struct cryptop *crp) | |
| 514 | { | |
| 515 | struct tdb_crypto *tc; | |
| 516 | struct ipsecrequest *isr; | |
| 517 | struct secasvar *sav; | |
| 518 | struct mbuf *m; | |
| cb753a0e | 519 | int error, skip, rlen; |
| 984263bc MD |
520 | |
| 521 | tc = (struct tdb_crypto *) crp->crp_opaque; | |
| 522 | KASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!")); | |
| 523 | m = (struct mbuf *) crp->crp_buf; | |
| 524 | skip = tc->tc_skip; | |
| 525 | rlen = crp->crp_ilen - skip; | |
| 526 | ||
| cb753a0e | 527 | crit_enter(); |
| 984263bc MD |
528 | |
| 529 | isr = tc->tc_isr; | |
| 530 | sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi); | |
| 531 | if (sav == NULL) { | |
| 532 | ipcompstat.ipcomps_notdb++; | |
| 533 | DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n")); | |
| 534 | error = ENOBUFS; /*XXX*/ | |
| 535 | goto bad; | |
| 536 | } | |
| 537 | KASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n")); | |
| 538 | ||
| 539 | /* Check for crypto errors */ | |
| 540 | if (crp->crp_etype) { | |
| 541 | /* Reset session ID */ | |
| 542 | if (sav->tdb_cryptoid != 0) | |
| 543 | sav->tdb_cryptoid = crp->crp_sid; | |
| 544 | ||
| 545 | if (crp->crp_etype == EAGAIN) { | |
| 546 | KEY_FREESAV(&sav); | |
| cb753a0e | 547 | crit_exit(); |
| 984263bc MD |
548 | return crypto_dispatch(crp); |
| 549 | } | |
| 550 | ipcompstat.ipcomps_noxform++; | |
| 551 | DPRINTF(("ipcomp_output_cb: crypto error %d\n", crp->crp_etype)); | |
| 552 | error = crp->crp_etype; | |
| 553 | goto bad; | |
| 554 | } | |
| 555 | /* Shouldn't happen... */ | |
| 556 | if (m == NULL) { | |
| 557 | ipcompstat.ipcomps_crypto++; | |
| 558 | DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n")); | |
| 559 | error = EINVAL; | |
| 560 | goto bad; | |
| 561 | } | |
| 562 | ipcompstat.ipcomps_hist[sav->alg_comp]++; | |
| 563 | ||
| 564 | if (rlen > crp->crp_olen) { | |
| 565 | /* Adjust the length in the IP header */ | |
| 566 | switch (sav->sah->saidx.dst.sa.sa_family) { | |
| 567 | #ifdef INET | |
| 568 | case AF_INET: | |
| 569 | mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); | |
| 570 | break; | |
| 571 | #endif /* INET */ | |
| 572 | #ifdef INET6 | |
| 573 | case AF_INET6: | |
| 574 | mtod(m, struct ip6_hdr *)->ip6_plen = | |
| 575 | htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); | |
| 576 | break; | |
| 577 | #endif /* INET6 */ | |
| 578 | default: | |
| 579 | ipcompstat.ipcomps_nopf++; | |
| 580 | DPRINTF(("ipcomp_output: unknown/unsupported protocol " | |
| 581 | "family %d, IPCA %s/%08lx\n", | |
| 582 | sav->sah->saidx.dst.sa.sa_family, | |
| 583 | ipsec_address(&sav->sah->saidx.dst), | |
| 584 | (u_long) ntohl(sav->spi))); | |
| 585 | error = EPFNOSUPPORT; | |
| 586 | goto bad; | |
| 587 | } | |
| 588 | } else { | |
| 589 | /* compression was useless, we have lost time */ | |
| 590 | /* XXX add statistic */ | |
| 591 | } | |
| 592 | ||
| 593 | /* Release the crypto descriptor */ | |
| efda3bd0 | 594 | kfree(tc, M_XDATA); |
| 984263bc MD |
595 | crypto_freereq(crp); |
| 596 | ||
| 597 | /* NB: m is reclaimed by ipsec_process_done. */ | |
| 598 | error = ipsec_process_done(m, isr); | |
| 599 | KEY_FREESAV(&sav); | |
| cb753a0e | 600 | crit_exit(); |
| 984263bc MD |
601 | return error; |
| 602 | bad: | |
| 603 | if (sav) | |
| 604 | KEY_FREESAV(&sav); | |
| cb753a0e | 605 | crit_exit(); |
| 984263bc MD |
606 | if (m) |
| 607 | m_freem(m); | |
| efda3bd0 | 608 | kfree(tc, M_XDATA); |
| 984263bc MD |
609 | crypto_freereq(crp); |
| 610 | return error; | |
| 611 | } | |
| 612 | ||
| 613 | static struct xformsw ipcomp_xformsw = { | |
| 614 | XF_IPCOMP, XFT_COMP, "IPcomp", | |
| 615 | ipcomp_init, ipcomp_zeroize, ipcomp_input, | |
| 616 | ipcomp_output | |
| 617 | }; | |
| 618 | ||
| 619 | static void | |
| 620 | ipcomp_attach(void) | |
| 621 | { | |
| 622 | xform_register(&ipcomp_xformsw); | |
| 623 | } | |
| 624 | SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL) |