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. export_slhc_syms
  14. init_module
  15. cleanup_module
  16. slhc_install

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

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