ppp-deflate.c revision 1.9 1 1.9 lukem /* $NetBSD: ppp-deflate.c,v 1.9 2001/11/12 23:49:46 lukem Exp $ */
2 1.4 christos /* Id: ppp-deflate.c,v 1.5 1997/03/04 03:33:28 paulus Exp */
3 1.1 paulus
4 1.1 paulus /*
5 1.1 paulus * ppp_deflate.c - interface the zlib procedures for Deflate compression
6 1.1 paulus * and decompression (as used by gzip) to the PPP code.
7 1.1 paulus * This version is for use with mbufs on BSD-derived systems.
8 1.1 paulus *
9 1.1 paulus * Copyright (c) 1994 The Australian National University.
10 1.1 paulus * All rights reserved.
11 1.1 paulus *
12 1.1 paulus * Permission to use, copy, modify, and distribute this software and its
13 1.1 paulus * documentation is hereby granted, provided that the above copyright
14 1.1 paulus * notice appears in all copies. This software is provided without any
15 1.1 paulus * warranty, express or implied. The Australian National University
16 1.1 paulus * makes no representations about the suitability of this software for
17 1.1 paulus * any purpose.
18 1.1 paulus *
19 1.1 paulus * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
20 1.1 paulus * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21 1.1 paulus * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
22 1.1 paulus * THE AUSTRALIAN NATIONAL UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY
23 1.1 paulus * OF SUCH DAMAGE.
24 1.1 paulus *
25 1.1 paulus * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26 1.1 paulus * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 1.1 paulus * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
28 1.1 paulus * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
29 1.1 paulus * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
30 1.1 paulus * OR MODIFICATIONS.
31 1.1 paulus */
32 1.9 lukem
33 1.9 lukem #include <sys/cdefs.h>
34 1.9 lukem __KERNEL_RCSID(0, "$NetBSD: ppp-deflate.c,v 1.9 2001/11/12 23:49:46 lukem Exp $");
35 1.1 paulus
36 1.1 paulus #include <sys/param.h>
37 1.1 paulus #include <sys/types.h>
38 1.1 paulus #include <sys/systm.h>
39 1.1 paulus #include <sys/mbuf.h>
40 1.1 paulus #include <net/ppp_defs.h>
41 1.1 paulus #include <net/zlib.h>
42 1.1 paulus
43 1.1 paulus #define PACKETPTR struct mbuf *
44 1.1 paulus #include <net/ppp-comp.h>
45 1.1 paulus
46 1.1 paulus #if DO_DEFLATE
47 1.1 paulus
48 1.4 christos #define DEFLATE_DEBUG 1
49 1.4 christos
50 1.1 paulus /*
51 1.1 paulus * State for a Deflate (de)compressor.
52 1.1 paulus */
53 1.1 paulus struct deflate_state {
54 1.1 paulus int seqno;
55 1.1 paulus int w_size;
56 1.1 paulus int unit;
57 1.1 paulus int hdrlen;
58 1.1 paulus int mru;
59 1.1 paulus int debug;
60 1.1 paulus z_stream strm;
61 1.1 paulus struct compstat stats;
62 1.1 paulus };
63 1.1 paulus
64 1.1 paulus #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
65 1.1 paulus
66 1.1 paulus static void *zalloc __P((void *, u_int items, u_int size));
67 1.6 christos static void zfree __P((void *, void *ptr));
68 1.1 paulus static void *z_comp_alloc __P((u_char *options, int opt_len));
69 1.1 paulus static void *z_decomp_alloc __P((u_char *options, int opt_len));
70 1.1 paulus static void z_comp_free __P((void *state));
71 1.1 paulus static void z_decomp_free __P((void *state));
72 1.1 paulus static int z_comp_init __P((void *state, u_char *options, int opt_len,
73 1.1 paulus int unit, int hdrlen, int debug));
74 1.1 paulus static int z_decomp_init __P((void *state, u_char *options, int opt_len,
75 1.1 paulus int unit, int hdrlen, int mru, int debug));
76 1.1 paulus static int z_compress __P((void *state, struct mbuf **mret,
77 1.1 paulus struct mbuf *mp, int slen, int maxolen));
78 1.1 paulus static void z_incomp __P((void *state, struct mbuf *dmsg));
79 1.1 paulus static int z_decompress __P((void *state, struct mbuf *cmp,
80 1.1 paulus struct mbuf **dmpp));
81 1.1 paulus static void z_comp_reset __P((void *state));
82 1.1 paulus static void z_decomp_reset __P((void *state));
83 1.1 paulus static void z_comp_stats __P((void *state, struct compstat *stats));
84 1.1 paulus
85 1.1 paulus /*
86 1.1 paulus * Procedures exported to if_ppp.c.
87 1.1 paulus */
88 1.1 paulus struct compressor ppp_deflate = {
89 1.1 paulus CI_DEFLATE, /* compress_proto */
90 1.1 paulus z_comp_alloc, /* comp_alloc */
91 1.1 paulus z_comp_free, /* comp_free */
92 1.1 paulus z_comp_init, /* comp_init */
93 1.1 paulus z_comp_reset, /* comp_reset */
94 1.1 paulus z_compress, /* compress */
95 1.1 paulus z_comp_stats, /* comp_stat */
96 1.1 paulus z_decomp_alloc, /* decomp_alloc */
97 1.1 paulus z_decomp_free, /* decomp_free */
98 1.1 paulus z_decomp_init, /* decomp_init */
99 1.1 paulus z_decomp_reset, /* decomp_reset */
100 1.1 paulus z_decompress, /* decompress */
101 1.1 paulus z_incomp, /* incomp */
102 1.1 paulus z_comp_stats, /* decomp_stat */
103 1.1 paulus };
104 1.1 paulus
105 1.6 christos struct compressor ppp_deflate_draft = {
106 1.6 christos CI_DEFLATE_DRAFT, /* compress_proto */
107 1.6 christos z_comp_alloc, /* comp_alloc */
108 1.6 christos z_comp_free, /* comp_free */
109 1.6 christos z_comp_init, /* comp_init */
110 1.6 christos z_comp_reset, /* comp_reset */
111 1.6 christos z_compress, /* compress */
112 1.6 christos z_comp_stats, /* comp_stat */
113 1.6 christos z_decomp_alloc, /* decomp_alloc */
114 1.6 christos z_decomp_free, /* decomp_free */
115 1.6 christos z_decomp_init, /* decomp_init */
116 1.6 christos z_decomp_reset, /* decomp_reset */
117 1.6 christos z_decompress, /* decompress */
118 1.6 christos z_incomp, /* incomp */
119 1.6 christos z_comp_stats, /* decomp_stat */
120 1.6 christos };
121 1.1 paulus /*
122 1.1 paulus * Space allocation and freeing routines for use by zlib routines.
123 1.1 paulus */
124 1.1 paulus void *
125 1.1 paulus zalloc(notused, items, size)
126 1.1 paulus void *notused;
127 1.1 paulus u_int items, size;
128 1.1 paulus {
129 1.1 paulus void *ptr;
130 1.1 paulus
131 1.7 thorpej ptr = malloc(items * size, M_DEVBUF, M_NOWAIT);
132 1.1 paulus return ptr;
133 1.1 paulus }
134 1.1 paulus
135 1.1 paulus void
136 1.6 christos zfree(notused, ptr)
137 1.1 paulus void *notused;
138 1.1 paulus void *ptr;
139 1.1 paulus {
140 1.7 thorpej free(ptr, M_DEVBUF);
141 1.1 paulus }
142 1.1 paulus
143 1.1 paulus /*
144 1.1 paulus * Allocate space for a compressor.
145 1.1 paulus */
146 1.1 paulus static void *
147 1.1 paulus z_comp_alloc(options, opt_len)
148 1.1 paulus u_char *options;
149 1.1 paulus int opt_len;
150 1.1 paulus {
151 1.1 paulus struct deflate_state *state;
152 1.1 paulus int w_size;
153 1.1 paulus
154 1.6 christos if (opt_len != CILEN_DEFLATE
155 1.6 christos || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
156 1.1 paulus || options[1] != CILEN_DEFLATE
157 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
158 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
159 1.1 paulus return NULL;
160 1.1 paulus w_size = DEFLATE_SIZE(options[2]);
161 1.1 paulus if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
162 1.1 paulus return NULL;
163 1.1 paulus
164 1.1 paulus MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
165 1.1 paulus M_DEVBUF, M_NOWAIT);
166 1.1 paulus if (state == NULL)
167 1.1 paulus return NULL;
168 1.1 paulus
169 1.1 paulus state->strm.next_in = NULL;
170 1.1 paulus state->strm.zalloc = zalloc;
171 1.1 paulus state->strm.zfree = zfree;
172 1.1 paulus if (deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION, DEFLATE_METHOD_VAL,
173 1.6 christos -w_size, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
174 1.1 paulus FREE(state, M_DEVBUF);
175 1.1 paulus return NULL;
176 1.1 paulus }
177 1.1 paulus
178 1.1 paulus state->w_size = w_size;
179 1.8 thorpej memset(&state->stats, 0, sizeof(state->stats));
180 1.1 paulus return (void *) state;
181 1.1 paulus }
182 1.1 paulus
183 1.1 paulus static void
184 1.1 paulus z_comp_free(arg)
185 1.1 paulus void *arg;
186 1.1 paulus {
187 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
188 1.1 paulus
189 1.1 paulus deflateEnd(&state->strm);
190 1.1 paulus FREE(state, M_DEVBUF);
191 1.1 paulus }
192 1.1 paulus
193 1.1 paulus static int
194 1.1 paulus z_comp_init(arg, options, opt_len, unit, hdrlen, debug)
195 1.1 paulus void *arg;
196 1.1 paulus u_char *options;
197 1.1 paulus int opt_len, unit, hdrlen, debug;
198 1.1 paulus {
199 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
200 1.1 paulus
201 1.6 christos if (opt_len < CILEN_DEFLATE
202 1.6 christos || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
203 1.1 paulus || options[1] != CILEN_DEFLATE
204 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
205 1.1 paulus || DEFLATE_SIZE(options[2]) != state->w_size
206 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
207 1.1 paulus return 0;
208 1.1 paulus
209 1.1 paulus state->seqno = 0;
210 1.1 paulus state->unit = unit;
211 1.1 paulus state->hdrlen = hdrlen;
212 1.1 paulus state->debug = debug;
213 1.1 paulus
214 1.1 paulus deflateReset(&state->strm);
215 1.1 paulus
216 1.1 paulus return 1;
217 1.1 paulus }
218 1.1 paulus
219 1.1 paulus static void
220 1.1 paulus z_comp_reset(arg)
221 1.1 paulus void *arg;
222 1.1 paulus {
223 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
224 1.1 paulus
225 1.1 paulus state->seqno = 0;
226 1.1 paulus deflateReset(&state->strm);
227 1.1 paulus }
228 1.1 paulus
229 1.1 paulus int
230 1.1 paulus z_compress(arg, mret, mp, orig_len, maxolen)
231 1.1 paulus void *arg;
232 1.1 paulus struct mbuf **mret; /* compressed packet (out) */
233 1.1 paulus struct mbuf *mp; /* uncompressed packet (in) */
234 1.1 paulus int orig_len, maxolen;
235 1.1 paulus {
236 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
237 1.1 paulus u_char *rptr, *wptr;
238 1.1 paulus int proto, olen, wspace, r, flush;
239 1.1 paulus struct mbuf *m;
240 1.1 paulus
241 1.1 paulus /*
242 1.1 paulus * Check that the protocol is in the range we handle.
243 1.1 paulus */
244 1.1 paulus rptr = mtod(mp, u_char *);
245 1.1 paulus proto = PPP_PROTOCOL(rptr);
246 1.1 paulus if (proto > 0x3fff || proto == 0xfd || proto == 0xfb) {
247 1.1 paulus *mret = NULL;
248 1.1 paulus return orig_len;
249 1.1 paulus }
250 1.1 paulus
251 1.1 paulus /* Allocate one mbuf initially. */
252 1.1 paulus if (maxolen > orig_len)
253 1.1 paulus maxolen = orig_len;
254 1.1 paulus MGET(m, M_DONTWAIT, MT_DATA);
255 1.1 paulus *mret = m;
256 1.1 paulus if (m != NULL) {
257 1.1 paulus m->m_len = 0;
258 1.1 paulus if (maxolen + state->hdrlen > MLEN)
259 1.1 paulus MCLGET(m, M_DONTWAIT);
260 1.1 paulus wspace = M_TRAILINGSPACE(m);
261 1.1 paulus if (state->hdrlen + PPP_HDRLEN + 2 < wspace) {
262 1.1 paulus m->m_data += state->hdrlen;
263 1.1 paulus wspace -= state->hdrlen;
264 1.1 paulus }
265 1.1 paulus wptr = mtod(m, u_char *);
266 1.1 paulus
267 1.1 paulus /*
268 1.1 paulus * Copy over the PPP header and store the 2-byte sequence number.
269 1.1 paulus */
270 1.1 paulus wptr[0] = PPP_ADDRESS(rptr);
271 1.1 paulus wptr[1] = PPP_CONTROL(rptr);
272 1.1 paulus wptr[2] = PPP_COMP >> 8;
273 1.1 paulus wptr[3] = PPP_COMP;
274 1.1 paulus wptr += PPP_HDRLEN;
275 1.1 paulus wptr[0] = state->seqno >> 8;
276 1.1 paulus wptr[1] = state->seqno;
277 1.1 paulus wptr += 2;
278 1.1 paulus state->strm.next_out = wptr;
279 1.1 paulus state->strm.avail_out = wspace - (PPP_HDRLEN + 2);
280 1.1 paulus } else {
281 1.1 paulus state->strm.next_out = NULL;
282 1.1 paulus state->strm.avail_out = 1000000;
283 1.1 paulus wptr = NULL;
284 1.1 paulus wspace = 0;
285 1.1 paulus }
286 1.1 paulus ++state->seqno;
287 1.1 paulus
288 1.1 paulus rptr += (proto > 0xff)? 2: 3; /* skip 1st proto byte if 0 */
289 1.1 paulus state->strm.next_in = rptr;
290 1.1 paulus state->strm.avail_in = mtod(mp, u_char *) + mp->m_len - rptr;
291 1.1 paulus mp = mp->m_next;
292 1.1 paulus flush = (mp == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
293 1.1 paulus olen = 0;
294 1.1 paulus for (;;) {
295 1.1 paulus r = deflate(&state->strm, flush);
296 1.1 paulus if (r != Z_OK) {
297 1.3 christos printf("z_compress: deflate returned %d (%s)\n",
298 1.4 christos r, (state->strm.msg? state->strm.msg: ""));
299 1.1 paulus break;
300 1.1 paulus }
301 1.1 paulus if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
302 1.1 paulus break; /* all done */
303 1.1 paulus if (state->strm.avail_in == 0 && mp != NULL) {
304 1.1 paulus state->strm.next_in = mtod(mp, u_char *);
305 1.1 paulus state->strm.avail_in = mp->m_len;
306 1.1 paulus mp = mp->m_next;
307 1.1 paulus if (mp == NULL)
308 1.1 paulus flush = Z_PACKET_FLUSH;
309 1.1 paulus }
310 1.1 paulus if (state->strm.avail_out == 0) {
311 1.1 paulus if (m != NULL) {
312 1.1 paulus m->m_len = wspace;
313 1.1 paulus olen += wspace;
314 1.1 paulus MGET(m->m_next, M_DONTWAIT, MT_DATA);
315 1.1 paulus m = m->m_next;
316 1.1 paulus if (m != NULL) {
317 1.1 paulus m->m_len = 0;
318 1.1 paulus if (maxolen - olen > MLEN)
319 1.1 paulus MCLGET(m, M_DONTWAIT);
320 1.1 paulus state->strm.next_out = mtod(m, u_char *);
321 1.1 paulus state->strm.avail_out = wspace = M_TRAILINGSPACE(m);
322 1.1 paulus }
323 1.1 paulus }
324 1.1 paulus if (m == NULL) {
325 1.1 paulus state->strm.next_out = NULL;
326 1.1 paulus state->strm.avail_out = 1000000;
327 1.1 paulus }
328 1.1 paulus }
329 1.1 paulus }
330 1.1 paulus if (m != NULL)
331 1.1 paulus olen += (m->m_len = wspace - state->strm.avail_out);
332 1.1 paulus
333 1.1 paulus /*
334 1.1 paulus * See if we managed to reduce the size of the packet.
335 1.1 paulus */
336 1.6 christos if (m != NULL && olen < orig_len) {
337 1.1 paulus state->stats.comp_bytes += olen;
338 1.1 paulus state->stats.comp_packets++;
339 1.1 paulus } else {
340 1.1 paulus if (*mret != NULL) {
341 1.1 paulus m_freem(*mret);
342 1.1 paulus *mret = NULL;
343 1.1 paulus }
344 1.1 paulus state->stats.inc_bytes += orig_len;
345 1.1 paulus state->stats.inc_packets++;
346 1.1 paulus olen = orig_len;
347 1.1 paulus }
348 1.1 paulus state->stats.unc_bytes += orig_len;
349 1.1 paulus state->stats.unc_packets++;
350 1.1 paulus
351 1.1 paulus return olen;
352 1.1 paulus }
353 1.1 paulus
354 1.1 paulus static void
355 1.1 paulus z_comp_stats(arg, stats)
356 1.1 paulus void *arg;
357 1.1 paulus struct compstat *stats;
358 1.1 paulus {
359 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
360 1.1 paulus u_int out;
361 1.1 paulus
362 1.1 paulus *stats = state->stats;
363 1.1 paulus stats->ratio = stats->unc_bytes;
364 1.1 paulus out = stats->comp_bytes + stats->inc_bytes;
365 1.1 paulus if (stats->ratio <= 0x7ffffff)
366 1.1 paulus stats->ratio <<= 8;
367 1.1 paulus else
368 1.1 paulus out >>= 8;
369 1.1 paulus if (out != 0)
370 1.1 paulus stats->ratio /= out;
371 1.1 paulus }
372 1.1 paulus
373 1.1 paulus /*
374 1.1 paulus * Allocate space for a decompressor.
375 1.1 paulus */
376 1.1 paulus static void *
377 1.1 paulus z_decomp_alloc(options, opt_len)
378 1.1 paulus u_char *options;
379 1.1 paulus int opt_len;
380 1.1 paulus {
381 1.1 paulus struct deflate_state *state;
382 1.1 paulus int w_size;
383 1.1 paulus
384 1.6 christos if (opt_len != CILEN_DEFLATE
385 1.6 christos || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
386 1.1 paulus || options[1] != CILEN_DEFLATE
387 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
388 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
389 1.1 paulus return NULL;
390 1.1 paulus w_size = DEFLATE_SIZE(options[2]);
391 1.1 paulus if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
392 1.1 paulus return NULL;
393 1.1 paulus
394 1.1 paulus MALLOC(state, struct deflate_state *, sizeof(struct deflate_state),
395 1.1 paulus M_DEVBUF, M_NOWAIT);
396 1.1 paulus if (state == NULL)
397 1.1 paulus return NULL;
398 1.1 paulus
399 1.1 paulus state->strm.next_out = NULL;
400 1.1 paulus state->strm.zalloc = zalloc;
401 1.1 paulus state->strm.zfree = zfree;
402 1.1 paulus if (inflateInit2(&state->strm, -w_size) != Z_OK) {
403 1.1 paulus FREE(state, M_DEVBUF);
404 1.1 paulus return NULL;
405 1.1 paulus }
406 1.1 paulus
407 1.1 paulus state->w_size = w_size;
408 1.8 thorpej memset(&state->stats, 0, sizeof(state->stats));
409 1.1 paulus return (void *) state;
410 1.1 paulus }
411 1.1 paulus
412 1.1 paulus static void
413 1.1 paulus z_decomp_free(arg)
414 1.1 paulus void *arg;
415 1.1 paulus {
416 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
417 1.1 paulus
418 1.1 paulus inflateEnd(&state->strm);
419 1.1 paulus FREE(state, M_DEVBUF);
420 1.1 paulus }
421 1.1 paulus
422 1.1 paulus static int
423 1.1 paulus z_decomp_init(arg, options, opt_len, unit, hdrlen, mru, debug)
424 1.1 paulus void *arg;
425 1.1 paulus u_char *options;
426 1.1 paulus int opt_len, unit, hdrlen, mru, debug;
427 1.1 paulus {
428 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
429 1.1 paulus
430 1.6 christos if (opt_len < CILEN_DEFLATE
431 1.6 christos || (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT)
432 1.1 paulus || options[1] != CILEN_DEFLATE
433 1.1 paulus || DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL
434 1.1 paulus || DEFLATE_SIZE(options[2]) != state->w_size
435 1.1 paulus || options[3] != DEFLATE_CHK_SEQUENCE)
436 1.1 paulus return 0;
437 1.1 paulus
438 1.1 paulus state->seqno = 0;
439 1.1 paulus state->unit = unit;
440 1.1 paulus state->hdrlen = hdrlen;
441 1.1 paulus state->debug = debug;
442 1.1 paulus state->mru = mru;
443 1.1 paulus
444 1.1 paulus inflateReset(&state->strm);
445 1.1 paulus
446 1.1 paulus return 1;
447 1.1 paulus }
448 1.1 paulus
449 1.1 paulus static void
450 1.1 paulus z_decomp_reset(arg)
451 1.1 paulus void *arg;
452 1.1 paulus {
453 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
454 1.1 paulus
455 1.1 paulus state->seqno = 0;
456 1.1 paulus inflateReset(&state->strm);
457 1.1 paulus }
458 1.1 paulus
459 1.1 paulus /*
460 1.1 paulus * Decompress a Deflate-compressed packet.
461 1.1 paulus *
462 1.1 paulus * Because of patent problems, we return DECOMP_ERROR for errors
463 1.1 paulus * found by inspecting the input data and for system problems, but
464 1.1 paulus * DECOMP_FATALERROR for any errors which could possibly be said to
465 1.1 paulus * be being detected "after" decompression. For DECOMP_ERROR,
466 1.1 paulus * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
467 1.1 paulus * infringing a patent of Motorola's if we do, so we take CCP down
468 1.1 paulus * instead.
469 1.1 paulus *
470 1.1 paulus * Given that the frame has the correct sequence number and a good FCS,
471 1.1 paulus * errors such as invalid codes in the input most likely indicate a
472 1.1 paulus * bug, so we return DECOMP_FATALERROR for them in order to turn off
473 1.1 paulus * compression, even though they are detected by inspecting the input.
474 1.1 paulus */
475 1.1 paulus int
476 1.1 paulus z_decompress(arg, mi, mop)
477 1.1 paulus void *arg;
478 1.1 paulus struct mbuf *mi, **mop;
479 1.1 paulus {
480 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
481 1.1 paulus struct mbuf *mo, *mo_head;
482 1.1 paulus u_char *rptr, *wptr;
483 1.1 paulus int rlen, olen, ospace;
484 1.1 paulus int seq, i, flush, r, decode_proto;
485 1.1 paulus u_char hdr[PPP_HDRLEN + DEFLATE_OVHD];
486 1.1 paulus
487 1.1 paulus *mop = NULL;
488 1.1 paulus rptr = mtod(mi, u_char *);
489 1.1 paulus rlen = mi->m_len;
490 1.1 paulus for (i = 0; i < PPP_HDRLEN + DEFLATE_OVHD; ++i) {
491 1.1 paulus while (rlen <= 0) {
492 1.1 paulus mi = mi->m_next;
493 1.1 paulus if (mi == NULL)
494 1.1 paulus return DECOMP_ERROR;
495 1.1 paulus rptr = mtod(mi, u_char *);
496 1.1 paulus rlen = mi->m_len;
497 1.1 paulus }
498 1.1 paulus hdr[i] = *rptr++;
499 1.1 paulus --rlen;
500 1.1 paulus }
501 1.1 paulus
502 1.1 paulus /* Check the sequence number. */
503 1.1 paulus seq = (hdr[PPP_HDRLEN] << 8) + hdr[PPP_HDRLEN+1];
504 1.1 paulus if (seq != state->seqno) {
505 1.1 paulus if (state->debug)
506 1.3 christos printf("z_decompress%d: bad seq # %d, expected %d\n",
507 1.4 christos state->unit, seq, state->seqno);
508 1.1 paulus return DECOMP_ERROR;
509 1.1 paulus }
510 1.1 paulus ++state->seqno;
511 1.1 paulus
512 1.1 paulus /* Allocate an output mbuf. */
513 1.1 paulus MGETHDR(mo, M_DONTWAIT, MT_DATA);
514 1.1 paulus if (mo == NULL)
515 1.1 paulus return DECOMP_ERROR;
516 1.1 paulus mo_head = mo;
517 1.1 paulus mo->m_len = 0;
518 1.1 paulus mo->m_next = NULL;
519 1.1 paulus MCLGET(mo, M_DONTWAIT);
520 1.1 paulus ospace = M_TRAILINGSPACE(mo);
521 1.1 paulus if (state->hdrlen + PPP_HDRLEN < ospace) {
522 1.1 paulus mo->m_data += state->hdrlen;
523 1.1 paulus ospace -= state->hdrlen;
524 1.1 paulus }
525 1.1 paulus
526 1.1 paulus /*
527 1.1 paulus * Fill in the first part of the PPP header. The protocol field
528 1.1 paulus * comes from the decompressed data.
529 1.1 paulus */
530 1.1 paulus wptr = mtod(mo, u_char *);
531 1.1 paulus wptr[0] = PPP_ADDRESS(hdr);
532 1.1 paulus wptr[1] = PPP_CONTROL(hdr);
533 1.1 paulus wptr[2] = 0;
534 1.1 paulus
535 1.1 paulus /*
536 1.1 paulus * Set up to call inflate. We set avail_out to 1 initially so we can
537 1.1 paulus * look at the first byte of the output and decide whether we have
538 1.1 paulus * a 1-byte or 2-byte protocol field.
539 1.1 paulus */
540 1.1 paulus state->strm.next_in = rptr;
541 1.1 paulus state->strm.avail_in = rlen;
542 1.1 paulus mi = mi->m_next;
543 1.1 paulus flush = (mi == NULL)? Z_PACKET_FLUSH: Z_NO_FLUSH;
544 1.1 paulus rlen += PPP_HDRLEN + DEFLATE_OVHD;
545 1.1 paulus state->strm.next_out = wptr + 3;
546 1.1 paulus state->strm.avail_out = 1;
547 1.1 paulus decode_proto = 1;
548 1.1 paulus olen = PPP_HDRLEN;
549 1.1 paulus
550 1.1 paulus /*
551 1.1 paulus * Call inflate, supplying more input or output as needed.
552 1.1 paulus */
553 1.1 paulus for (;;) {
554 1.1 paulus r = inflate(&state->strm, flush);
555 1.1 paulus if (r != Z_OK) {
556 1.4 christos #if !DEFLATE_DEBUG
557 1.1 paulus if (state->debug)
558 1.4 christos #endif
559 1.3 christos printf("z_decompress%d: inflate returned %d (%s)\n",
560 1.4 christos state->unit, r, (state->strm.msg? state->strm.msg: ""));
561 1.1 paulus m_freem(mo_head);
562 1.1 paulus return DECOMP_FATALERROR;
563 1.1 paulus }
564 1.1 paulus if (flush != Z_NO_FLUSH && state->strm.avail_out != 0)
565 1.1 paulus break; /* all done */
566 1.1 paulus if (state->strm.avail_in == 0 && mi != NULL) {
567 1.1 paulus state->strm.next_in = mtod(mi, u_char *);
568 1.1 paulus state->strm.avail_in = mi->m_len;
569 1.1 paulus rlen += mi->m_len;
570 1.1 paulus mi = mi->m_next;
571 1.1 paulus if (mi == NULL)
572 1.1 paulus flush = Z_PACKET_FLUSH;
573 1.1 paulus }
574 1.1 paulus if (state->strm.avail_out == 0) {
575 1.1 paulus if (decode_proto) {
576 1.1 paulus state->strm.avail_out = ospace - PPP_HDRLEN;
577 1.1 paulus if ((wptr[3] & 1) == 0) {
578 1.1 paulus /* 2-byte protocol field */
579 1.1 paulus wptr[2] = wptr[3];
580 1.1 paulus --state->strm.next_out;
581 1.1 paulus ++state->strm.avail_out;
582 1.1 paulus --olen;
583 1.1 paulus }
584 1.1 paulus decode_proto = 0;
585 1.1 paulus } else {
586 1.1 paulus mo->m_len = ospace;
587 1.1 paulus olen += ospace;
588 1.1 paulus MGET(mo->m_next, M_DONTWAIT, MT_DATA);
589 1.1 paulus mo = mo->m_next;
590 1.1 paulus if (mo == NULL) {
591 1.1 paulus m_freem(mo_head);
592 1.1 paulus return DECOMP_ERROR;
593 1.1 paulus }
594 1.1 paulus MCLGET(mo, M_DONTWAIT);
595 1.1 paulus state->strm.next_out = mtod(mo, u_char *);
596 1.1 paulus state->strm.avail_out = ospace = M_TRAILINGSPACE(mo);
597 1.1 paulus }
598 1.1 paulus }
599 1.1 paulus }
600 1.1 paulus if (decode_proto) {
601 1.1 paulus m_freem(mo_head);
602 1.1 paulus return DECOMP_ERROR;
603 1.1 paulus }
604 1.1 paulus olen += (mo->m_len = ospace - state->strm.avail_out);
605 1.4 christos #if DEFLATE_DEBUG
606 1.4 christos if (olen > state->mru + PPP_HDRLEN)
607 1.4 christos printf("ppp_deflate%d: exceeded mru (%d > %d)\n",
608 1.4 christos state->unit, olen, state->mru + PPP_HDRLEN);
609 1.4 christos #endif
610 1.1 paulus
611 1.1 paulus state->stats.unc_bytes += olen;
612 1.1 paulus state->stats.unc_packets++;
613 1.1 paulus state->stats.comp_bytes += rlen;
614 1.1 paulus state->stats.comp_packets++;
615 1.1 paulus
616 1.1 paulus *mop = mo_head;
617 1.1 paulus return DECOMP_OK;
618 1.1 paulus }
619 1.1 paulus
620 1.1 paulus /*
621 1.1 paulus * Incompressible data has arrived - add it to the history.
622 1.1 paulus */
623 1.1 paulus static void
624 1.1 paulus z_incomp(arg, mi)
625 1.1 paulus void *arg;
626 1.1 paulus struct mbuf *mi;
627 1.1 paulus {
628 1.1 paulus struct deflate_state *state = (struct deflate_state *) arg;
629 1.1 paulus u_char *rptr;
630 1.1 paulus int rlen, proto, r;
631 1.1 paulus
632 1.1 paulus /*
633 1.1 paulus * Check that the protocol is one we handle.
634 1.1 paulus */
635 1.1 paulus rptr = mtod(mi, u_char *);
636 1.1 paulus proto = PPP_PROTOCOL(rptr);
637 1.1 paulus if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
638 1.1 paulus return;
639 1.1 paulus
640 1.1 paulus ++state->seqno;
641 1.1 paulus
642 1.1 paulus /*
643 1.1 paulus * Iterate through the mbufs, adding the characters in them
644 1.1 paulus * to the decompressor's history. For the first mbuf, we start
645 1.1 paulus * at the either the 1st or 2nd byte of the protocol field,
646 1.1 paulus * depending on whether the protocol value is compressible.
647 1.1 paulus */
648 1.1 paulus rlen = mi->m_len;
649 1.1 paulus state->strm.next_in = rptr + 3;
650 1.1 paulus state->strm.avail_in = rlen - 3;
651 1.1 paulus if (proto > 0xff) {
652 1.1 paulus --state->strm.next_in;
653 1.1 paulus ++state->strm.avail_in;
654 1.1 paulus }
655 1.1 paulus for (;;) {
656 1.1 paulus r = inflateIncomp(&state->strm);
657 1.1 paulus if (r != Z_OK) {
658 1.1 paulus /* gak! */
659 1.4 christos #if !DEFLATE_DEBUG
660 1.4 christos if (state->debug)
661 1.4 christos #endif
662 1.3 christos printf("z_incomp%d: inflateIncomp returned %d (%s)\n",
663 1.4 christos state->unit, r, (state->strm.msg? state->strm.msg: ""));
664 1.1 paulus return;
665 1.1 paulus }
666 1.1 paulus mi = mi->m_next;
667 1.1 paulus if (mi == NULL)
668 1.1 paulus break;
669 1.1 paulus state->strm.next_in = mtod(mi, u_char *);
670 1.1 paulus state->strm.avail_in = mi->m_len;
671 1.1 paulus rlen += mi->m_len;
672 1.1 paulus }
673 1.1 paulus
674 1.1 paulus /*
675 1.1 paulus * Update stats.
676 1.1 paulus */
677 1.1 paulus state->stats.inc_bytes += rlen;
678 1.1 paulus state->stats.inc_packets++;
679 1.1 paulus state->stats.unc_bytes += rlen;
680 1.1 paulus state->stats.unc_packets++;
681 1.1 paulus }
682 1.1 paulus
683 1.1 paulus #endif /* DO_DEFLATE */
684