isdn_bsdcomp.c 23 KB

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