Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BSD compression module |
| 3 | * |
| 4 | * Patched version for ISDN syncPPP written 1997/1998 by Michael Hipp |
| 5 | * The whole module is now SKB based. |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /* |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 10 | * Update: The Berkeley copyright was changed, and the change |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * is retroactive to all "true" BSD software (ie everything |
| 12 | * from UCB as opposed to other peoples code that just carried |
| 13 | * the same license). The new copyright doesn't clash with the |
| 14 | * GPL, so the module-only restriction has been removed.. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Original copyright notice: |
| 19 | * |
| 20 | * Copyright (c) 1985, 1986 The Regents of the University of California. |
| 21 | * All rights reserved. |
| 22 | * |
| 23 | * This code is derived from software contributed to Berkeley by |
| 24 | * James A. Woods, derived from original work by Spencer Thomas |
| 25 | * and Joseph Orost. |
| 26 | * |
| 27 | * Redistribution and use in source and binary forms, with or without |
| 28 | * modification, are permitted provided that the following conditions |
| 29 | * are met: |
| 30 | * 1. Redistributions of source code must retain the above copyright |
| 31 | * notice, this list of conditions and the following disclaimer. |
| 32 | * 2. Redistributions in binary form must reproduce the above copyright |
| 33 | * notice, this list of conditions and the following disclaimer in the |
| 34 | * documentation and/or other materials provided with the distribution. |
| 35 | * 3. All advertising materials mentioning features or use of this software |
| 36 | * must display the following acknowledgement: |
| 37 | * This product includes software developed by the University of |
| 38 | * California, Berkeley and its contributors. |
| 39 | * 4. Neither the name of the University nor the names of its contributors |
| 40 | * may be used to endorse or promote products derived from this software |
| 41 | * without specific prior written permission. |
| 42 | * |
| 43 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 44 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 45 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 46 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 47 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 48 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 49 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 50 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 51 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 52 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 53 | * SUCH DAMAGE. |
| 54 | */ |
| 55 | |
| 56 | #include <linux/module.h> |
| 57 | #include <linux/init.h> |
| 58 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <linux/types.h> |
| 60 | #include <linux/fcntl.h> |
| 61 | #include <linux/interrupt.h> |
| 62 | #include <linux/ptrace.h> |
| 63 | #include <linux/ioport.h> |
| 64 | #include <linux/in.h> |
| 65 | #include <linux/slab.h> |
| 66 | #include <linux/tty.h> |
| 67 | #include <linux/errno.h> |
| 68 | #include <linux/string.h> /* used in new tty drivers */ |
| 69 | #include <linux/signal.h> /* used in new tty drivers */ |
| 70 | #include <linux/bitops.h> |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | #include <asm/byteorder.h> |
| 73 | #include <asm/types.h> |
| 74 | |
| 75 | #include <linux/if.h> |
| 76 | |
| 77 | #include <linux/if_ether.h> |
| 78 | #include <linux/netdevice.h> |
| 79 | #include <linux/skbuff.h> |
| 80 | #include <linux/inet.h> |
| 81 | #include <linux/ioctl.h> |
| 82 | #include <linux/vmalloc.h> |
| 83 | |
| 84 | #include <linux/ppp_defs.h> |
| 85 | |
| 86 | #include <linux/isdn.h> |
| 87 | #include <linux/isdn_ppp.h> |
| 88 | #include <linux/ip.h> |
| 89 | #include <linux/tcp.h> |
| 90 | #include <linux/if_arp.h> |
| 91 | #include <linux/ppp-comp.h> |
| 92 | |
| 93 | #include "isdn_ppp.h" |
| 94 | |
| 95 | MODULE_DESCRIPTION("ISDN4Linux: BSD Compression for PPP over ISDN"); |
| 96 | MODULE_LICENSE("Dual BSD/GPL"); |
| 97 | |
| 98 | #define BSD_VERSION(x) ((x) >> 5) |
| 99 | #define BSD_NBITS(x) ((x) & 0x1F) |
| 100 | |
| 101 | #define BSD_CURRENT_VERSION 1 |
| 102 | |
| 103 | #define DEBUG 1 |
| 104 | |
| 105 | /* |
| 106 | * A dictionary for doing BSD compress. |
| 107 | */ |
| 108 | |
| 109 | struct bsd_dict { |
| 110 | u32 fcode; |
| 111 | u16 codem1; /* output of hash table -1 */ |
| 112 | u16 cptr; /* map code to hash table entry */ |
| 113 | }; |
| 114 | |
| 115 | struct bsd_db { |
| 116 | int totlen; /* length of this structure */ |
| 117 | unsigned int hsize; /* size of the hash table */ |
| 118 | unsigned char hshift; /* used in hash function */ |
| 119 | unsigned char n_bits; /* current bits/code */ |
| 120 | unsigned char maxbits; /* maximum bits/code */ |
| 121 | unsigned char debug; /* non-zero if debug desired */ |
| 122 | unsigned char unit; /* ppp unit number */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 123 | u16 seqno; /* sequence # of next packet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | unsigned int mru; /* size of receive (decompress) bufr */ |
| 125 | unsigned int maxmaxcode; /* largest valid code */ |
| 126 | unsigned int max_ent; /* largest code in use */ |
| 127 | unsigned int in_count; /* uncompressed bytes, aged */ |
| 128 | unsigned int bytes_out; /* compressed bytes, aged */ |
| 129 | unsigned int ratio; /* recent compression ratio */ |
| 130 | unsigned int checkpoint; /* when to next check the ratio */ |
| 131 | unsigned int clear_count; /* times dictionary cleared */ |
| 132 | unsigned int incomp_count; /* incompressible packets */ |
| 133 | unsigned int incomp_bytes; /* incompressible bytes */ |
| 134 | unsigned int uncomp_count; /* uncompressed packets */ |
| 135 | unsigned int uncomp_bytes; /* uncompressed bytes */ |
| 136 | unsigned int comp_count; /* compressed packets */ |
| 137 | unsigned int comp_bytes; /* compressed bytes */ |
| 138 | unsigned short *lens; /* array of lengths of codes */ |
| 139 | struct bsd_dict *dict; /* dictionary */ |
| 140 | int xmit; |
| 141 | }; |
| 142 | |
| 143 | #define BSD_OVHD 2 /* BSD compress overhead/packet */ |
| 144 | #define MIN_BSD_BITS 9 |
| 145 | #define BSD_INIT_BITS MIN_BSD_BITS |
| 146 | #define MAX_BSD_BITS 15 |
| 147 | |
| 148 | /* |
| 149 | * the next two codes should not be changed lightly, as they must not |
| 150 | * lie within the contiguous general code space. |
| 151 | */ |
| 152 | #define CLEAR 256 /* table clear output code */ |
| 153 | #define FIRST 257 /* first free entry */ |
| 154 | #define LAST 255 |
| 155 | |
| 156 | #define MAXCODE(b) ((1 << (b)) - 1) |
Phil Carmody | 497888c | 2011-07-14 15:07:13 +0300 | [diff] [blame] | 157 | #define BADCODEM1 MAXCODE(MAX_BSD_BITS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 159 | #define BSD_HASH(prefix, suffix, hshift) ((((unsigned long)(suffix)) << (hshift)) \ |
| 160 | ^ (unsigned long)(prefix)) |
| 161 | #define BSD_KEY(prefix, suffix) ((((unsigned long)(suffix)) << 16) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | + (unsigned long)(prefix)) |
| 163 | |
| 164 | #define CHECK_GAP 10000 /* Ratio check interval */ |
| 165 | |
| 166 | #define RATIO_SCALE_LOG 8 |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 167 | #define RATIO_SCALE (1 << RATIO_SCALE_LOG) |
| 168 | #define RATIO_MAX (0x7fffffff >> RATIO_SCALE_LOG) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * clear the dictionary |
| 172 | */ |
| 173 | |
| 174 | static void bsd_clear(struct bsd_db *db) |
| 175 | { |
| 176 | db->clear_count++; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 177 | db->max_ent = FIRST - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | db->n_bits = BSD_INIT_BITS; |
| 179 | db->bytes_out = 0; |
| 180 | db->in_count = 0; |
| 181 | db->incomp_count = 0; |
| 182 | db->ratio = 0; |
| 183 | db->checkpoint = CHECK_GAP; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * If the dictionary is full, then see if it is time to reset it. |
| 188 | * |
| 189 | * Compute the compression ratio using fixed-point arithmetic |
| 190 | * with 8 fractional bits. |
| 191 | * |
| 192 | * Since we have an infinite stream instead of a single file, |
| 193 | * watch only the local compression ratio. |
| 194 | * |
| 195 | * Since both peers must reset the dictionary at the same time even in |
| 196 | * the absence of CLEAR codes (while packets are incompressible), they |
| 197 | * must compute the same ratio. |
| 198 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 199 | static int bsd_check(struct bsd_db *db) /* 1=output CLEAR */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 201 | unsigned int new_ratio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 203 | if (db->in_count >= db->checkpoint) |
| 204 | { |
| 205 | /* age the ratio by limiting the size of the counts */ |
| 206 | if (db->in_count >= RATIO_MAX || db->bytes_out >= RATIO_MAX) |
| 207 | { |
| 208 | db->in_count -= (db->in_count >> 2); |
| 209 | db->bytes_out -= (db->bytes_out >> 2); |
| 210 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 212 | db->checkpoint = db->in_count + CHECK_GAP; |
| 213 | |
| 214 | if (db->max_ent >= db->maxmaxcode) |
| 215 | { |
| 216 | /* Reset the dictionary only if the ratio is worse, |
| 217 | * or if it looks as if it has been poisoned |
| 218 | * by incompressible data. |
| 219 | * |
| 220 | * This does not overflow, because |
| 221 | * db->in_count <= RATIO_MAX. |
| 222 | */ |
| 223 | |
| 224 | new_ratio = db->in_count << RATIO_SCALE_LOG; |
| 225 | if (db->bytes_out != 0) |
| 226 | { |
| 227 | new_ratio /= db->bytes_out; |
| 228 | } |
| 229 | |
| 230 | if (new_ratio < db->ratio || new_ratio < 1 * RATIO_SCALE) |
| 231 | { |
| 232 | bsd_clear(db); |
| 233 | return 1; |
| 234 | } |
| 235 | db->ratio = new_ratio; |
| 236 | } |
| 237 | } |
| 238 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Return statistics. |
| 243 | */ |
| 244 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 245 | static void bsd_stats(void *state, struct compstat *stats) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | { |
| 247 | struct bsd_db *db = (struct bsd_db *) state; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | stats->unc_bytes = db->uncomp_bytes; |
| 250 | stats->unc_packets = db->uncomp_count; |
| 251 | stats->comp_bytes = db->comp_bytes; |
| 252 | stats->comp_packets = db->comp_count; |
| 253 | stats->inc_bytes = db->incomp_bytes; |
| 254 | stats->inc_packets = db->incomp_count; |
| 255 | stats->in_count = db->in_count; |
| 256 | stats->bytes_out = db->bytes_out; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Reset state, as on a CCP ResetReq. |
| 261 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 262 | static void bsd_reset(void *state, unsigned char code, unsigned char id, |
| 263 | unsigned char *data, unsigned len, |
| 264 | struct isdn_ppp_resetparams *rsparm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { |
| 266 | struct bsd_db *db = (struct bsd_db *) state; |
| 267 | |
| 268 | bsd_clear(db); |
| 269 | db->seqno = 0; |
| 270 | db->clear_count = 0; |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Release the compression structure |
| 275 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 276 | static void bsd_free(void *state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
| 278 | struct bsd_db *db = (struct bsd_db *) state; |
| 279 | |
| 280 | if (db) { |
| 281 | /* |
| 282 | * Release the dictionary |
| 283 | */ |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 284 | vfree(db->dict); |
| 285 | db->dict = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * Release the string buffer |
| 289 | */ |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 290 | vfree(db->lens); |
| 291 | db->lens = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Finally release the structure itself. |
| 295 | */ |
Jesper Juhl | f9101210 | 2005-09-10 00:26:54 -0700 | [diff] [blame] | 296 | kfree(db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /* |
| 302 | * Allocate space for a (de) compressor. |
| 303 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 304 | static void *bsd_alloc(struct isdn_ppp_comp_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | int bits; |
| 307 | unsigned int hsize, hshift, maxmaxcode; |
| 308 | struct bsd_db *db; |
| 309 | int decomp; |
| 310 | |
| 311 | static unsigned int htab[][2] = { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 312 | { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , { 5003 , 4 } , |
| 313 | { 9001 , 5 } , { 18013 , 6 } , { 35023 , 7 } , { 69001 , 8 } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | }; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (data->optlen != 1 || data->num != CI_BSD_COMPRESS |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 317 | || BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | return NULL; |
| 319 | |
| 320 | bits = BSD_NBITS(data->options[0]); |
| 321 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 322 | if (bits < 9 || bits > 15) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | return NULL; |
| 324 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 325 | hsize = htab[bits - 9][0]; |
| 326 | hshift = htab[bits - 9][1]; |
| 327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | /* |
| 329 | * Allocate the main control structure for this instance. |
| 330 | */ |
| 331 | maxmaxcode = MAXCODE(bits); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 332 | db = kzalloc(sizeof(struct bsd_db), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | if (!db) |
| 334 | return NULL; |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | db->xmit = data->flags & IPPP_COMP_FLAG_XMIT; |
| 337 | decomp = db->xmit ? 0 : 1; |
| 338 | |
| 339 | /* |
| 340 | * Allocate space for the dictionary. This may be more than one page in |
| 341 | * length. |
| 342 | */ |
Jesper Juhl | 7c8347a | 2007-08-24 23:25:33 -0700 | [diff] [blame] | 343 | db->dict = vmalloc(hsize * sizeof(struct bsd_dict)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | if (!db->dict) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 345 | bsd_free(db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * If this is the compression buffer then there is no length data. |
| 351 | * For decompression, the length information is needed as well. |
| 352 | */ |
| 353 | if (!decomp) |
| 354 | db->lens = NULL; |
| 355 | else { |
Jesper Juhl | 7c8347a | 2007-08-24 23:25:33 -0700 | [diff] [blame] | 356 | db->lens = vmalloc((maxmaxcode + 1) * sizeof(db->lens[0])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | if (!db->lens) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 358 | bsd_free(db); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | return (NULL); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Initialize the data information for the compression code |
| 365 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 366 | db->totlen = sizeof(struct bsd_db) + (sizeof(struct bsd_dict) * hsize); |
| 367 | db->hsize = hsize; |
| 368 | db->hshift = hshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | db->maxmaxcode = maxmaxcode; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 370 | db->maxbits = bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 372 | return (void *)db; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Initialize the database. |
| 377 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 378 | static int bsd_init(void *state, struct isdn_ppp_comp_data *data, int unit, int debug) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { |
| 380 | struct bsd_db *db = state; |
| 381 | int indx; |
| 382 | int decomp; |
| 383 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 384 | if (!state || !data) { |
| 385 | printk(KERN_ERR "isdn_bsd_init: [%d] ERR, state %lx data %lx\n", unit, (long)state, (long)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | decomp = db->xmit ? 0 : 1; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | if (data->optlen != 1 || data->num != CI_BSD_COMPRESS |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 392 | || (BSD_VERSION(data->options[0]) != BSD_CURRENT_VERSION) |
| 393 | || (BSD_NBITS(data->options[0]) != db->maxbits) |
| 394 | || (decomp && db->lens == NULL)) { |
| 395 | printk(KERN_ERR "isdn_bsd: %d %d %d %d %lx\n", data->optlen, data->num, data->options[0], decomp, (unsigned long)db->lens); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | if (decomp) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 400 | for (indx = LAST; indx >= 0; indx--) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | db->lens[indx] = 1; |
| 402 | |
| 403 | indx = db->hsize; |
| 404 | while (indx-- != 0) { |
| 405 | db->dict[indx].codem1 = BADCODEM1; |
| 406 | db->dict[indx].cptr = 0; |
| 407 | } |
| 408 | |
| 409 | db->unit = unit; |
| 410 | db->mru = 0; |
| 411 | |
| 412 | db->debug = 1; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 413 | |
| 414 | bsd_reset(db, 0, 0, NULL, 0, NULL); |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | return 1; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Obtain pointers to the various structures in the compression tables |
| 421 | */ |
| 422 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 423 | #define dict_ptrx(p, idx) &(p->dict[idx]) |
| 424 | #define lens_ptrx(p, idx) &(p->lens[idx]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
| 426 | #ifdef DEBUG |
| 427 | static unsigned short *lens_ptr(struct bsd_db *db, int idx) |
| 428 | { |
| 429 | if ((unsigned int) idx > (unsigned int) db->maxmaxcode) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 430 | printk(KERN_DEBUG "<9>ppp: lens_ptr(%d) > max\n", idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | idx = 0; |
| 432 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 433 | return lens_ptrx(db, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | static struct bsd_dict *dict_ptr(struct bsd_db *db, int idx) |
| 437 | { |
| 438 | if ((unsigned int) idx >= (unsigned int) db->hsize) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 439 | printk(KERN_DEBUG "<9>ppp: dict_ptr(%d) > max\n", idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | idx = 0; |
| 441 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 442 | return dict_ptrx(db, idx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | #else |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 446 | #define lens_ptr(db, idx) lens_ptrx(db, idx) |
| 447 | #define dict_ptr(db, idx) dict_ptrx(db, idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | #endif |
| 449 | |
| 450 | /* |
| 451 | * compress a packet |
| 452 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 453 | static int bsd_compress(void *state, struct sk_buff *skb_in, struct sk_buff *skb_out, int proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | { |
| 455 | struct bsd_db *db; |
| 456 | int hshift; |
| 457 | unsigned int max_ent; |
| 458 | unsigned int n_bits; |
| 459 | unsigned int bitno; |
| 460 | unsigned long accm; |
| 461 | int ent; |
| 462 | unsigned long fcode; |
| 463 | struct bsd_dict *dictp; |
| 464 | unsigned char c; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 465 | int hval, disp, ilen, mxcode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | unsigned char *rptr = skb_in->data; |
| 467 | int isize = skb_in->len; |
| 468 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 469 | #define OUTPUT(ent) \ |
| 470 | { \ |
| 471 | bitno -= n_bits; \ |
| 472 | accm |= ((ent) << bitno); \ |
| 473 | do { \ |
| 474 | if (skb_out && skb_tailroom(skb_out) > 0) \ |
| 475 | *(skb_put(skb_out, 1)) = (unsigned char)(accm >> 24); \ |
| 476 | accm <<= 8; \ |
| 477 | bitno += 8; \ |
| 478 | } while (bitno <= 24); \ |
| 479 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * If the protocol is not in the range we're interested in, |
| 483 | * just return without compressing the packet. If it is, |
| 484 | * the protocol becomes the first byte to compress. |
| 485 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 486 | printk(KERN_DEBUG "bsd_compress called with %x\n", proto); |
| 487 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | ent = proto; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 489 | if (proto < 0x21 || proto > 0xf9 || !(proto & 0x1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return 0; |
| 491 | |
| 492 | db = (struct bsd_db *) state; |
| 493 | hshift = db->hshift; |
| 494 | max_ent = db->max_ent; |
| 495 | n_bits = db->n_bits; |
| 496 | bitno = 32; |
| 497 | accm = 0; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 498 | mxcode = MAXCODE(n_bits); |
| 499 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | /* This is the PPP header information */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 501 | if (skb_out && skb_tailroom(skb_out) >= 2) { |
| 502 | char *v = skb_put(skb_out, 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* we only push our own data on the header, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 504 | AC,PC and protos is pushed by caller */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | v[0] = db->seqno >> 8; |
| 506 | v[1] = db->seqno; |
| 507 | } |
| 508 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 509 | ilen = ++isize; /* This is off by one, but that is what is in draft! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
| 511 | while (--ilen > 0) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 512 | c = *rptr++; |
| 513 | fcode = BSD_KEY(ent, c); |
| 514 | hval = BSD_HASH(ent, c, hshift); |
| 515 | dictp = dict_ptr(db, hval); |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | /* Validate and then check the entry. */ |
| 518 | if (dictp->codem1 >= max_ent) |
| 519 | goto nomatch; |
| 520 | |
| 521 | if (dictp->fcode == fcode) { |
| 522 | ent = dictp->codem1 + 1; |
| 523 | continue; /* found (prefix,suffix) */ |
| 524 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | /* continue probing until a match or invalid entry */ |
| 527 | disp = (hval == 0) ? 1 : hval; |
| 528 | |
| 529 | do { |
| 530 | hval += disp; |
| 531 | if (hval >= db->hsize) |
| 532 | hval -= db->hsize; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 533 | dictp = dict_ptr(db, hval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | if (dictp->codem1 >= max_ent) |
| 535 | goto nomatch; |
| 536 | } while (dictp->fcode != fcode); |
| 537 | |
| 538 | ent = dictp->codem1 + 1; /* finally found (prefix,suffix) */ |
| 539 | continue; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 540 | |
| 541 | nomatch: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | OUTPUT(ent); /* output the prefix */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 543 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | /* code -> hashtable */ |
| 545 | if (max_ent < db->maxmaxcode) { |
| 546 | struct bsd_dict *dictp2; |
| 547 | struct bsd_dict *dictp3; |
| 548 | int indx; |
| 549 | |
| 550 | /* expand code size if needed */ |
| 551 | if (max_ent >= mxcode) { |
| 552 | db->n_bits = ++n_bits; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 553 | mxcode = MAXCODE(n_bits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 555 | |
| 556 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | * Invalidate old hash table entry using |
| 558 | * this code, and then take it over. |
| 559 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 560 | dictp2 = dict_ptr(db, max_ent + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | indx = dictp2->cptr; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 562 | dictp3 = dict_ptr(db, indx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | if (dictp3->codem1 == max_ent) |
| 565 | dictp3->codem1 = BADCODEM1; |
| 566 | |
| 567 | dictp2->cptr = hval; |
| 568 | dictp->codem1 = max_ent; |
| 569 | dictp->fcode = fcode; |
| 570 | db->max_ent = ++max_ent; |
| 571 | |
| 572 | if (db->lens) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 573 | unsigned short *len1 = lens_ptr(db, max_ent); |
| 574 | unsigned short *len2 = lens_ptr(db, ent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | *len1 = *len2 + 1; |
| 576 | } |
| 577 | } |
| 578 | ent = c; |
| 579 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | OUTPUT(ent); /* output the last code */ |
| 582 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 583 | if (skb_out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | db->bytes_out += skb_out->len; /* Do not count bytes from here */ |
| 585 | db->uncomp_bytes += isize; |
| 586 | db->in_count += isize; |
| 587 | ++db->uncomp_count; |
| 588 | ++db->seqno; |
| 589 | |
| 590 | if (bitno < 32) |
| 591 | ++db->bytes_out; /* must be set before calling bsd_check */ |
| 592 | |
| 593 | /* |
| 594 | * Generate the clear command if needed |
| 595 | */ |
| 596 | |
| 597 | if (bsd_check(db)) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 598 | OUTPUT(CLEAR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | * Pad dribble bits of last code with ones. |
| 602 | * Do not emit a completely useless byte of ones. |
| 603 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 604 | if (bitno < 32 && skb_out && skb_tailroom(skb_out) > 0) |
| 605 | *(skb_put(skb_out, 1)) = (unsigned char)((accm | (0xff << (bitno - 8))) >> 24); |
| 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | /* |
| 608 | * Increase code size if we would have without the packet |
| 609 | * boundary because the decompressor will do so. |
| 610 | */ |
| 611 | if (max_ent >= mxcode && max_ent < db->maxmaxcode) |
| 612 | db->n_bits++; |
| 613 | |
| 614 | /* If output length is too large then this is an incompressible frame. */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 615 | if (!skb_out || (skb_out && skb_out->len >= skb_in->len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | ++db->incomp_count; |
| 617 | db->incomp_bytes += isize; |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | /* Count the number of compressed frames */ |
| 622 | ++db->comp_count; |
| 623 | db->comp_bytes += skb_out->len; |
| 624 | return skb_out->len; |
| 625 | |
| 626 | #undef OUTPUT |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * Update the "BSD Compress" dictionary on the receiver for |
| 631 | * incompressible data by pretending to compress the incoming data. |
| 632 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 633 | static void bsd_incomp(void *state, struct sk_buff *skb_in, int proto) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 635 | bsd_compress(state, skb_in, NULL, proto); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | /* |
| 639 | * Decompress "BSD Compress". |
| 640 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 641 | static int bsd_decompress(void *state, struct sk_buff *skb_in, struct sk_buff *skb_out, |
| 642 | struct isdn_ppp_resetparams *rsparm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | { |
| 644 | struct bsd_db *db; |
| 645 | unsigned int max_ent; |
| 646 | unsigned long accm; |
| 647 | unsigned int bitno; /* 1st valid bit in accm */ |
| 648 | unsigned int n_bits; |
| 649 | unsigned int tgtbitno; /* bitno when we have a code */ |
| 650 | struct bsd_dict *dictp; |
| 651 | int seq; |
| 652 | unsigned int incode; |
| 653 | unsigned int oldcode; |
| 654 | unsigned int finchar; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 655 | unsigned char *p, *ibuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | int ilen; |
| 657 | int codelen; |
| 658 | int extra; |
| 659 | |
| 660 | db = (struct bsd_db *) state; |
| 661 | max_ent = db->max_ent; |
| 662 | accm = 0; |
| 663 | bitno = 32; /* 1st valid bit in accm */ |
| 664 | n_bits = db->n_bits; |
| 665 | tgtbitno = 32 - n_bits; /* bitno when we have a code */ |
| 666 | |
| 667 | printk(KERN_DEBUG "bsd_decompress called\n"); |
| 668 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 669 | if (!skb_in || !skb_out) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | printk(KERN_ERR "bsd_decompress called with NULL parameter\n"); |
| 671 | return DECOMP_ERROR; |
| 672 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /* |
| 675 | * Get the sequence number. |
| 676 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 677 | if ((p = skb_pull(skb_in, 2)) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return DECOMP_ERROR; |
| 679 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 680 | p -= 2; |
| 681 | seq = (p[0] << 8) + p[1]; |
| 682 | ilen = skb_in->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | ibuf = skb_in->data; |
| 684 | |
| 685 | /* |
| 686 | * Check the sequence number and give up if it differs from |
| 687 | * the value we're expecting. |
| 688 | */ |
| 689 | if (seq != db->seqno) { |
| 690 | if (db->debug) { |
| 691 | printk(KERN_DEBUG "bsd_decomp%d: bad sequence # %d, expected %d\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 692 | db->unit, seq, db->seqno - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | return DECOMP_ERROR; |
| 695 | } |
| 696 | |
| 697 | ++db->seqno; |
| 698 | db->bytes_out += ilen; |
| 699 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 700 | if (skb_tailroom(skb_out) > 0) |
| 701 | *(skb_put(skb_out, 1)) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | else |
| 703 | return DECOMP_ERR_NOMEM; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | oldcode = CLEAR; |
| 706 | |
| 707 | /* |
| 708 | * Keep the checkpoint correctly so that incompressible packets |
| 709 | * clear the dictionary at the proper times. |
| 710 | */ |
| 711 | |
| 712 | for (;;) { |
| 713 | if (ilen-- <= 0) { |
| 714 | db->in_count += (skb_out->len - 1); /* don't count the header */ |
| 715 | break; |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * Accumulate bytes until we have a complete code. |
| 720 | * Then get the next code, relying on the 32-bit, |
| 721 | * unsigned accm to mask the result. |
| 722 | */ |
| 723 | |
| 724 | bitno -= 8; |
| 725 | accm |= *ibuf++ << bitno; |
| 726 | if (tgtbitno < bitno) |
| 727 | continue; |
| 728 | |
| 729 | incode = accm >> tgtbitno; |
| 730 | accm <<= n_bits; |
| 731 | bitno += n_bits; |
| 732 | |
| 733 | /* |
| 734 | * The dictionary must only be cleared at the end of a packet. |
| 735 | */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | if (incode == CLEAR) { |
| 738 | if (ilen > 0) { |
| 739 | if (db->debug) |
| 740 | printk(KERN_DEBUG "bsd_decomp%d: bad CLEAR\n", db->unit); |
| 741 | return DECOMP_FATALERROR; /* probably a bug */ |
| 742 | } |
| 743 | bsd_clear(db); |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | if ((incode > max_ent + 2) || (incode > db->maxmaxcode) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 748 | || (incode > max_ent && oldcode == CLEAR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | if (db->debug) { |
| 750 | printk(KERN_DEBUG "bsd_decomp%d: bad code 0x%x oldcode=0x%x ", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 751 | db->unit, incode, oldcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | printk(KERN_DEBUG "max_ent=0x%x skb->Len=%d seqno=%d\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 753 | max_ent, skb_out->len, db->seqno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | } |
| 755 | return DECOMP_FATALERROR; /* probably a bug */ |
| 756 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /* Special case for KwKwK string. */ |
| 759 | if (incode > max_ent) { |
| 760 | finchar = oldcode; |
| 761 | extra = 1; |
| 762 | } else { |
| 763 | finchar = incode; |
| 764 | extra = 0; |
| 765 | } |
| 766 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 767 | codelen = *(lens_ptr(db, finchar)); |
| 768 | if (skb_tailroom(skb_out) < codelen + extra) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | if (db->debug) { |
| 770 | printk(KERN_DEBUG "bsd_decomp%d: ran out of mru\n", db->unit); |
| 771 | #ifdef DEBUG |
| 772 | printk(KERN_DEBUG " len=%d, finchar=0x%x, codelen=%d,skblen=%d\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 773 | ilen, finchar, codelen, skb_out->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | #endif |
| 775 | } |
| 776 | return DECOMP_FATALERROR; |
| 777 | } |
| 778 | |
| 779 | /* |
| 780 | * Decode this code and install it in the decompressed buffer. |
| 781 | */ |
| 782 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 783 | p = skb_put(skb_out, codelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | p += codelen; |
| 785 | while (finchar > LAST) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 786 | struct bsd_dict *dictp2 = dict_ptr(db, finchar); |
| 787 | |
| 788 | dictp = dict_ptr(db, dictp2->cptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | |
| 790 | #ifdef DEBUG |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 791 | if (--codelen <= 0 || dictp->codem1 != finchar - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (codelen <= 0) { |
| 793 | printk(KERN_ERR "bsd_decomp%d: fell off end of chain ", db->unit); |
| 794 | printk(KERN_ERR "0x%x at 0x%x by 0x%x, max_ent=0x%x\n", incode, finchar, dictp2->cptr, max_ent); |
| 795 | } else { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 796 | if (dictp->codem1 != finchar - 1) { |
| 797 | printk(KERN_ERR "bsd_decomp%d: bad code chain 0x%x finchar=0x%x ", db->unit, incode, finchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | printk(KERN_ERR "oldcode=0x%x cptr=0x%x codem1=0x%x\n", oldcode, dictp2->cptr, dictp->codem1); |
| 799 | } |
| 800 | } |
| 801 | return DECOMP_FATALERROR; |
| 802 | } |
| 803 | #endif |
| 804 | |
| 805 | { |
| 806 | u32 fcode = dictp->fcode; |
| 807 | *--p = (fcode >> 16) & 0xff; |
| 808 | finchar = fcode & 0xffff; |
| 809 | } |
| 810 | } |
| 811 | *--p = finchar; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 812 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | #ifdef DEBUG |
| 814 | if (--codelen != 0) |
| 815 | printk(KERN_ERR "bsd_decomp%d: short by %d after code 0x%x, max_ent=0x%x\n", db->unit, codelen, incode, max_ent); |
| 816 | #endif |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 817 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | if (extra) /* the KwKwK case again */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 819 | *(skb_put(skb_out, 1)) = finchar; |
| 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | /* |
| 822 | * If not first code in a packet, and |
| 823 | * if not out of code space, then allocate a new code. |
| 824 | * |
| 825 | * Keep the hash table correct so it can be used |
| 826 | * with uncompressed packets. |
| 827 | */ |
| 828 | if (oldcode != CLEAR && max_ent < db->maxmaxcode) { |
| 829 | struct bsd_dict *dictp2, *dictp3; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 830 | u16 *lens1, *lens2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | unsigned long fcode; |
| 832 | int hval, disp, indx; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 833 | |
| 834 | fcode = BSD_KEY(oldcode, finchar); |
| 835 | hval = BSD_HASH(oldcode, finchar, db->hshift); |
| 836 | dictp = dict_ptr(db, hval); |
| 837 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | /* look for a free hash table entry */ |
| 839 | if (dictp->codem1 < max_ent) { |
| 840 | disp = (hval == 0) ? 1 : hval; |
| 841 | do { |
| 842 | hval += disp; |
| 843 | if (hval >= db->hsize) |
| 844 | hval -= db->hsize; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 845 | dictp = dict_ptr(db, hval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } while (dictp->codem1 < max_ent); |
| 847 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | /* |
| 850 | * Invalidate previous hash table entry |
| 851 | * assigned this code, and then take it over |
| 852 | */ |
| 853 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 854 | dictp2 = dict_ptr(db, max_ent + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | indx = dictp2->cptr; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 856 | dictp3 = dict_ptr(db, indx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | if (dictp3->codem1 == max_ent) |
| 859 | dictp3->codem1 = BADCODEM1; |
| 860 | |
| 861 | dictp2->cptr = hval; |
| 862 | dictp->codem1 = max_ent; |
| 863 | dictp->fcode = fcode; |
| 864 | db->max_ent = ++max_ent; |
| 865 | |
| 866 | /* Update the length of this string. */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 867 | lens1 = lens_ptr(db, max_ent); |
| 868 | lens2 = lens_ptr(db, oldcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | *lens1 = *lens2 + 1; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | /* Expand code size if needed. */ |
| 872 | if (max_ent >= MAXCODE(n_bits) && max_ent < db->maxmaxcode) { |
| 873 | db->n_bits = ++n_bits; |
| 874 | tgtbitno = 32-n_bits; |
| 875 | } |
| 876 | } |
| 877 | oldcode = incode; |
| 878 | } |
| 879 | |
| 880 | ++db->comp_count; |
| 881 | ++db->uncomp_count; |
| 882 | db->comp_bytes += skb_in->len - BSD_OVHD; |
| 883 | db->uncomp_bytes += skb_out->len; |
| 884 | |
| 885 | if (bsd_check(db)) { |
| 886 | if (db->debug) |
| 887 | printk(KERN_DEBUG "bsd_decomp%d: peer should have cleared dictionary on %d\n", |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 888 | db->unit, db->seqno - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | return skb_out->len; |
| 891 | } |
| 892 | |
| 893 | /************************************************************* |
| 894 | * Table of addresses for the BSD compression module |
| 895 | *************************************************************/ |
| 896 | |
| 897 | static struct isdn_ppp_compressor ippp_bsd_compress = { |
| 898 | .owner = THIS_MODULE, |
| 899 | .num = CI_BSD_COMPRESS, |
| 900 | .alloc = bsd_alloc, |
| 901 | .free = bsd_free, |
| 902 | .init = bsd_init, |
| 903 | .reset = bsd_reset, |
| 904 | .compress = bsd_compress, |
| 905 | .decompress = bsd_decompress, |
| 906 | .incomp = bsd_incomp, |
| 907 | .stat = bsd_stats, |
| 908 | }; |
| 909 | |
| 910 | /************************************************************* |
| 911 | * Module support routines |
| 912 | *************************************************************/ |
| 913 | |
| 914 | static int __init isdn_bsdcomp_init(void) |
| 915 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 916 | int answer = isdn_ppp_register_compressor(&ippp_bsd_compress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | if (answer == 0) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 918 | printk(KERN_INFO "PPP BSD Compression module registered\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | return answer; |
| 920 | } |
| 921 | |
| 922 | static void __exit isdn_bsdcomp_exit(void) |
| 923 | { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 924 | isdn_ppp_unregister_compressor(&ippp_bsd_compress); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | module_init(isdn_bsdcomp_init); |
| 928 | module_exit(isdn_bsdcomp_exit); |