root/drivers/net/slhc.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. slhc_init
  2. slhc_free
  3. put16
  4. encode
  5. pull16
  6. decode
  7. slhc_compress
  8. slhc_uncompress
  9. slhc_remember
  10. slhc_toss
  11. slhc_i_status
  12. slhc_o_status
  13. init_module
  14. cleanup_module

   1 /*
   2  * Routines to compress and uncompress tcp packets (for transmission
   3  * over low speed serial lines).
   4  *
   5  * Copyright (c) 1989 Regents of the University of California.
   6  * All rights reserved.
   7  *
   8  * Redistribution and use in source and binary forms are permitted
   9  * provided that the above copyright notice and this paragraph are
  10  * duplicated in all such forms and that any documentation,
  11  * advertising materials, and other materials related to such
  12  * distribution and use acknowledge that the software was developed
  13  * by the University of California, Berkeley.  The name of the
  14  * University may not be used to endorse or promote products derived
  15  * from this software without specific prior written permission.
  16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19  *
  20  *      Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
  21  *      - Initial distribution.
  22  *
  23  *
  24  * modified for KA9Q Internet Software Package by
  25  * Katie Stevens (dkstevens@ucdavis.edu)
  26  * University of California, Davis
  27  * Computing Services
  28  *      - 01-31-90      initial adaptation (from 1.19)
  29  *      PPP.05  02-15-90 [ks]
  30  *      PPP.08  05-02-90 [ks]   use PPP protocol field to signal compression
  31  *      PPP.15  09-90    [ks]   improve mbuf handling
  32  *      PPP.16  11-02    [karn] substantially rewritten to use NOS facilities
  33  *
  34  *      - Feb 1991      Bill_Simpson@um.cc.umich.edu
  35  *                      variable number of conversation slots
  36  *                      allow zero or one slots
  37  *                      separate routines
  38  *                      status display
  39  *      - Jul 1994      Dmitry Gorodchanin
  40  *                      Fixes for memory leaks.
  41  *      - Oct 1994      Dmitry Gorodchanin
  42  *                      Modularization.
  43  *      - Jan 1995      Bjorn Ekwall
  44  *                      Use ip_fast_csum from ip.h
  45  *      - July 1995     Christos A. Polyzols 
  46  *                      Spotted bug in tcp option checking
  47  *
  48  *
  49  *      This module is a difficult issue. It's clearly inet code but it's also clearly
  50  *      driver code belonging close to PPP and SLIP
  51  */
  52 
  53 #include <linux/config.h>
  54 #ifdef CONFIG_INET
  55 /* Entire module is for IP only */
  56 #ifdef MODULE
  57 #include <linux/module.h>
  58 #include <linux/version.h>
  59 #endif
  60 
  61 #include <linux/types.h>
  62 #include <linux/sched.h>
  63 #include <linux/mm.h>
  64 #include <linux/string.h>
  65 #include <linux/socket.h>
  66 #include <linux/sockios.h>
  67 #include <linux/termios.h>
  68 #include <linux/in.h>
  69 #include <linux/fcntl.h>
  70 #include <linux/inet.h>
  71 #include <linux/netdevice.h>
  72 #include <net/ip.h>
  73 #include <net/protocol.h>
  74 #include <net/icmp.h>
  75 #include <net/tcp.h>
  76 #include <linux/skbuff.h>
  77 #include <net/sock.h>
  78 #include <linux/errno.h>
  79 #include <linux/timer.h>
  80 #include <asm/system.h>
  81 #include <asm/segment.h>
  82 #include <linux/mm.h>
  83 #include <net/checksum.h>
  84 #include "slhc.h"
  85 
  86 int last_retran;
  87 
  88 static unsigned char *encode(unsigned char *cp, unsigned short n);
  89 static long decode(unsigned char **cpp);
  90 static unsigned char * put16(unsigned char *cp, unsigned short x);
  91 static unsigned short pull16(unsigned char **cpp);
  92 
  93 /* Initialize compression data structure
  94  *      slots must be in range 0 to 255 (zero meaning no compression)
  95  */
  96 struct slcompress *
  97 slhc_init(int rslots, int tslots)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         register short i;
 100         register struct cstate *ts;
 101         struct slcompress *comp;
 102 
 103         comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
 104                                             GFP_KERNEL);
 105         if (! comp)
 106                 return NULL;
 107 
 108         memset(comp, 0, sizeof(struct slcompress));
 109 
 110         if ( rslots > 0  &&  rslots < 256 ) {
 111                 comp->rstate =
 112                   (struct cstate *)kmalloc(rslots * sizeof(struct cstate),
 113                                            GFP_KERNEL);
 114                 if (! comp->rstate)
 115                 {
 116                         kfree((unsigned char *)comp);
 117                         return NULL;
 118                 }
 119                 memset(comp->rstate, 0, rslots * sizeof(struct cstate));
 120                 comp->rslot_limit = rslots - 1;
 121         }
 122 
 123         if ( tslots > 0  &&  tslots < 256 ) {
 124                 comp->tstate =
 125                   (struct cstate *)kmalloc(tslots * sizeof(struct cstate),
 126                                            GFP_KERNEL);
 127                 if (! comp->tstate)
 128                 {
 129                         kfree((unsigned char *)comp->rstate);
 130                         kfree((unsigned char *)comp);
 131                         return NULL;
 132                 }
 133                 memset(comp->tstate, 0, rslots * sizeof(struct cstate));
 134                 comp->tslot_limit = tslots - 1;
 135         }
 136 
 137         comp->xmit_oldest = 0;
 138         comp->xmit_current = 255;
 139         comp->recv_current = 255;
 140         /*
 141          * don't accept any packets with implicit index until we get
 142          * one with an explicit index.  Otherwise the uncompress code
 143          * will try to use connection 255, which is almost certainly
 144          * out of range
 145          */
 146         comp->flags |= SLF_TOSS;
 147 
 148         if ( tslots > 0 ) {
 149                 ts = comp->tstate;
 150                 for(i = comp->tslot_limit; i > 0; --i){
 151                         ts[i].cs_this = i;
 152                         ts[i].next = &(ts[i - 1]);
 153                 }
 154                 ts[0].next = &(ts[comp->tslot_limit]);
 155                 ts[0].cs_this = 0;
 156         }
 157 #ifdef MODULE
 158         MOD_INC_USE_COUNT;
 159 #endif
 160         return comp;
 161 }
 162 
 163 
 164 /* Free a compression data structure */
 165 void
 166 slhc_free(struct slcompress *comp)
     /* [previous][next][first][last][top][bottom][index][help] */
 167 {
 168         if ( comp == NULLSLCOMPR )
 169                 return;
 170 
 171         if ( comp->rstate != NULLSLSTATE )
 172                 kfree( comp->rstate );
 173 
 174         if ( comp->tstate != NULLSLSTATE )
 175                 kfree( comp->tstate );
 176 
 177 #ifdef MODULE
 178         MOD_DEC_USE_COUNT;
 179 #endif
 180         kfree( comp );
 181 }
 182 
 183 
 184 /* Put a short in host order into a char array in network order */
 185 static inline unsigned char *
 186 put16(unsigned char *cp, unsigned short x)
     /* [previous][next][first][last][top][bottom][index][help] */
 187 {
 188         *cp++ = x >> 8;
 189         *cp++ = x;
 190 
 191         return cp;
 192 }
 193 
 194 
 195 /* Encode a number */
 196 unsigned char *
 197 encode(unsigned char *cp, unsigned short n)
     /* [previous][next][first][last][top][bottom][index][help] */
 198 {
 199         if(n >= 256 || n == 0){
 200                 *cp++ = 0;
 201                 cp = put16(cp,n);
 202         } else {
 203                 *cp++ = n;
 204         }
 205         return cp;
 206 }
 207 
 208 /* Pull a 16-bit integer in host order from buffer in network byte order */
 209 static unsigned short
 210 pull16(unsigned char **cpp)
     /* [previous][next][first][last][top][bottom][index][help] */
 211 {
 212         short rval;
 213 
 214         rval = *(*cpp)++;
 215         rval <<= 8;
 216         rval |= *(*cpp)++;
 217         return rval;
 218 }
 219 
 220 /* Decode a number */
 221 long
 222 decode(unsigned char **cpp)
     /* [previous][next][first][last][top][bottom][index][help] */
 223 {
 224         register int x;
 225 
 226         x = *(*cpp)++;
 227         if(x == 0){
 228                 return pull16(cpp) & 0xffff;    /* pull16 returns -1 on error */
 229         } else {
 230                 return x & 0xff;                /* -1 if PULLCHAR returned error */
 231         }
 232 }
 233 
 234 /*
 235  * icp and isize are the original packet.
 236  * ocp is a place to put a copy if necessary.
 237  * cpp is initially a pointer to icp.  If the copy is used,
 238  *    change it to ocp.
 239  */
 240 
 241 int
 242 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
     /* [previous][next][first][last][top][bottom][index][help] */
 243         unsigned char *ocp, unsigned char **cpp, int compress_cid)
 244 {
 245         register struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
 246         register struct cstate *lcs = ocs;
 247         register struct cstate *cs = lcs->next;
 248         register unsigned long deltaS, deltaA;
 249         register short changes = 0;
 250         int hlen;
 251         unsigned char new_seq[16];
 252         register unsigned char *cp = new_seq;
 253         struct iphdr *ip;
 254         struct tcphdr *th, *oth;
 255 
 256         ip = (struct iphdr *) icp;
 257 
 258         /* Bail if this packet isn't TCP, or is an IP fragment */
 259         if(ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x1fff) ||
 260                                        (ip->frag_off & 32)){
 261                 /* Send as regular IP */
 262                 if(ip->protocol != IPPROTO_TCP)
 263                         comp->sls_o_nontcp++;
 264                 else
 265                         comp->sls_o_tcp++;
 266                 return isize;
 267         }
 268         /* Extract TCP header */
 269 
 270         th = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
 271         hlen = ip->ihl*4 + th->doff*4;
 272 
 273         /*  Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
 274          *  some other control bit is set).
 275          */
 276         if(th->syn || th->fin || th->rst ||
 277             ! (th->ack)){
 278                 /* TCP connection stuff; send as regular IP */
 279                 comp->sls_o_tcp++;
 280                 return isize;
 281         }
 282         /*
 283          * Packet is compressible -- we're going to send either a
 284          * COMPRESSED_TCP or UNCOMPRESSED_TCP packet.  Either way,
 285          * we need to locate (or create) the connection state.
 286          *
 287          * States are kept in a circularly linked list with
 288          * xmit_oldest pointing to the end of the list.  The
 289          * list is kept in lru order by moving a state to the
 290          * head of the list whenever it is referenced.  Since
 291          * the list is short and, empirically, the connection
 292          * we want is almost always near the front, we locate
 293          * states via linear search.  If we don't find a state
 294          * for the datagram, the oldest state is (re-)used.
 295          */
 296         for ( ; ; ) {
 297                 if( ip->saddr == cs->cs_ip.saddr
 298                  && ip->daddr == cs->cs_ip.daddr
 299                  && th->source == cs->cs_tcp.source
 300                  && th->dest == cs->cs_tcp.dest)
 301                         goto found;
 302 
 303                 /* if current equal oldest, at end of list */
 304                 if ( cs == ocs )
 305                         break;
 306                 lcs = cs;
 307                 cs = cs->next;
 308                 comp->sls_o_searches++;
 309         };
 310         /*
 311          * Didn't find it -- re-use oldest cstate.  Send an
 312          * uncompressed packet that tells the other side what
 313          * connection number we're using for this conversation.
 314          *
 315          * Note that since the state list is circular, the oldest
 316          * state points to the newest and we only need to set
 317          * xmit_oldest to update the lru linkage.
 318          */
 319         comp->sls_o_misses++;
 320         comp->xmit_oldest = lcs->cs_this;
 321         goto uncompressed;
 322 
 323 found:
 324         /*
 325          * Found it -- move to the front on the connection list.
 326          */
 327         if(lcs == ocs) {
 328                 /* found at most recently used */
 329         } else if (cs == ocs) {
 330                 /* found at least recently used */
 331                 comp->xmit_oldest = lcs->cs_this;
 332         } else {
 333                 /* more than 2 elements */
 334                 lcs->next = cs->next;
 335                 cs->next = ocs->next;
 336                 ocs->next = cs;
 337         }
 338 
 339         /*
 340          * Make sure that only what we expect to change changed.
 341          * Check the following:
 342          * IP protocol version, header length & type of service.
 343          * The "Don't fragment" bit.
 344          * The time-to-live field.
 345          * The TCP header length.
 346          * IP options, if any.
 347          * TCP options, if any.
 348          * If any of these things are different between the previous &
 349          * current datagram, we send the current datagram `uncompressed'.
 350          */
 351         oth = &cs->cs_tcp;
 352 
 353         if(last_retran
 354          || ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
 355          || ip->tos != cs->cs_ip.tos
 356          || (ip->frag_off & 64) != (cs->cs_ip.frag_off & 64)
 357          || ip->ttl != cs->cs_ip.ttl
 358          || th->doff != cs->cs_tcp.doff
 359          || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
 360          || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4) != 0)){
 361                 goto uncompressed;
 362         }
 363 
 364         /*
 365          * Figure out which of the changing fields changed.  The
 366          * receiver expects changes in the order: urgent, window,
 367          * ack, seq (the order minimizes the number of temporaries
 368          * needed in this section of code).
 369          */
 370         if(th->urg){
 371                 deltaS = ntohs(th->urg_ptr);
 372                 cp = encode(cp,deltaS);
 373                 changes |= NEW_U;
 374         } else if(th->urg_ptr != oth->urg_ptr){
 375                 /* argh! URG not set but urp changed -- a sensible
 376                  * implementation should never do this but RFC793
 377                  * doesn't prohibit the change so we have to deal
 378                  * with it. */
 379                 goto uncompressed;
 380         }
 381         if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
 382                 cp = encode(cp,deltaS);
 383                 changes |= NEW_W;
 384         }
 385         if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
 386                 if(deltaA > 0x0000ffff)
 387                         goto uncompressed;
 388                 cp = encode(cp,deltaA);
 389                 changes |= NEW_A;
 390         }
 391         if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
 392                 if(deltaS > 0x0000ffff)
 393                         goto uncompressed;
 394                 cp = encode(cp,deltaS);
 395                 changes |= NEW_S;
 396         }
 397 
 398         switch(changes){
 399         case 0: /* Nothing changed. If this packet contains data and the
 400                  * last one didn't, this is probably a data packet following
 401                  * an ack (normal on an interactive connection) and we send
 402                  * it compressed.  Otherwise it's probably a retransmit,
 403                  * retransmitted ack or window probe.  Send it uncompressed
 404                  * in case the other side missed the compressed version.
 405                  */
 406                 if(ip->tot_len != cs->cs_ip.tot_len &&
 407                    ntohs(cs->cs_ip.tot_len) == hlen)
 408                         break;
 409                 goto uncompressed;
 410                 break;
 411         case SPECIAL_I:
 412         case SPECIAL_D:
 413                 /* actual changes match one of our special case encodings --
 414                  * send packet uncompressed.
 415                  */
 416                 goto uncompressed;
 417         case NEW_S|NEW_A:
 418                 if(deltaS == deltaA &&
 419                     deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
 420                         /* special case for echoed terminal traffic */
 421                         changes = SPECIAL_I;
 422                         cp = new_seq;
 423                 }
 424                 break;
 425         case NEW_S:
 426                 if(deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
 427                         /* special case for data xfer */
 428                         changes = SPECIAL_D;
 429                         cp = new_seq;
 430                 }
 431                 break;
 432         }
 433         deltaS = ntohs(ip->id) - ntohs(cs->cs_ip.id);
 434         if(deltaS != 1){
 435                 cp = encode(cp,deltaS);
 436                 changes |= NEW_I;
 437         }
 438         if(th->psh)
 439                 changes |= TCP_PUSH_BIT;
 440         /* Grab the cksum before we overwrite it below.  Then update our
 441          * state with this packet's header.
 442          */
 443         deltaA = ntohs(th->check);
 444         memcpy(&cs->cs_ip,ip,20);
 445         memcpy(&cs->cs_tcp,th,20);
 446         /* We want to use the original packet as our compressed packet.
 447          * (cp - new_seq) is the number of bytes we need for compressed
 448          * sequence numbers.  In addition we need one byte for the change
 449          * mask, one for the connection id and two for the tcp checksum.
 450          * So, (cp - new_seq) + 4 bytes of header are needed.
 451          */
 452         deltaS = cp - new_seq;
 453         if(compress_cid == 0 || comp->xmit_current != cs->cs_this){
 454                 cp = ocp;
 455                 *cpp = ocp;
 456                 *cp++ = changes | NEW_C;
 457                 *cp++ = cs->cs_this;
 458                 comp->xmit_current = cs->cs_this;
 459         } else {
 460                 cp = ocp;
 461                 *cpp = ocp;
 462                 *cp++ = changes;
 463         }
 464         cp = put16(cp,(short)deltaA);   /* Write TCP checksum */
 465 /* deltaS is now the size of the change section of the compressed header */
 466         memcpy(cp,new_seq,deltaS);      /* Write list of deltas */
 467         memcpy(cp+deltaS,icp+hlen,isize-hlen);
 468         comp->sls_o_compressed++;
 469         ocp[0] |= SL_TYPE_COMPRESSED_TCP;
 470         return isize - hlen + deltaS + (cp - ocp);
 471 
 472         /* Update connection state cs & send uncompressed packet (i.e.,
 473          * a regular ip/tcp packet but with the 'conversation id' we hope
 474          * to use on future compressed packets in the protocol field).
 475          */
 476 uncompressed:
 477         memcpy(&cs->cs_ip,ip,20);
 478         memcpy(&cs->cs_tcp,th,20);
 479         if (ip->ihl > 5)
 480           memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
 481         if (th->doff > 5)
 482           memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
 483         comp->xmit_current = cs->cs_this;
 484         comp->sls_o_uncompressed++;
 485         memcpy(ocp, icp, isize);
 486         *cpp = ocp;
 487         ocp[9] = cs->cs_this;
 488         ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
 489         return isize;
 490 }
 491 
 492 
 493 int
 494 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
     /* [previous][next][first][last][top][bottom][index][help] */
 495 {
 496         register int changes;
 497         long x;
 498         register struct tcphdr *thp;
 499         register struct iphdr *ip;
 500         register struct cstate *cs;
 501         int len, hdrlen;
 502         unsigned char *cp = icp;
 503 
 504         /* We've got a compressed packet; read the change byte */
 505         comp->sls_i_compressed++;
 506         if(isize < 3){
 507                 comp->sls_i_error++;
 508                 return 0;
 509         }
 510         changes = *cp++;
 511         if(changes & NEW_C){
 512                 /* Make sure the state index is in range, then grab the state.
 513                  * If we have a good state index, clear the 'discard' flag.
 514                  */
 515                 x = *cp++;      /* Read conn index */
 516                 if(x < 0 || x > comp->rslot_limit)
 517                         goto bad;
 518 
 519                 comp->flags &=~ SLF_TOSS;
 520                 comp->recv_current = x;
 521         } else {
 522                 /* this packet has an implicit state index.  If we've
 523                  * had a line error since the last time we got an
 524                  * explicit state index, we have to toss the packet. */
 525                 if(comp->flags & SLF_TOSS){
 526                         comp->sls_i_tossed++;
 527                         return 0;
 528                 }
 529         }
 530         cs = &comp->rstate[comp->recv_current];
 531         thp = &cs->cs_tcp;
 532         ip = &cs->cs_ip;
 533 
 534         if((x = pull16(&cp)) == -1) {   /* Read the TCP checksum */
 535                 goto bad;
 536         }
 537         thp->check = htons(x);
 538 
 539         thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
 540 /*
 541  * we can use the same number for the length of the saved header and
 542  * the current one, because the packet wouldn't have been sent
 543  * as compressed unless the options were the same as the previous one
 544  */
 545 
 546         hdrlen = ip->ihl * 4 + thp->doff * 4;
 547 
 548         switch(changes & SPECIALS_MASK){
 549         case SPECIAL_I:         /* Echoed terminal traffic */
 550                 {
 551                 register short i;
 552                 i = ntohs(ip->tot_len) - hdrlen;
 553                 thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
 554                 thp->seq = htonl( ntohl(thp->seq) + i);
 555                 }
 556                 break;
 557 
 558         case SPECIAL_D:                 /* Unidirectional data */
 559                 thp->seq = htonl( ntohl(thp->seq) +
 560                                   ntohs(ip->tot_len) - hdrlen);
 561                 break;
 562 
 563         default:
 564                 if(changes & NEW_U){
 565                         thp->urg = 1;
 566                         if((x = decode(&cp)) == -1) {
 567                                 goto bad;
 568                         }
 569                         thp->urg_ptr = htons(x);
 570                 } else
 571                         thp->urg = 0;
 572                 if(changes & NEW_W){
 573                         if((x = decode(&cp)) == -1) {
 574                                 goto bad;
 575                         }
 576                         thp->window = htons( ntohs(thp->window) + x);
 577                 }
 578                 if(changes & NEW_A){
 579                         if((x = decode(&cp)) == -1) {
 580                                 goto bad;
 581                         }
 582                         thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
 583                 }
 584                 if(changes & NEW_S){
 585                         if((x = decode(&cp)) == -1) {
 586                                 goto bad;
 587                         }
 588                         thp->seq = htonl( ntohl(thp->seq) + x);
 589                 }
 590                 break;
 591         }
 592         if(changes & NEW_I){
 593                 if((x = decode(&cp)) == -1) {
 594                         goto bad;
 595                 }
 596                 ip->id = htons (ntohs (ip->id) + x);
 597         } else
 598                 ip->id = htons (ntohs (ip->id) + 1);
 599 
 600         /*
 601          * At this point, cp points to the first byte of data in the
 602          * packet.  Put the reconstructed TCP and IP headers back on the
 603          * packet.  Recalculate IP checksum (but not TCP checksum).
 604          */
 605 
 606         len = isize - (cp - icp);
 607         if (len < 0)
 608                 goto bad;
 609         len += hdrlen;
 610         ip->tot_len = htons(len);
 611         ip->check = 0;
 612 
 613         memmove(icp + hdrlen, cp, len - hdrlen);
 614 
 615         cp = icp;
 616         memcpy(cp, ip, 20);
 617         cp += 20;
 618 
 619         if (ip->ihl > 5) {
 620           memcpy(cp, cs->cs_ipopt, (ip->ihl - 5) * 4);
 621           cp += (ip->ihl - 5) * 4;
 622         }
 623 
 624         ((struct iphdr *)icp)->check = ip_fast_csum(icp, ((struct iphdr*)icp)->ihl);
 625 
 626         memcpy(cp, thp, 20);
 627         cp += 20;
 628 
 629         if (thp->doff > 5) {
 630           memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
 631           cp += ((thp->doff) - 5) * 4;
 632         }
 633 
 634         return len;
 635 bad:
 636         comp->sls_i_error++;
 637         return slhc_toss( comp );
 638 }
 639 
 640 
 641 int
 642 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
     /* [previous][next][first][last][top][bottom][index][help] */
 643 {
 644         register struct cstate *cs;
 645         unsigned ihl;
 646 
 647         unsigned char index;
 648 
 649         if(isize < 20) {
 650                 /* The packet is shorter than a legal IP header */
 651                 comp->sls_i_runt++;
 652                 return slhc_toss( comp );
 653         }
 654         /* Peek at the IP header's IHL field to find its length */
 655         ihl = icp[0] & 0xf;
 656         if(ihl < 20 / 4){
 657                 /* The IP header length field is too small */
 658                 comp->sls_i_runt++;
 659                 return slhc_toss( comp );
 660         }
 661         index = icp[9];
 662         icp[9] = IPPROTO_TCP;
 663 
 664         if (ip_fast_csum(icp, ihl)) {
 665                 /* Bad IP header checksum; discard */
 666                 comp->sls_i_badcheck++;
 667                 return slhc_toss( comp );
 668         }
 669         if(index > comp->rslot_limit) {
 670                 comp->sls_i_error++;
 671                 return slhc_toss(comp);
 672         }
 673 
 674         /* Update local state */
 675         cs = &comp->rstate[comp->recv_current = index];
 676         comp->flags &=~ SLF_TOSS;
 677         memcpy(&cs->cs_ip,icp,20);
 678         memcpy(&cs->cs_tcp,icp + ihl*4,20);
 679         if (ihl > 5)
 680           memcpy(cs->cs_ipopt, icp + sizeof(struct iphdr), (ihl - 5) * 4);
 681         if (cs->cs_tcp.doff > 5)
 682           memcpy(cs->cs_tcpopt, icp + ihl*4 + sizeof(struct tcphdr), (cs->cs_tcp.doff - 5) * 4);
 683         cs->cs_hsize = ihl*2 + cs->cs_tcp.doff*2;
 684         /* Put headers back on packet
 685          * Neither header checksum is recalculated
 686          */
 687         comp->sls_i_uncompressed++;
 688         return isize;
 689 }
 690 
 691 
 692 int
 693 slhc_toss(struct slcompress *comp)
     /* [previous][next][first][last][top][bottom][index][help] */
 694 {
 695         if ( comp == NULLSLCOMPR )
 696                 return 0;
 697 
 698         comp->flags |= SLF_TOSS;
 699         return 0;
 700 }
 701 
 702 
 703 void slhc_i_status(struct slcompress *comp)
     /* [previous][next][first][last][top][bottom][index][help] */
 704 {
 705         if (comp != NULLSLCOMPR) {
 706                 printk("\t%ld Cmp, %ld Uncmp, %ld Bad, %ld Tossed\n",
 707                         comp->sls_i_compressed,
 708                         comp->sls_i_uncompressed,
 709                         comp->sls_i_error,
 710                         comp->sls_i_tossed);
 711         }
 712 }
 713 
 714 
 715 void slhc_o_status(struct slcompress *comp)
     /* [previous][next][first][last][top][bottom][index][help] */
 716 {
 717         if (comp != NULLSLCOMPR) {
 718                 printk("\t%ld Cmp, %ld Uncmp, %ld AsIs, %ld NotTCP\n",
 719                         comp->sls_o_compressed,
 720                         comp->sls_o_uncompressed,
 721                         comp->sls_o_tcp,
 722                         comp->sls_o_nontcp);
 723                 printk("\t%10ld Searches, %10ld Misses\n",
 724                         comp->sls_o_searches,
 725                         comp->sls_o_misses);
 726         }
 727 }
 728 
 729 #ifdef MODULE
 730 char kernel_version[] = UTS_RELEASE;
 731 
 732 int init_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 733 {
 734         printk("CSLIP: code copyright 1989 Regents of the University of California\n");
 735         return 0;
 736 }
 737 
 738 void cleanup_module(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 739 {
 740         if (MOD_IN_USE)  {
 741                 printk("CSLIP: module in use, remove delayed");
 742         }
 743         return;
 744 }
 745 #endif /* MODULE */
 746 #endif /* CONFIG_INET */

/* [previous][next][first][last][top][bottom][index][help] */