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