^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * ppp_deflate.c - interface the zlib procedures for Deflate compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * and decompression (as used by gzip) to the PPP code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 1994-1998 Paul Mackerras.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/ppp_defs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ppp-comp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/zlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * State for a Deflate (de)compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct ppp_deflate_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int mru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) z_stream strm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct compstat stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define DEFLATE_OVHD 2 /* Deflate overhead/packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static void *z_comp_alloc(unsigned char *options, int opt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static void *z_decomp_alloc(unsigned char *options, int opt_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static void z_comp_free(void *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static void z_decomp_free(void *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static int z_comp_init(void *state, unsigned char *options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int opt_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int unit, int hdrlen, int debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int z_decomp_init(void *state, unsigned char *options,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int opt_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int unit, int hdrlen, int mru, int debug);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) static int z_compress(void *state, unsigned char *rptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned char *obuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int isize, int osize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static void z_incomp(void *state, unsigned char *ibuf, int icnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int z_decompress(void *state, unsigned char *ibuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int isize, unsigned char *obuf, int osize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static void z_comp_reset(void *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void z_decomp_reset(void *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static void z_comp_stats(void *state, struct compstat *stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * z_comp_free - free the memory used by a compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * @arg: pointer to the private state for the compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static void z_comp_free(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) zlib_deflateEnd(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) vfree(state->strm.workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) kfree(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * z_comp_alloc - allocate space for a compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * @options: pointer to CCP option data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @opt_len: length of the CCP option at @options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * The @options pointer points to the a buffer containing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * CCP option data for the compression being negotiated. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * formatted according to RFC1979, and describes the window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * size that the peer is requesting that we use in compressing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * data to be sent to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Returns the pointer to the private state for the compressor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * or NULL if we could not allocate enough memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static void *z_comp_alloc(unsigned char *options, int opt_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct ppp_deflate_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (opt_len != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) options[1] != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) options[3] != DEFLATE_CHK_SEQUENCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) w_size = DEFLATE_SIZE(options[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) state = kzalloc(sizeof(*state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (state == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) state->strm.next_in = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) state->w_size = w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) state->strm.workspace = vmalloc(zlib_deflate_workspacesize(-w_size, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (state->strm.workspace == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (zlib_deflateInit2(&state->strm, Z_DEFAULT_COMPRESSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) DEFLATE_METHOD_VAL, -w_size, 8, Z_DEFAULT_STRATEGY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) != Z_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return (void *) state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) z_comp_free(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * z_comp_init - initialize a previously-allocated compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * @arg: pointer to the private state for the compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * @options: pointer to the CCP option data describing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * compression that was negotiated with the peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * @opt_len: length of the CCP option data at @options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @unit: PPP unit number for diagnostic messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @hdrlen: ignored (present for backwards compatibility)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @debug: debug flag; if non-zero, debug messages are printed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * The CCP options described by @options must match the options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * specified when the compressor was allocated. The compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * history is reset. Returns 0 for failure (CCP options don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * match) or 1 for success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int z_comp_init(void *arg, unsigned char *options, int opt_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int unit, int hdrlen, int debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (opt_len < CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) options[1] != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) DEFLATE_SIZE(options[2]) != state->w_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) options[3] != DEFLATE_CHK_SEQUENCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) state->seqno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) state->unit = unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) state->debug = debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) zlib_deflateReset(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * z_comp_reset - reset a previously-allocated compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @arg: pointer to private state for the compressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * This clears the history for the compressor and makes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * ready to start emitting a new compressed stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void z_comp_reset(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) state->seqno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) zlib_deflateReset(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * z_compress - compress a PPP packet with Deflate compression.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @arg: pointer to private state for the compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @rptr: uncompressed packet (input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * @obuf: compressed packet (output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @isize: size of uncompressed packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @osize: space available at @obuf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Returns the length of the compressed packet, or 0 if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * packet is incompressible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int z_compress(void *arg, unsigned char *rptr, unsigned char *obuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) int isize, int osize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) int r, proto, off, olen, oavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) unsigned char *wptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * Check that the protocol is in the range we handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) proto = PPP_PROTOCOL(rptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /* Don't generate compressed packets which are larger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) the uncompressed packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (osize > isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) osize = isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) wptr = obuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * Copy over the PPP header and store the 2-byte sequence number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) wptr[0] = PPP_ADDRESS(rptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) wptr[1] = PPP_CONTROL(rptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) put_unaligned_be16(PPP_COMP, wptr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) wptr += PPP_HDRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) put_unaligned_be16(state->seqno, wptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) wptr += DEFLATE_OVHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) olen = PPP_HDRLEN + DEFLATE_OVHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) state->strm.next_out = wptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) state->strm.avail_out = oavail = osize - olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) ++state->seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) off = (proto > 0xff) ? 2 : 3; /* skip 1st proto byte if 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) rptr += off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) state->strm.next_in = rptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) state->strm.avail_in = (isize - off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) r = zlib_deflate(&state->strm, Z_PACKET_FLUSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (r != Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) "z_compress: deflate returned %d\n", r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (state->strm.avail_out == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) olen += oavail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) state->strm.next_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) state->strm.avail_out = oavail = 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break; /* all done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) olen += oavail - state->strm.avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * See if we managed to reduce the size of the packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (olen < isize && olen <= osize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) state->stats.comp_bytes += olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) state->stats.comp_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) state->stats.inc_bytes += isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) state->stats.inc_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) olen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) state->stats.unc_bytes += isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) state->stats.unc_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * z_comp_stats - return compression statistics for a compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * or decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * @arg: pointer to private space for the (de)compressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) * @stats: pointer to a struct compstat to receive the result.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static void z_comp_stats(void *arg, struct compstat *stats)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) *stats = state->stats;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * z_decomp_free - Free the memory used by a decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * @arg: pointer to private space for the decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static void z_decomp_free(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) zlib_inflateEnd(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) vfree(state->strm.workspace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) kfree(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * z_decomp_alloc - allocate space for a decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * @options: pointer to CCP option data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * @opt_len: length of the CCP option at @options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * The @options pointer points to the a buffer containing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * CCP option data for the compression being negotiated. It is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * formatted according to RFC1979, and describes the window
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * size that we are requesting the peer to use in compressing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * data to be sent to us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * Returns the pointer to the private state for the decompressor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * or NULL if we could not allocate enough memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static void *z_decomp_alloc(unsigned char *options, int opt_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct ppp_deflate_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (opt_len != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) options[1] != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) options[3] != DEFLATE_CHK_SEQUENCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) w_size = DEFLATE_SIZE(options[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (w_size < DEFLATE_MIN_SIZE || w_size > DEFLATE_MAX_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) state = kzalloc(sizeof(*state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (state == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) state->w_size = w_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) state->strm.next_out = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) state->strm.workspace = vmalloc(zlib_inflate_workspacesize());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (state->strm.workspace == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (zlib_inflateInit2(&state->strm, -w_size) != Z_OK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return (void *) state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) z_decomp_free(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * z_decomp_init - initialize a previously-allocated decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * @arg: pointer to the private state for the decompressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * @options: pointer to the CCP option data describing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * compression that was negotiated with the peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * @opt_len: length of the CCP option data at @options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * @unit: PPP unit number for diagnostic messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * @hdrlen: ignored (present for backwards compatibility)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * @mru: maximum length of decompressed packets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @debug: debug flag; if non-zero, debug messages are printed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * The CCP options described by @options must match the options
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) * specified when the decompressor was allocated. The decompressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * history is reset. Returns 0 for failure (CCP options don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) * match) or 1 for success.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int z_decomp_init(void *arg, unsigned char *options, int opt_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) int unit, int hdrlen, int mru, int debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (opt_len < CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) (options[0] != CI_DEFLATE && options[0] != CI_DEFLATE_DRAFT) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) options[1] != CILEN_DEFLATE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) DEFLATE_METHOD(options[2]) != DEFLATE_METHOD_VAL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) DEFLATE_SIZE(options[2]) != state->w_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) options[3] != DEFLATE_CHK_SEQUENCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) state->seqno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) state->unit = unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) state->debug = debug;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) state->mru = mru;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) zlib_inflateReset(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) * z_decomp_reset - reset a previously-allocated decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * @arg: pointer to private state for the decompressor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) * This clears the history for the decompressor and makes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) * ready to receive a new compressed stream.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) static void z_decomp_reset(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) state->seqno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) zlib_inflateReset(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * z_decompress - decompress a Deflate-compressed packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) * @arg: pointer to private state for the decompressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * @ibuf: pointer to input (compressed) packet data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * @isize: length of input packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * @obuf: pointer to space for output (decompressed) packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @osize: amount of space available at @obuf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * Because of patent problems, we return DECOMP_ERROR for errors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * found by inspecting the input data and for system problems, but
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * DECOMP_FATALERROR for any errors which could possibly be said to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * be being detected "after" decompression. For DECOMP_ERROR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) * we can issue a CCP reset-request; for DECOMP_FATALERROR, we may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * infringing a patent of Motorola's if we do, so we take CCP down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) * instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * Given that the frame has the correct sequence number and a good FCS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * errors such as invalid codes in the input most likely indicate a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * bug, so we return DECOMP_FATALERROR for them in order to turn off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * compression, even though they are detected by inspecting the input.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int z_decompress(void *arg, unsigned char *ibuf, int isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) unsigned char *obuf, int osize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int olen, seq, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int decode_proto, overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) unsigned char overflow_buf[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (isize <= PPP_HDRLEN + DEFLATE_OVHD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) printk(KERN_DEBUG "z_decompress%d: short pkt (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) state->unit, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) return DECOMP_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /* Check the sequence number. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) seq = get_unaligned_be16(ibuf + PPP_HDRLEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) if (seq != (state->seqno & 0xffff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) printk(KERN_DEBUG "z_decompress%d: bad seq # %d, expected %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) state->unit, seq, state->seqno & 0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return DECOMP_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) ++state->seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Fill in the first part of the PPP header. The protocol field
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * comes from the decompressed data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) obuf[0] = PPP_ADDRESS(ibuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) obuf[1] = PPP_CONTROL(ibuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) obuf[2] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) * Set up to call inflate. We set avail_out to 1 initially so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) * look at the first byte of the output and decide whether we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) * a 1-byte or 2-byte protocol field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) state->strm.next_in = ibuf + PPP_HDRLEN + DEFLATE_OVHD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) state->strm.avail_in = isize - (PPP_HDRLEN + DEFLATE_OVHD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) state->strm.next_out = obuf + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) state->strm.avail_out = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) decode_proto = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) overflow = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * Call inflate, supplying more input or output as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) r = zlib_inflate(&state->strm, Z_PACKET_FLUSH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) if (r != Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) printk(KERN_DEBUG "z_decompress%d: inflate returned %d (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) state->unit, r, (state->strm.msg? state->strm.msg: ""));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return DECOMP_FATALERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) if (state->strm.avail_out != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) break; /* all done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) if (decode_proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) state->strm.avail_out = osize - PPP_HDRLEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) if ((obuf[3] & 1) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) /* 2-byte protocol field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) obuf[2] = obuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) --state->strm.next_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ++state->strm.avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) decode_proto = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) } else if (!overflow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * We've filled up the output buffer; the only way to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * find out whether inflate has any more characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * left is to give it another byte of output space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) state->strm.next_out = overflow_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) state->strm.avail_out = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) overflow = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) printk(KERN_DEBUG "z_decompress%d: ran out of mru\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) state->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return DECOMP_FATALERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (decode_proto) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (state->debug)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) printk(KERN_DEBUG "z_decompress%d: didn't get proto\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) state->unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return DECOMP_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) olen = osize + overflow - state->strm.avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) state->stats.unc_bytes += olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) state->stats.unc_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) state->stats.comp_bytes += isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) state->stats.comp_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) return olen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) * z_incomp - add incompressible input data to the history.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * @arg: pointer to private state for the decompressor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) * @ibuf: pointer to input packet data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) * @icnt: length of input data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static void z_incomp(void *arg, unsigned char *ibuf, int icnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) struct ppp_deflate_state *state = (struct ppp_deflate_state *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) int proto, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) * Check that the protocol is one we handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) proto = PPP_PROTOCOL(ibuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (proto > 0x3fff || proto == 0xfd || proto == 0xfb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ++state->seqno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * We start at the either the 1st or 2nd byte of the protocol field,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) * depending on whether the protocol value is compressible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) state->strm.next_in = ibuf + 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) state->strm.avail_in = icnt - 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (proto > 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) --state->strm.next_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ++state->strm.avail_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) r = zlib_inflateIncomp(&state->strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) if (r != Z_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) /* gak! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) if (state->debug) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) printk(KERN_DEBUG "z_incomp%d: inflateIncomp returned %d (%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) state->unit, r, (state->strm.msg? state->strm.msg: ""));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) * Update stats.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) state->stats.inc_bytes += icnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) state->stats.inc_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) state->stats.unc_bytes += icnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) state->stats.unc_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /*************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * Module interface table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) *************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) /* These are in ppp_generic.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) extern int ppp_register_compressor (struct compressor *cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) extern void ppp_unregister_compressor (struct compressor *cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * Procedures exported to if_ppp.c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) static struct compressor ppp_deflate = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) .compress_proto = CI_DEFLATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) .comp_alloc = z_comp_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) .comp_free = z_comp_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) .comp_init = z_comp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) .comp_reset = z_comp_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) .compress = z_compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) .comp_stat = z_comp_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) .decomp_alloc = z_decomp_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) .decomp_free = z_decomp_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) .decomp_init = z_decomp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) .decomp_reset = z_decomp_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) .decompress = z_decompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) .incomp = z_incomp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) .decomp_stat = z_comp_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) .owner = THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) static struct compressor ppp_deflate_draft = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) .compress_proto = CI_DEFLATE_DRAFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) .comp_alloc = z_comp_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) .comp_free = z_comp_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) .comp_init = z_comp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) .comp_reset = z_comp_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) .compress = z_compress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) .comp_stat = z_comp_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) .decomp_alloc = z_decomp_alloc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) .decomp_free = z_decomp_free,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) .decomp_init = z_decomp_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) .decomp_reset = z_decomp_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) .decompress = z_decompress,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) .incomp = z_incomp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) .decomp_stat = z_comp_stats,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) .owner = THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static int __init deflate_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) rc = ppp_register_compressor(&ppp_deflate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) rc = ppp_register_compressor(&ppp_deflate_draft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) ppp_unregister_compressor(&ppp_deflate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) pr_info("PPP Deflate Compression module registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) static void __exit deflate_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) ppp_unregister_compressor(&ppp_deflate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) ppp_unregister_compressor(&ppp_deflate_draft);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) module_init(deflate_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) module_exit(deflate_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) MODULE_LICENSE("Dual BSD/GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) MODULE_ALIAS("ppp-compress-" __stringify(CI_DEFLATE_DRAFT));