isdn_bsdcomp.c 24 KB

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