]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ipcomp_core.c
xnu-124.13.tar.gz
[apple/xnu.git] / bsd / netinet6 / ipcomp_core.c
1 /* $KAME: ipcomp_core.c,v 1.10 2000/02/22 14:04:23 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1999 WIDE Project.
5 * All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * RFC2393 IP payload compression protocol (IPComp).
34 */
35
36 #define _IP_VHL
37 #if (defined(__FreeBSD__) && __FreeBSD__ >= 3) || defined(__NetBSD__)
38 #include "opt_inet.h"
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/errno.h>
49 #include <sys/time.h>
50 #include <sys/kernel.h>
51 #include <sys/syslog.h>
52 #include <sys/queue.h>
53
54 #include <net/if.h>
55 #include <net/route.h>
56 #include <net/netisr.h>
57 #include <net/zlib.h>
58 #include <kern/cpu_number.h>
59
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
63 #include <netinet6/ipcomp.h>
64 #include <netinet6/ipsec.h>
65
66 #include <net/net_osdep.h>
67
68 static void *deflate_alloc __P((void *, u_int, u_int));
69 static void deflate_free __P((void *, void *));
70 static int deflate_common __P((struct mbuf *, struct mbuf *, size_t *, int));
71 static int deflate_compress __P((struct mbuf *, struct mbuf *, size_t *));
72 static int deflate_decompress __P((struct mbuf *, struct mbuf *, size_t *));
73
74 /*
75 * We need to use default window size (2^15 = 32Kbytes as of writing) for
76 * inbound case. Otherwise we get interop problem.
77 * Use negative value to avoid Adler32 checksum. This is an undocumented
78 * feature in zlib (see ipsec wg mailing list archive in January 2000).
79 */
80 static int deflate_policy = Z_DEFAULT_COMPRESSION;
81 static int deflate_window_out = -12;
82 static const int deflate_window_in = -1 * MAX_WBITS; /* don't change it */
83 static int deflate_memlevel = MAX_MEM_LEVEL;
84
85 struct ipcomp_algorithm ipcomp_algorithms[] = {
86 { NULL, NULL, -1 },
87 { NULL, NULL, -1 },
88 { deflate_compress, deflate_decompress, 90 },
89 { NULL, NULL, 90 },
90 };
91
92 static void *
93 deflate_alloc(aux, items, siz)
94 void *aux;
95 u_int items;
96 u_int siz;
97 {
98 void *ptr;
99 MALLOC(ptr, void *, items * siz, M_TEMP, M_NOWAIT);
100 return ptr;
101 }
102
103 static void
104 deflate_free(aux, ptr)
105 void *aux;
106 void *ptr;
107 {
108 FREE(ptr, M_TEMP);
109 }
110
111 static int
112 deflate_common(m, md, lenp, mode)
113 struct mbuf *m;
114 struct mbuf *md;
115 size_t *lenp;
116 int mode; /* 0: compress 1: decompress */
117 {
118 struct mbuf *mprev;
119 struct mbuf *p;
120 struct mbuf *n, *n0 = NULL, **np;
121 z_stream zs;
122 int error = 0;
123 int zerror;
124 size_t offset;
125 int firsttime, final, flush;
126
127 for (mprev = m; mprev && mprev->m_next != md; mprev = mprev->m_next)
128 ;
129 if (!mprev)
130 panic("md is not in m in deflate_common");
131
132 bzero(&zs, sizeof(zs));
133 zs.zalloc = deflate_alloc;
134 zs.zfree = deflate_free;
135
136 zerror = mode ? inflateInit2(&zs, deflate_window_in)
137 : deflateInit2(&zs, deflate_policy, Z_DEFLATED,
138 deflate_window_out, deflate_memlevel,
139 Z_DEFAULT_STRATEGY);
140 if (zerror != Z_OK) {
141 error = ENOBUFS;
142 goto fail;
143 }
144
145 n0 = n = NULL;
146 np = &n0;
147 offset = 0;
148 firsttime = 1;
149 final = 0;
150 flush = Z_NO_FLUSH;
151 zerror = 0;
152 p = md;
153 while (1) {
154 /*
155 * first time, we need to setup the buffer before calling
156 * compression function.
157 */
158 if (firsttime)
159 firsttime = 0;
160 else {
161 zerror = mode ? inflate(&zs, flush)
162 : deflate(&zs, flush);
163 }
164
165 /* get input buffer */
166 if (p && zs.avail_in == 0) {
167 zs.next_in = mtod(p, u_int8_t *);
168 zs.avail_in = p->m_len;
169 p = p->m_next;
170 if (!p) {
171 final = 1;
172 flush = Z_PARTIAL_FLUSH;
173 }
174 }
175
176 /* get output buffer */
177 if (zs.next_out == NULL || zs.avail_out == 0) {
178 /* keep the reply buffer into our chain */
179 if (n) {
180 n->m_len = zs.total_out - offset;
181 offset = zs.total_out;
182 *np = n;
183 np = &n->m_next;
184 }
185
186 /* get a fresh reply buffer */
187 MGET(n, M_DONTWAIT, MT_DATA);
188 if (n) {
189 MCLGET(n, M_DONTWAIT);
190 }
191 if (!n) {
192 error = ENOBUFS;
193 goto fail;
194 }
195 n->m_len = 0;
196 n->m_len = M_TRAILINGSPACE(n);
197 n->m_next = NULL;
198 /*
199 * if this is the first reply buffer, reserve
200 * region for ipcomp header.
201 */
202 if (*np == NULL) {
203 n->m_len -= sizeof(struct ipcomp);
204 n->m_data += sizeof(struct ipcomp);
205 }
206
207 zs.next_out = mtod(n, u_int8_t *);
208 zs.avail_out = n->m_len;
209 }
210
211 if (zerror == Z_OK) {
212 /*
213 * to terminate deflate/inflate process, we need to
214 * call {in,de}flate() with different flushing methods.
215 *
216 * deflate() needs at least one Z_PARTIAL_FLUSH,
217 * then use Z_FINISH until we get to the end.
218 * (if we use Z_FLUSH without Z_PARTIAL_FLUSH, deflate()
219 * will assume contiguous single output buffer, and that
220 * is not what we want)
221 * inflate() does not care about flushing method, but
222 * needs output buffer until it gets to the end.
223 *
224 * the most outer loop will be terminated with
225 * Z_STREAM_END.
226 */
227 if (final == 1) {
228 /* reached end of mbuf chain */
229 if (mode == 0)
230 final = 2;
231 else
232 final = 3;
233 } else if (final == 2) {
234 /* terminate deflate case */
235 flush = Z_FINISH;
236 } else if (final == 3) {
237 /* terminate inflate case */
238 ;
239 }
240 } else if (zerror == Z_STREAM_END)
241 break;
242 else {
243 ipseclog((LOG_ERR, "ipcomp_%scompress: %sflate: %s\n",
244 mode ? "de" : "", mode ? "in" : "de",
245 zs.msg ? zs.msg : "unknown error"));
246 error = EINVAL;
247 goto fail;
248 }
249 }
250 zerror = mode ? inflateEnd(&zs) : deflateEnd(&zs);
251 if (zerror != Z_OK) {
252 ipseclog((LOG_ERR, "ipcomp_%scompress: %sflate: %s\n",
253 mode ? "de" : "", mode ? "in" : "de",
254 zs.msg ? zs.msg : "unknown error"));
255 error = EINVAL;
256 goto fail;
257 }
258 /* keep the final reply buffer into our chain */
259 if (n) {
260 n->m_len = zs.total_out - offset;
261 offset = zs.total_out;
262 *np = n;
263 np = &n->m_next;
264 }
265
266 /* switch the mbuf to the new one */
267 mprev->m_next = n0;
268 m_freem(md);
269 *lenp = zs.total_out;
270
271 return 0;
272
273 fail:
274 if (m)
275 m_freem(m);
276 if (n0)
277 m_freem(n0);
278 return error;
279 }
280
281 static int
282 deflate_compress(m, md, lenp)
283 struct mbuf *m;
284 struct mbuf *md;
285 size_t *lenp;
286 {
287 if (!m)
288 panic("m == NULL in deflate_compress");
289 if (!md)
290 panic("md == NULL in deflate_compress");
291 if (!lenp)
292 panic("lenp == NULL in deflate_compress");
293
294 return deflate_common(m, md, lenp, 0);
295 }
296
297 static int
298 deflate_decompress(m, md, lenp)
299 struct mbuf *m;
300 struct mbuf *md;
301 size_t *lenp;
302 {
303 if (!m)
304 panic("m == NULL in deflate_decompress");
305 if (!md)
306 panic("md == NULL in deflate_decompress");
307 if (!lenp)
308 panic("lenp == NULL in deflate_decompress");
309
310 return deflate_common(m, md, lenp, 1);
311 }