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