isdn_bsdcomp.c 23 KB

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