1 /* $FreeBSD: releng/6.4/sys/netipsec/xform_ipcomp.c 161999 2006-09-04 15:17:50Z pjd $ */
2 /* $OpenBSD: ip_ipcomp.c,v 1.1 2001/07/05 12:08:52 jjbg Exp $ */
3
4 /*-
5 * Copyright (c) 2001 Jean-Jacques Bernard-Gundol (jj@wabbitt.org)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* IP payload compression protocol (IPComp), see RFC 2393 */
32 #include "opt_inet.h"
33 #include "opt_inet6.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/mbuf.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/socket.h>
41 #include <sys/kernel.h>
42 #include <sys/protosw.h>
43 #include <sys/sysctl.h>
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>
51 #include <netipsec/ipsec.h>
52 #include <netipsec/xform.h>
53
54 #ifdef INET6
55 #include <netinet/ip6.h>
56 #include <netipsec/ipsec6.h>
57 #endif
58
59 #include <netipsec/ipcomp.h>
60 #include <netipsec/ipcomp_var.h>
61
62 #include <netipsec/key.h>
63 #include <netipsec/key_debug.h>
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(("%s: unsupported compression algorithm %d\n", __func__,
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;
142 int hlen = IPCOMP_HLENGTH;
143
144 IPSEC_SPLASSERT_SOFTNET(__func__);
145
146 /* Get crypto descriptors */
147 crp = crypto_getreq(1);
148 if (crp == NULL) {
149 m_freem(m);
150 DPRINTF(("%s: no crypto descriptors\n", __func__));
151 ipcompstat.ipcomps_crypto++;
152 return ENOBUFS;
153 }
154 /* Get IPsec-specific opaque pointer */
155 tc = (struct tdb_crypto *) malloc(sizeof (*tc), M_XDATA, M_NOWAIT|M_ZERO);
156 if (tc == NULL) {
157 m_freem(m);
158 crypto_freereq(crp);
159 DPRINTF(("%s: cannot allocate tdb_crypto\n", __func__));
160 ipcompstat.ipcomps_crypto++;
161 return ENOBUFS;
162 }
163 crdc = crp->crp_desc;
164
165 crdc->crd_skip = skip + hlen;
166 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
167 crdc->crd_inject = skip;
168
169 tc->tc_ptr = 0;
170
171 /* Decompression operation */
172 crdc->crd_alg = sav->tdb_compalgxform->type;
173
174 /* Crypto operation descriptor */
175 crp->crp_ilen = m->m_pkthdr.len - (skip + hlen);
176 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
177 crp->crp_buf = (caddr_t) m;
178 crp->crp_callback = ipcomp_input_cb;
179 crp->crp_sid = sav->tdb_cryptoid;
180 crp->crp_opaque = (caddr_t) tc;
181
182 /* These are passed as-is to the callback */
183 tc->tc_spi = sav->spi;
184 tc->tc_dst = sav->sah->saidx.dst;
185 tc->tc_proto = sav->sah->saidx.proto;
186 tc->tc_protoff = protoff;
187 tc->tc_skip = skip;
188
189 return crypto_dispatch(crp);
190 }
191
192 #ifdef INET6
193 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) do { \
194 if (saidx->dst.sa.sa_family == AF_INET6) { \
195 error = ipsec6_common_input_cb(m, sav, skip, protoff, mtag); \
196 } else { \
197 error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag); \
198 } \
199 } while (0)
200 #else
201 #define IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, mtag) \
202 (error = ipsec4_common_input_cb(m, sav, skip, protoff, mtag))
203 #endif
204
205 /*
206 * IPComp input callback from the crypto driver.
207 */
208 static int
209 ipcomp_input_cb(struct cryptop *crp)
210 {
211 struct cryptodesc *crd;
212 struct tdb_crypto *tc;
213 int skip, protoff;
214 struct mtag *mtag;
215 struct mbuf *m;
216 struct secasvar *sav;
217 struct secasindex *saidx;
218 int hlen = IPCOMP_HLENGTH, error, clen;
219 u_int8_t nproto;
220 caddr_t addr;
221
222 NET_LOCK_GIANT();
223
224 crd = crp->crp_desc;
225
226 tc = (struct tdb_crypto *) crp->crp_opaque;
227 IPSEC_ASSERT(tc != NULL, ("null opaque crypto data area!"));
228 skip = tc->tc_skip;
229 protoff = tc->tc_protoff;
230 mtag = (struct mtag *) tc->tc_ptr;
231 m = (struct mbuf *) crp->crp_buf;
232
233 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
234 if (sav == NULL) {
235 ipcompstat.ipcomps_notdb++;
236 DPRINTF(("%s: SA expired while in crypto\n", __func__));
237 error = ENOBUFS; /*XXX*/
238 goto bad;
239 }
240
241 saidx = &sav->sah->saidx;
242 IPSEC_ASSERT(saidx->dst.sa.sa_family == AF_INET ||
243 saidx->dst.sa.sa_family == AF_INET6,
244 ("unexpected protocol family %u", saidx->dst.sa.sa_family));
245
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;
251
252 if (crp->crp_etype == EAGAIN) {
253 KEY_FREESAV(&sav);
254 error = crypto_dispatch(crp);
255 NET_UNLOCK_GIANT();
256 return error;
257 }
258
259 ipcompstat.ipcomps_noxform++;
260 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
261 error = crp->crp_etype;
262 goto bad;
263 }
264 /* Shouldn't happen... */
265 if (m == NULL) {
266 ipcompstat.ipcomps_crypto++;
267 DPRINTF(("%s: null mbuf returned from crypto\n", __func__));
268 error = EINVAL;
269 goto bad;
270 }
271 ipcompstat.ipcomps_hist[sav->alg_comp]++;
272
273 clen = crp->crp_olen; /* Length of data after processing */
274
275 /* Release the crypto descriptors */
276 free(tc, M_XDATA), tc = NULL;
277 crypto_freereq(crp), crp = NULL;
278
279 /* In case it's not done already, adjust the size of the mbuf chain */
280 m->m_pkthdr.len = clen + hlen + skip;
281
282 if (m->m_len < skip + hlen && (m = m_pullup(m, skip + hlen)) == 0) {
283 ipcompstat.ipcomps_hdrops++; /*XXX*/
284 DPRINTF(("%s: m_pullup failed\n", __func__));
285 error = EINVAL; /*XXX*/
286 goto bad;
287 }
288
289 /* Keep the next protocol field */
290 addr = (caddr_t) mtod(m, struct ip *) + skip;
291 nproto = ((struct ipcomp *) addr)->comp_nxt;
292
293 /* Remove the IPCOMP header */
294 error = m_striphdr(m, skip, hlen);
295 if (error) {
296 ipcompstat.ipcomps_hdrops++;
297 DPRINTF(("%s: bad mbuf chain, IPCA %s/%08lx\n", __func__,
298 ipsec_address(&sav->sah->saidx.dst),
299 (u_long) ntohl(sav->spi)));
300 goto bad;
301 }
302
303 /* Restore the Next Protocol field */
304 m_copyback(m, protoff, sizeof (u_int8_t), (u_int8_t *) &nproto);
305
306 IPSEC_COMMON_INPUT_CB(m, sav, skip, protoff, NULL);
307
308 KEY_FREESAV(&sav);
309 NET_UNLOCK_GIANT();
310 return error;
311 bad:
312 if (sav)
313 KEY_FREESAV(&sav);
314 if (m)
315 m_freem(m);
316 if (tc != NULL)
317 free(tc, M_XDATA);
318 if (crp)
319 crypto_freereq(crp);
320 NET_UNLOCK_GIANT();
321 return error;
322 }
323
324 /*
325 * IPComp output routine, called by ipsec[46]_process_packet()
326 */
327 static int
328 ipcomp_output(
329 struct mbuf *m,
330 struct ipsecrequest *isr,
331 struct mbuf **mp,
332 int skip,
333 int protoff
334 )
335 {
336 struct secasvar *sav;
337 struct comp_algo *ipcompx;
338 int error, ralen, hlen, maxpacketsize, roff;
339 u_int8_t prot;
340 struct cryptodesc *crdc;
341 struct cryptop *crp;
342 struct tdb_crypto *tc;
343 struct mbuf *mo;
344 struct ipcomp *ipcomp;
345
346 IPSEC_SPLASSERT_SOFTNET(__func__);
347
348 sav = isr->sav;
349 IPSEC_ASSERT(sav != NULL, ("null SA"));
350 ipcompx = sav->tdb_compalgxform;
351 IPSEC_ASSERT(ipcompx != NULL, ("null compression xform"));
352
353 ralen = m->m_pkthdr.len - skip; /* Raw payload length before comp. */
354 hlen = IPCOMP_HLENGTH;
355
356 ipcompstat.ipcomps_output++;
357
358 /* Check for maximum packet size violations. */
359 switch (sav->sah->saidx.dst.sa.sa_family) {
360 #ifdef INET
361 case AF_INET:
362 maxpacketsize = IP_MAXPACKET;
363 break;
364 #endif /* INET */
365 #ifdef INET6
366 case AF_INET6:
367 maxpacketsize = IPV6_MAXPACKET;
368 break;
369 #endif /* INET6 */
370 default:
371 ipcompstat.ipcomps_nopf++;
372 DPRINTF(("%s: unknown/unsupported protocol family %d, "
373 "IPCA %s/%08lx\n", __func__,
374 sav->sah->saidx.dst.sa.sa_family,
375 ipsec_address(&sav->sah->saidx.dst),
376 (u_long) ntohl(sav->spi)));
377 error = EPFNOSUPPORT;
378 goto bad;
379 }
380 if (skip + hlen + ralen > maxpacketsize) {
381 ipcompstat.ipcomps_toobig++;
382 DPRINTF(("%s: packet in IPCA %s/%08lx got too big "
383 "(len %u, max len %u)\n", __func__,
384 ipsec_address(&sav->sah->saidx.dst),
385 (u_long) ntohl(sav->spi),
386 skip + hlen + ralen, maxpacketsize));
387 error = EMSGSIZE;
388 goto bad;
389 }
390
391 /* Update the counters */
392 ipcompstat.ipcomps_obytes += m->m_pkthdr.len - skip;
393
394 m = m_unshare(m, M_NOWAIT);
395 if (m == NULL) {
396 ipcompstat.ipcomps_hdrops++;
397 DPRINTF(("%s: cannot clone mbuf chain, IPCA %s/%08lx\n",
398 __func__, ipsec_address(&sav->sah->saidx.dst),
399 (u_long) ntohl(sav->spi)));
400 error = ENOBUFS;
401 goto bad;
402 }
403
404 /* Inject IPCOMP header */
405 mo = m_makespace(m, skip, hlen, &roff);
406 if (mo == NULL) {
407 ipcompstat.ipcomps_wrap++;
408 DPRINTF(("%s: IPCOMP header inject failed for IPCA %s/%08lx\n",
409 __func__, ipsec_address(&sav->sah->saidx.dst),
410 (u_long) ntohl(sav->spi)));
411 error = ENOBUFS;
412 goto bad;
413 }
414 ipcomp = (struct ipcomp *)(mtod(mo, caddr_t) + roff);
415
416 /* Initialize the IPCOMP header */
417 /* XXX alignment always correct? */
418 switch (sav->sah->saidx.dst.sa.sa_family) {
419 #ifdef INET
420 case AF_INET:
421 ipcomp->comp_nxt = mtod(m, struct ip *)->ip_p;
422 break;
423 #endif /* INET */
424 #ifdef INET6
425 case AF_INET6:
426 ipcomp->comp_nxt = mtod(m, struct ip6_hdr *)->ip6_nxt;
427 break;
428 #endif
429 }
430 ipcomp->comp_flags = 0;
431 ipcomp->comp_cpi = htons((u_int16_t) ntohl(sav->spi));
432
433 /* Fix Next Protocol in IPv4/IPv6 header */
434 prot = IPPROTO_IPCOMP;
435 m_copyback(m, protoff, sizeof(u_int8_t), (u_char *) &prot);
436
437 /* Ok now, we can pass to the crypto processing */
438
439 /* Get crypto descriptors */
440 crp = crypto_getreq(1);
441 if (crp == NULL) {
442 ipcompstat.ipcomps_crypto++;
443 DPRINTF(("%s: failed to acquire crypto descriptor\n",__func__));
444 error = ENOBUFS;
445 goto bad;
446 }
447 crdc = crp->crp_desc;
448
449 /* Compression descriptor */
450 crdc->crd_skip = skip + hlen;
451 crdc->crd_len = m->m_pkthdr.len - (skip + hlen);
452 crdc->crd_flags = CRD_F_COMP;
453 crdc->crd_inject = skip + hlen;
454
455 /* Compression operation */
456 crdc->crd_alg = ipcompx->type;
457
458 /* IPsec-specific opaque crypto info */
459 tc = (struct tdb_crypto *) malloc(sizeof(struct tdb_crypto),
460 M_XDATA, M_NOWAIT|M_ZERO);
461 if (tc == NULL) {
462 ipcompstat.ipcomps_crypto++;
463 DPRINTF(("%s: failed to allocate tdb_crypto\n", __func__));
464 crypto_freereq(crp);
465 error = ENOBUFS;
466 goto bad;
467 }
468
469 tc->tc_isr = isr;
470 tc->tc_spi = sav->spi;
471 tc->tc_dst = sav->sah->saidx.dst;
472 tc->tc_proto = sav->sah->saidx.proto;
473 tc->tc_skip = skip + hlen;
474
475 /* Crypto operation descriptor */
476 crp->crp_ilen = m->m_pkthdr.len; /* Total input length */
477 crp->crp_flags = CRYPTO_F_IMBUF | CRYPTO_F_CBIFSYNC;
478 crp->crp_buf = (caddr_t) m;
479 crp->crp_callback = ipcomp_output_cb;
480 crp->crp_opaque = (caddr_t) tc;
481 crp->crp_sid = sav->tdb_cryptoid;
482
483 return crypto_dispatch(crp);
484 bad:
485 if (m)
486 m_freem(m);
487 return (error);
488 }
489
490 /*
491 * IPComp output callback from the crypto driver.
492 */
493 static int
494 ipcomp_output_cb(struct cryptop *crp)
495 {
496 struct tdb_crypto *tc;
497 struct ipsecrequest *isr;
498 struct secasvar *sav;
499 struct mbuf *m;
500 int error, skip, rlen;
501
502 NET_LOCK_GIANT();
503
504 tc = (struct tdb_crypto *) crp->crp_opaque;
505 IPSEC_ASSERT(tc != NULL, ("null opaque data area!"));
506 m = (struct mbuf *) crp->crp_buf;
507 skip = tc->tc_skip;
508 rlen = crp->crp_ilen - skip;
509
510 isr = tc->tc_isr;
511 IPSECREQUEST_LOCK(isr);
512 sav = KEY_ALLOCSA(&tc->tc_dst, tc->tc_proto, tc->tc_spi);
513 if (sav == NULL) {
514 ipcompstat.ipcomps_notdb++;
515 DPRINTF(("%s: SA expired while in crypto\n", __func__));
516 error = ENOBUFS; /*XXX*/
517 goto bad;
518 }
519 IPSEC_ASSERT(isr->sav == sav, ("SA changed\n"));
520
521 /* Check for crypto errors */
522 if (crp->crp_etype) {
523 /* Reset session ID */
524 if (sav->tdb_cryptoid != 0)
525 sav->tdb_cryptoid = crp->crp_sid;
526
527 if (crp->crp_etype == EAGAIN) {
528 KEY_FREESAV(&sav);
529 IPSECREQUEST_UNLOCK(isr);
530 error = crypto_dispatch(crp);
531 NET_UNLOCK_GIANT();
532 return error;
533 }
534 ipcompstat.ipcomps_noxform++;
535 DPRINTF(("%s: crypto error %d\n", __func__, crp->crp_etype));
536 error = crp->crp_etype;
537 goto bad;
538 }
539 /* Shouldn't happen... */
540 if (m == NULL) {
541 ipcompstat.ipcomps_crypto++;
542 DPRINTF(("%s: bogus return buffer from crypto\n", __func__));
543 error = EINVAL;
544 goto bad;
545 }
546 ipcompstat.ipcomps_hist[sav->alg_comp]++;
547
548 if (rlen > crp->crp_olen) {
549 /* Adjust the length in the IP header */
550 switch (sav->sah->saidx.dst.sa.sa_family) {
551 #ifdef INET
552 case AF_INET:
553 mtod(m, struct ip *)->ip_len = htons(m->m_pkthdr.len);
554 break;
555 #endif /* INET */
556 #ifdef INET6
557 case AF_INET6:
558 mtod(m, struct ip6_hdr *)->ip6_plen =
559 htons(m->m_pkthdr.len) - sizeof(struct ip6_hdr);
560 break;
561 #endif /* INET6 */
562 default:
563 ipcompstat.ipcomps_nopf++;
564 DPRINTF(("%s: unknown/unsupported protocol "
565 "family %d, IPCA %s/%08lx\n", __func__,
566 sav->sah->saidx.dst.sa.sa_family,
567 ipsec_address(&sav->sah->saidx.dst),
568 (u_long) ntohl(sav->spi)));
569 error = EPFNOSUPPORT;
570 goto bad;
571 }
572 } else {
573 /* compression was useless, we have lost time */
574 /* XXX add statistic */
575 }
576
577 /* Release the crypto descriptor */
578 free(tc, M_XDATA);
579 crypto_freereq(crp);
580
581 /* NB: m is reclaimed by ipsec_process_done. */
582 error = ipsec_process_done(m, isr);
583 KEY_FREESAV(&sav);
584 IPSECREQUEST_UNLOCK(isr);
585 NET_UNLOCK_GIANT();
586 return error;
587 bad:
588 if (sav)
589 KEY_FREESAV(&sav);
590 IPSECREQUEST_UNLOCK(isr);
591 if (m)
592 m_freem(m);
593 free(tc, M_XDATA);
594 crypto_freereq(crp);
595 NET_UNLOCK_GIANT();
596 return error;
597 }
598
599 static struct xformsw ipcomp_xformsw = {
600 XF_IPCOMP, XFT_COMP, "IPcomp",
601 ipcomp_init, ipcomp_zeroize, ipcomp_input,
602 ipcomp_output
603 };
604
605 static void
606 ipcomp_attach(void)
607 {
608 xform_register(&ipcomp_xformsw);
609 }
610 SYSINIT(ipcomp_xform_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ipcomp_attach, NULL);
Cache object: da58554ff55dfc073d7bc58a5f408c2e
|