| 1 | /* $NetBSD: xform_ipcomp.c,v 1.31 2013/11/03 18:37:10 mrg 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 $ */ |
| 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 | #include <sys/cdefs.h> |
| 33 | __KERNEL_RCSID(0, "$NetBSD: xform_ipcomp.c,v 1.31 2013/11/03 18:37:10 mrg Exp $" ); |
| 34 | |
| 35 | /* IP payload compression protocol (IPComp), see RFC 2393 */ |
| 36 | #include "opt_inet.h" |
| 37 | #ifdef __FreeBSD__ |
| 38 | #include "opt_inet6.h" |
| 39 | #endif |
| 40 | |
| 41 | #include <sys/param.h> |
| 42 | #include <sys/systm.h> |
| 43 | #include <sys/mbuf.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <sys/kernel.h> |
| 46 | #include <sys/protosw.h> |
| 47 | #include <sys/sysctl.h> |
| 48 | #include <sys/socketvar.h> /* for softnet_lock */ |
| 49 | |
| 50 | #include <netinet/in.h> |
| 51 | #include <netinet/in_systm.h> |
| 52 | #include <netinet/ip.h> |
| 53 | #include <netinet/ip_var.h> |
| 54 | |
| 55 | #include <net/route.h> |
| 56 | #include <netipsec/ipsec.h> |
| 57 | #include <netipsec/ipsec_private.h> |
| 58 | #include <netipsec/xform.h> |
| 59 | |
| 60 | #ifdef INET6 |
| 61 | #include <netinet/ip6.h> |
| 62 | #include <netipsec/ipsec6.h> |
| 63 | #endif |
| 64 | |
| 65 | #include <netipsec/ipcomp.h> |
| 66 | #include <netipsec/ipcomp_var.h> |
| 67 | |
| 68 | #include <netipsec/key.h> |
| 69 | #include <netipsec/key_debug.h> |
| 70 | |
| 71 | #include <netipsec/ipsec_osdep.h> |
| 72 | |
| 73 | #include <opencrypto/cryptodev.h> |
| 74 | #include <opencrypto/deflate.h> |
| 75 | #include <opencrypto/xform.h> |
| 76 | |
| 77 | percpu_t *ipcompstat_percpu; |
| 78 | |
| 79 | int ipcomp_enable = 1; |
| 80 | |
| 81 | #ifdef __FreeBSD__ |
| 82 | SYSCTL_DECL(_net_inet_ipcomp); |
| 83 | SYSCTL_INT(_net_inet_ipcomp, OID_AUTO, |
| 84 | ipcomp_enable, CTLFLAG_RW, &ipcomp_enable, 0, "" ); |
| 85 | SYSCTL_STRUCT(_net_inet_ipcomp, IPSECCTL_STATS, |
| 86 | stats, CTLFLAG_RD, &ipcompstat, ipcompstat, "" ); |
| 87 | #endif /* __FreeBSD__ */ |
| 88 | |
| 89 | static int ipcomp_input_cb(struct cryptop *crp); |
| 90 | static int ipcomp_output_cb(struct cryptop *crp); |
| 91 | |
| 92 | const struct comp_algo * |
| 93 | ipcomp_algorithm_lookup(int alg) |
| 94 | { |
| 95 | if (alg >= IPCOMP_ALG_MAX) |
| 96 | return NULL; |
| 97 | switch (alg) { |
| 98 | case SADB_X_CALG_DEFLATE: |
| 99 | return &comp_algo_deflate_nogrow; |
| 100 | } |
| 101 | return NULL; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * ipcomp_init() is called when an CPI is being set up. |
| 106 | */ |
| 107 | static int |
| 108 | ipcomp_init(struct secasvar *sav, const struct xformsw *xsp) |
| 109 | { |
| 110 | const struct comp_algo *tcomp; |
| 111 | struct cryptoini cric; |
| 112 | int ses; |
| 113 | |
| 114 | /* NB: algorithm really comes in alg_enc and not alg_comp! */ |
| 115 | tcomp = ipcomp_algorithm_lookup(sav->alg_enc); |
| 116 | if (tcomp == NULL) { |
| 117 | DPRINTF(("ipcomp_init: unsupported compression algorithm %d\n" , |
| 118 | sav->alg_comp)); |
| 119 | return EINVAL; |
| 120 | } |
| 121 | sav->alg_comp = sav->alg_enc; /* set for doing histogram */ |
| 122 | sav->tdb_xform = xsp; |
| 123 | sav->tdb_compalgxform = tcomp; |
| 124 | |
| 125 | /* Initialize crypto session */ |
| 126 | memset(&cric, 0, sizeof (cric)); |
| 127 | cric.cri_alg = sav->tdb_compalgxform->type; |
| 128 | |
| 129 | ses = crypto_newsession(&sav->tdb_cryptoid, &cric, crypto_support); |
| 130 | return ses; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * ipcomp_zeroize() used when IPCA is deleted |
| 135 | */ |
| 136 | static int |
| 137 | ipcomp_zeroize(struct secasvar *sav) |
| 138 | { |
| 139 | int err; |
| 140 | |
| 141 | err = crypto_freesession(sav->tdb_cryptoid); |
| 142 | sav->tdb_cryptoid = 0; |
| 143 | return err; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * ipcomp_input() gets called to uncompress an input packet |
| 148 | */ |
| 149 | static int |
| 150 | ipcomp_input(struct mbuf *m, const struct secasvar *sav, int skip, int protoff) |
| 151 | { |
| 152 | struct tdb_crypto *tc; |
| 153 | struct cryptodesc *crdc; |
| 154 | struct cryptop *crp; |
| 155 | int error, hlen = IPCOMP_HLENGTH; |
| 156 | |
| 157 | IPSEC_SPLASSERT_SOFTNET("ipcomp_input" ); |
| 158 | |
| 159 | /* Get crypto descriptors */ |
| 160 | crp = crypto_getreq(1); |
| 161 | if (crp == NULL) { |
| 162 | m_freem(m); |
| 163 | DPRINTF(("ipcomp_input: no crypto descriptors\n" )); |
| 164 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 165 | return ENOBUFS; |
| 166 | } |
| 167 | /* Get IPsec-specific opaque pointer */ |
| 168 | tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO); |
| 169 | if (tc == NULL) { |
| 170 | m_freem(m); |
| 171 | crypto_freereq(crp); |
| 172 | DPRINTF(("ipcomp_input: cannot allocate tdb_crypto\n" )); |
| 173 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 174 | return ENOBUFS; |
| 175 | } |
| 176 | |
| 177 | error = m_makewritable(&m, 0, m->m_pkthdr.len, M_NOWAIT); |
| 178 | if (error) { |
| 179 | DPRINTF(("ipcomp_input: m_makewritable failed\n" )); |
| 180 | m_freem(m); |
| 181 | free(tc, M_XDATA); |
| 182 | crypto_freereq(crp); |
| 183 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 184 | return error; |
| 185 | } |
| 186 | |
| 187 | crdc = crp->crp_desc; |
| 188 | |
| 189 | crdc->crd_skip = skip + hlen; |
| 190 | crdc->crd_len = m->m_pkthdr.len - (skip + hlen); |
| 191 | crdc->crd_inject = 0; /* unused */ |
| 192 | |
| 193 | tc->tc_ptr = 0; |
| 194 | |
| 195 | /* Decompression operation */ |
| 196 | crdc->crd_alg = sav->tdb_compalgxform->type; |
| 197 | |
| 198 | /* Crypto operation descriptor */ |
| 199 | crp->crp_ilen = m->m_pkthdr.len - (skip + hlen); |
| 200 | crp->crp_olen = MCLBYTES; /* hint to decompression code */ |
| 201 | crp->crp_flags = CRYPTO_F_IMBUF; |
| 202 | crp->crp_buf = m; |
| 203 | crp->crp_callback = ipcomp_input_cb; |
| 204 | crp->crp_sid = sav->tdb_cryptoid; |
| 205 | crp->crp_opaque = tc; |
| 206 | |
| 207 | /* These are passed as-is to the callback */ |
| 208 | tc->tc_spi = sav->spi; |
| 209 | tc->tc_dst = sav->sah->saidx.dst; |
| 210 | tc->tc_proto = sav->sah->saidx.proto; |
| 211 | tc->tc_protoff = protoff; |
| 212 | tc->tc_skip = skip; |
| 213 | |
| 214 | return crypto_dispatch(crp); |
| 215 | } |
| 216 | |
| 217 | #ifdef INET6 |
| 218 | #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \ |
| 219 | if (saidx->dst.sa.sa_family == AF_INET6) { \ |
| 220 | error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \ |
| 221 | } else { \ |
| 222 | error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \ |
| 223 | } \ |
| 224 | } while (0) |
| 225 | #else |
| 226 | #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \ |
| 227 | (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag)) |
| 228 | #endif |
| 229 | |
| 230 | /* |
| 231 | * IPComp input callback from the crypto driver. |
| 232 | */ |
| 233 | static int |
| 234 | ipcomp_input_cb(struct cryptop *crp) |
| 235 | { |
| 236 | struct tdb_crypto *tc; |
| 237 | int skip, protoff; |
| 238 | struct mbuf *m; |
| 239 | struct secasvar *sav; |
| 240 | struct secasindex *saidx __diagused; |
| 241 | int s, hlen = IPCOMP_HLENGTH, error, clen; |
| 242 | u_int8_t nproto; |
| 243 | void *addr; |
| 244 | u_int16_t dport; |
| 245 | u_int16_t sport; |
| 246 | |
| 247 | tc = (struct tdb_crypto *) crp->crp_opaque; |
| 248 | IPSEC_ASSERT(tc != NULL, ("ipcomp_input_cb: null opaque crypto data area!" )); |
| 249 | skip = tc->tc_skip; |
| 250 | protoff = tc->tc_protoff; |
| 251 | m = (struct mbuf *) crp->crp_buf; |
| 252 | |
| 253 | /* find the source port for NAT-T */ |
| 254 | nat_t_ports_get(m, &dport, &sport); |
| 255 | |
| 256 | s = splsoftnet(); |
| 257 | mutex_enter(softnet_lock); |
| 258 | |
| 259 | sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, sport, dport); |
| 260 | if (sav == NULL) { |
| 261 | IPCOMP_STATINC(IPCOMP_STAT_NOTDB); |
| 262 | DPRINTF(("ipcomp_input_cb: SA expired while in crypto\n" )); |
| 263 | error = ENOBUFS; /*XXX*/ |
| 264 | goto bad; |
| 265 | } |
| 266 | |
| 267 | saidx = &sav->sah->saidx; |
| 268 | IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET || |
| 269 | saidx->dst.sa.sa_family == AF_INET6, |
| 270 | ("ipcomp_input_cb: unexpected protocol family %u" , |
| 271 | saidx->dst.sa.sa_family)); |
| 272 | |
| 273 | /* Check for crypto errors */ |
| 274 | if (crp->crp_etype) { |
| 275 | /* Reset the session ID */ |
| 276 | if (sav->tdb_cryptoid != 0) |
| 277 | sav->tdb_cryptoid = crp->crp_sid; |
| 278 | |
| 279 | if (crp->crp_etype == EAGAIN) { |
| 280 | KEY_FREESAV(&sav); |
| 281 | mutex_exit(softnet_lock); |
| 282 | splx(s); |
| 283 | return crypto_dispatch(crp); |
| 284 | } |
| 285 | |
| 286 | IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); |
| 287 | DPRINTF(("ipcomp_input_cb: crypto error %d\n" , crp->crp_etype)); |
| 288 | error = crp->crp_etype; |
| 289 | goto bad; |
| 290 | } |
| 291 | /* Shouldn't happen... */ |
| 292 | if (m == NULL) { |
| 293 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 294 | DPRINTF(("ipcomp_input_cb: null mbuf returned from crypto\n" )); |
| 295 | error = EINVAL; |
| 296 | goto bad; |
| 297 | } |
| 298 | IPCOMP_STATINC(IPCOMP_STAT_HIST + sav->alg_comp); |
| 299 | |
| 300 | /* Update the counters */ |
| 301 | IPCOMP_STATADD(IPCOMP_STAT_IBYTES, m->m_pkthdr.len - skip - hlen); |
| 302 | |
| 303 | |
| 304 | clen = crp->crp_olen; /* Length of data after processing */ |
| 305 | |
| 306 | /* Release the crypto descriptors */ |
| 307 | free(tc, M_XDATA), tc = NULL; |
| 308 | crypto_freereq(crp), crp = NULL; |
| 309 | |
| 310 | /* In case it's not done already, adjust the size of the mbuf chain */ |
| 311 | m->m_pkthdr.len = clen + hlen + skip; |
| 312 | |
| 313 | if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) { |
| 314 | IPCOMP_STATINC(IPCOMP_STAT_HDROPS); /*XXX*/ |
| 315 | DPRINTF(("ipcomp_input_cb: m_pullup failed\n" )); |
| 316 | error = EINVAL; /*XXX*/ |
| 317 | goto bad; |
| 318 | } |
| 319 | |
| 320 | /* Keep the next protocol field */ |
| 321 | addr = (uint8_t*) mtod(m, struct ip *) + skip; |
| 322 | nproto = ((struct ipcomp *) addr)->comp_nxt; |
| 323 | if (nproto == IPPROTO_IPCOMP || nproto == IPPROTO_AH || nproto == IPPROTO_ESP) { |
| 324 | IPCOMP_STATINC(IPCOMP_STAT_HDROPS); |
| 325 | DPRINTF(("ipcomp_input_cb: nested ipcomp, IPCA %s/%08lx\n" , |
| 326 | ipsec_address(&sav->sah->saidx.dst), |
| 327 | (u_long) ntohl(sav->spi))); |
| 328 | error = EINVAL; |
| 329 | goto bad; |
| 330 | } |
| 331 | |
| 332 | /* Remove the IPCOMP header */ |
| 333 | error = m_striphdr(m, skip, hlen); |
| 334 | if (error) { |
| 335 | IPCOMP_STATINC(IPCOMP_STAT_HDROPS); |
| 336 | DPRINTF(("ipcomp_input_cb: bad mbuf chain, IPCA %s/%08lx\n" , |
| 337 | ipsec_address(&sav->sah->saidx.dst), |
| 338 | (u_long) ntohl(sav->spi))); |
| 339 | goto bad; |
| 340 | } |
| 341 | |
| 342 | /* Restore the Next Protocol field */ |
| 343 | m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto); |
| 344 | |
| 345 | IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL); |
| 346 | |
| 347 | KEY_FREESAV(&sav); |
| 348 | mutex_exit(softnet_lock); |
| 349 | splx(s); |
| 350 | return error; |
| 351 | bad: |
| 352 | if (sav) |
| 353 | KEY_FREESAV(&sav); |
| 354 | mutex_exit(softnet_lock); |
| 355 | splx(s); |
| 356 | if (m) |
| 357 | m_freem(m); |
| 358 | if (tc != NULL) |
| 359 | free(tc, M_XDATA); |
| 360 | if (crp) |
| 361 | crypto_freereq(crp); |
| 362 | return error; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * IPComp output routine, called by ipsec[46]_process_packet() |
| 367 | */ |
| 368 | static int |
| 369 | ipcomp_output( |
| 370 | struct mbuf *m, |
| 371 | struct ipsecrequest *isr, |
| 372 | struct mbuf **mp, |
| 373 | int skip, |
| 374 | int protoff |
| 375 | ) |
| 376 | { |
| 377 | const struct secasvar *sav; |
| 378 | const struct comp_algo *ipcompx; |
| 379 | int error, ralen, hlen, maxpacketsize; |
| 380 | struct cryptodesc *crdc; |
| 381 | struct cryptop *crp; |
| 382 | struct tdb_crypto *tc; |
| 383 | |
| 384 | IPSEC_SPLASSERT_SOFTNET("ipcomp_output" ); |
| 385 | sav = isr->sav; |
| 386 | IPSEC_ASSERT(sav != NULL, ("ipcomp_output: null SA" )); |
| 387 | ipcompx = sav->tdb_compalgxform; |
| 388 | IPSEC_ASSERT(ipcompx != NULL, ("ipcomp_output: null compression xform" )); |
| 389 | |
| 390 | ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */ |
| 391 | |
| 392 | /* Don't process the packet if it is too short */ |
| 393 | if (ralen < ipcompx->minlen) { |
| 394 | IPCOMP_STATINC(IPCOMP_STAT_MINLEN); |
| 395 | return ipsec_process_done(m,isr); |
| 396 | } |
| 397 | |
| 398 | hlen = IPCOMP_HLENGTH; |
| 399 | |
| 400 | IPCOMP_STATINC(IPCOMP_STAT_OUTPUT); |
| 401 | |
| 402 | /* Check for maximum packet size violations. */ |
| 403 | switch (sav->sah->saidx.dst.sa.sa_family) { |
| 404 | #ifdef INET |
| 405 | case AF_INET: |
| 406 | maxpacketsize = IP_MAXPACKET; |
| 407 | break; |
| 408 | #endif /* INET */ |
| 409 | #ifdef INET6 |
| 410 | case AF_INET6: |
| 411 | maxpacketsize = IPV6_MAXPACKET; |
| 412 | break; |
| 413 | #endif /* INET6 */ |
| 414 | default: |
| 415 | IPCOMP_STATINC(IPCOMP_STAT_NOPF); |
| 416 | DPRINTF(("ipcomp_output: unknown/unsupported protocol family %d" |
| 417 | ", IPCA %s/%08lx\n" , |
| 418 | sav->sah->saidx.dst.sa.sa_family, |
| 419 | ipsec_address(&sav->sah->saidx.dst), |
| 420 | (u_long) ntohl(sav->spi))); |
| 421 | error = EPFNOSUPPORT; |
| 422 | goto bad; |
| 423 | } |
| 424 | if (skip + hlen + ralen > maxpacketsize) { |
| 425 | IPCOMP_STATINC(IPCOMP_STAT_TOOBIG); |
| 426 | DPRINTF(("ipcomp_output: packet in IPCA %s/%08lx got too big " |
| 427 | "(len %u, max len %u)\n" , |
| 428 | ipsec_address(&sav->sah->saidx.dst), |
| 429 | (u_long) ntohl(sav->spi), |
| 430 | skip + hlen + ralen, maxpacketsize)); |
| 431 | error = EMSGSIZE; |
| 432 | goto bad; |
| 433 | } |
| 434 | |
| 435 | /* Update the counters */ |
| 436 | IPCOMP_STATADD(IPCOMP_STAT_OBYTES, m->m_pkthdr.len - skip); |
| 437 | |
| 438 | m = m_clone(m); |
| 439 | if (m == NULL) { |
| 440 | IPCOMP_STATINC(IPCOMP_STAT_HDROPS); |
| 441 | DPRINTF(("ipcomp_output: cannot clone mbuf chain, IPCA %s/%08lx\n" , |
| 442 | ipsec_address(&sav->sah->saidx.dst), |
| 443 | (u_long) ntohl(sav->spi))); |
| 444 | error = ENOBUFS; |
| 445 | goto bad; |
| 446 | } |
| 447 | |
| 448 | /* Ok now, we can pass to the crypto processing */ |
| 449 | |
| 450 | /* Get crypto descriptors */ |
| 451 | crp = crypto_getreq(1); |
| 452 | if (crp == NULL) { |
| 453 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 454 | DPRINTF(("ipcomp_output: failed to acquire crypto descriptor\n" )); |
| 455 | error = ENOBUFS; |
| 456 | goto bad; |
| 457 | } |
| 458 | crdc = crp->crp_desc; |
| 459 | |
| 460 | /* Compression descriptor */ |
| 461 | crdc->crd_skip = skip; |
| 462 | crdc->crd_len = m->m_pkthdr.len - skip; |
| 463 | crdc->crd_flags = CRD_F_COMP; |
| 464 | crdc->crd_inject = skip; |
| 465 | |
| 466 | /* Compression operation */ |
| 467 | crdc->crd_alg = ipcompx->type; |
| 468 | |
| 469 | /* IPsec-specific opaque crypto info */ |
| 470 | tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto), |
| 471 | M_XDATA, M_NOWAIT|M_ZERO); |
| 472 | if (tc == NULL) { |
| 473 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 474 | DPRINTF(("ipcomp_output: failed to allocate tdb_crypto\n" )); |
| 475 | crypto_freereq(crp); |
| 476 | error = ENOBUFS; |
| 477 | goto bad; |
| 478 | } |
| 479 | |
| 480 | tc->tc_isr = isr; |
| 481 | tc->tc_spi = sav->spi; |
| 482 | tc->tc_dst = sav->sah->saidx.dst; |
| 483 | tc->tc_proto = sav->sah->saidx.proto; |
| 484 | tc->tc_skip = skip; |
| 485 | tc->tc_protoff = protoff; |
| 486 | |
| 487 | /* Crypto operation descriptor */ |
| 488 | crp->crp_ilen = m->m_pkthdr.len; /* Total input length */ |
| 489 | crp->crp_flags = CRYPTO_F_IMBUF; |
| 490 | crp->crp_buf = m; |
| 491 | crp->crp_callback = ipcomp_output_cb; |
| 492 | crp->crp_opaque = tc; |
| 493 | crp->crp_sid = sav->tdb_cryptoid; |
| 494 | |
| 495 | return crypto_dispatch(crp); |
| 496 | bad: |
| 497 | if (m) |
| 498 | m_freem(m); |
| 499 | return (error); |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * IPComp output callback from the crypto driver. |
| 504 | */ |
| 505 | static int |
| 506 | ipcomp_output_cb(struct cryptop *crp) |
| 507 | { |
| 508 | struct tdb_crypto *tc; |
| 509 | struct ipsecrequest *isr; |
| 510 | struct secasvar *sav; |
| 511 | struct mbuf *m, *mo; |
| 512 | int s, error, skip, rlen, roff; |
| 513 | u_int8_t prot; |
| 514 | u_int16_t cpi; |
| 515 | struct ipcomp * ipcomp; |
| 516 | |
| 517 | |
| 518 | tc = (struct tdb_crypto *) crp->crp_opaque; |
| 519 | IPSEC_ASSERT(tc != NULL, ("ipcomp_output_cb: null opaque data area!" )); |
| 520 | m = (struct mbuf *) crp->crp_buf; |
| 521 | skip = tc->tc_skip; |
| 522 | rlen = crp->crp_ilen - skip; |
| 523 | |
| 524 | s = splsoftnet(); |
| 525 | mutex_enter(softnet_lock); |
| 526 | |
| 527 | isr = tc->tc_isr; |
| 528 | sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi, 0, 0); |
| 529 | if (sav == NULL) { |
| 530 | IPCOMP_STATINC(IPCOMP_STAT_NOTDB); |
| 531 | DPRINTF(("ipcomp_output_cb: SA expired while in crypto\n" )); |
| 532 | error = ENOBUFS; /*XXX*/ |
| 533 | goto bad; |
| 534 | } |
| 535 | IPSEC_ASSERT(isr->sav == sav, ("ipcomp_output_cb: SA changed\n" )); |
| 536 | |
| 537 | /* Check for crypto errors */ |
| 538 | if (crp->crp_etype) { |
| 539 | /* Reset session ID */ |
| 540 | if (sav->tdb_cryptoid != 0) |
| 541 | sav->tdb_cryptoid = crp->crp_sid; |
| 542 | |
| 543 | if (crp->crp_etype == EAGAIN) { |
| 544 | KEY_FREESAV(&sav); |
| 545 | mutex_exit(softnet_lock); |
| 546 | splx(s); |
| 547 | return crypto_dispatch(crp); |
| 548 | } |
| 549 | IPCOMP_STATINC(IPCOMP_STAT_NOXFORM); |
| 550 | DPRINTF(("ipcomp_output_cb: crypto error %d\n" , crp->crp_etype)); |
| 551 | error = crp->crp_etype; |
| 552 | goto bad; |
| 553 | } |
| 554 | /* Shouldn't happen... */ |
| 555 | if (m == NULL) { |
| 556 | IPCOMP_STATINC(IPCOMP_STAT_CRYPTO); |
| 557 | DPRINTF(("ipcomp_output_cb: bogus return buffer from crypto\n" )); |
| 558 | error = EINVAL; |
| 559 | goto bad; |
| 560 | } |
| 561 | IPCOMP_STATINC(IPCOMP_STAT_HIST + sav->alg_comp); |
| 562 | |
| 563 | if (rlen > crp->crp_olen) { |
| 564 | /* Inject IPCOMP header */ |
| 565 | mo = m_makespace(m, skip, IPCOMP_HLENGTH, &roff); |
| 566 | if (mo == NULL) { |
| 567 | IPCOMP_STATINC(IPCOMP_STAT_WRAP); |
| 568 | DPRINTF(("ipcomp_output: failed to inject IPCOMP header for " |
| 569 | "IPCA %s/%08lx\n" , |
| 570 | ipsec_address(&sav->sah->saidx.dst), |
| 571 | (u_long) ntohl(sav->spi))); |
| 572 | error = ENOBUFS; |
| 573 | goto bad; |
| 574 | } |
| 575 | ipcomp = (struct ipcomp *)(mtod(mo, char *) + roff); |
| 576 | |
| 577 | /* Initialize the IPCOMP header */ |
| 578 | /* XXX alignment always correct? */ |
| 579 | switch (sav->sah->saidx.dst.sa.sa_family) { |
| 580 | #ifdef INET |
| 581 | case AF_INET: |
| 582 | ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p; |
| 583 | break; |
| 584 | #endif /* INET */ |
| 585 | #ifdef INET6 |
| 586 | case AF_INET6: |
| 587 | ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt; |
| 588 | break; |
| 589 | #endif |
| 590 | } |
| 591 | ipcomp->comp_flags = 0; |
| 592 | |
| 593 | if ((sav->flags & SADB_X_EXT_RAWCPI) == 0) |
| 594 | cpi = sav->alg_enc; |
| 595 | else |
| 596 | cpi = ntohl(sav->spi) & 0xffff; |
| 597 | ipcomp->comp_cpi = htons(cpi); |
| 598 | |
| 599 | /* Fix Next Protocol in IPv4/IPv6 header */ |
| 600 | prot = IPPROTO_IPCOMP; |
| 601 | m_copyback(m, tc->tc_protoff, sizeof(u_int8_t), (u_char *)&prot); |
| 602 | |
| 603 | /* Adjust the length in the IP header */ |
| 604 | switch (sav->sah->saidx.dst.sa.sa_family) { |
| 605 | #ifdef INET |
| 606 | case AF_INET: |
| 607 | mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len); |
| 608 | break; |
| 609 | #endif /* INET */ |
| 610 | #ifdef INET6 |
| 611 | case AF_INET6: |
| 612 | mtod(m, struct ip6_hdr *)->ip6_plen = |
| 613 | htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr); |
| 614 | break; |
| 615 | #endif /* INET6 */ |
| 616 | default: |
| 617 | IPCOMP_STATINC(IPCOMP_STAT_NOPF); |
| 618 | DPRINTF(("ipcomp_output: unknown/unsupported protocol " |
| 619 | "family %d, IPCA %s/%08lx\n" , |
| 620 | sav->sah->saidx.dst.sa.sa_family, |
| 621 | ipsec_address(&sav->sah->saidx.dst), |
| 622 | (u_long) ntohl(sav->spi))); |
| 623 | error = EPFNOSUPPORT; |
| 624 | goto bad; |
| 625 | } |
| 626 | } else { |
| 627 | /* compression was useless, we have lost time */ |
| 628 | IPCOMP_STATINC(IPCOMP_STAT_USELESS); |
| 629 | DPRINTF(("ipcomp_output_cb: compression was useless : initial size was %d" |
| 630 | "and compressed size is %d\n" , rlen, crp->crp_olen)); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | /* Release the crypto descriptor */ |
| 635 | free(tc, M_XDATA); |
| 636 | crypto_freereq(crp); |
| 637 | |
| 638 | /* NB: m is reclaimed by ipsec_process_done. */ |
| 639 | error = ipsec_process_done(m, isr); |
| 640 | KEY_FREESAV(&sav); |
| 641 | mutex_exit(softnet_lock); |
| 642 | splx(s); |
| 643 | return error; |
| 644 | bad: |
| 645 | if (sav) |
| 646 | KEY_FREESAV(&sav); |
| 647 | mutex_exit(softnet_lock); |
| 648 | splx(s); |
| 649 | if (m) |
| 650 | m_freem(m); |
| 651 | free(tc, M_XDATA); |
| 652 | crypto_freereq(crp); |
| 653 | return error; |
| 654 | } |
| 655 | |
| 656 | static struct xformsw ipcomp_xformsw = { |
| 657 | XF_IPCOMP, XFT_COMP, "IPcomp" , |
| 658 | ipcomp_init, ipcomp_zeroize, ipcomp_input, |
| 659 | ipcomp_output, |
| 660 | NULL, |
| 661 | }; |
| 662 | |
| 663 | INITFN void |
| 664 | ipcomp_attach(void) |
| 665 | { |
| 666 | ipcompstat_percpu = percpu_alloc(sizeof(uint64_t) * IPCOMP_NSTATS); |
| 667 | xform_register(&ipcomp_xformsw); |
| 668 | } |
| 669 | |
| 670 | #ifdef __FreeBSD__ |
| 671 | SYSINIT(ipcomp_xform_init, SI_SUB_DRIVERS, SI_ORDER_FIRST, ipcomp_attach, NULL) |
| 672 | #endif /* __FreeBSD__ */ |
| 673 | |