* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-0.99 / drivers / net / slhc.c
blob9ba15d0193f430ca68825156b21547a9b03929bb
1 /*
2 * Routines to compress and uncompress tcp packets (for transmission
3 * over low speed serial lines).
5 * Copyright (c) 1989 Regents of the University of California.
6 * All rights reserved.
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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
21 * - Initial distribution.
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
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
41 #include <linux/types.h>
42 #include <linux/sched.h>
43 #include <linux/mm.h>
44 #include <linux/string.h>
45 #include <linux/socket.h>
46 #include <linux/sockios.h>
47 #include <linux/termios.h>
48 #include <linux/in.h>
49 #include <linux/fcntl.h>
50 #include "inet.h"
51 #include "dev.h"
52 #include "ip.h"
53 #include "protocol.h"
54 #include "icmp.h"
55 #include "tcp.h"
56 #include "skbuff.h"
57 #include "sock.h"
58 #include "arp.h"
59 #include <linux/errno.h>
60 #include <linux/timer.h>
61 #include <asm/system.h>
62 #include <asm/segment.h>
63 #include <linux/mm.h>
64 #include "slhc.h"
66 #define DPRINT(x)
68 int last_retran;
70 static unsigned char *encode(unsigned char *cp,int n);
71 static long decode(unsigned char **cpp);
72 static unsigned char * put16(unsigned char *cp, unsigned short x);
73 static unsigned short pull16(unsigned char **cpp);
75 extern int ip_csum(struct iphdr *iph);
78 /* Initialize compression data structure
79 * slots must be in range 0 to 255 (zero meaning no compression)
81 struct slcompress *
82 slhc_init(int rslots, int tslots)
84 register short i;
85 register struct cstate *ts;
86 struct slcompress *comp;
88 comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
89 GFP_KERNEL);
90 if (! comp)
91 return NULL;
93 memset(comp, 0, sizeof(struct slcompress));
95 if ( rslots > 0 && rslots < 256 ) {
96 comp->rstate =
97 (struct cstate *)kmalloc(rslots * sizeof(struct cstate),
98 GFP_KERNEL);
99 if (! comp->rstate)
100 return NULL;
101 memset(comp->rstate, 0, rslots * sizeof(struct cstate));
102 comp->rslot_limit = rslots - 1;
105 if ( tslots > 0 && tslots < 256 ) {
106 comp->tstate =
107 (struct cstate *)kmalloc(tslots * sizeof(struct cstate),
108 GFP_KERNEL);
109 if (! comp->tstate)
110 return NULL;
111 memset(comp->tstate, 0, rslots * sizeof(struct cstate));
112 comp->tslot_limit = tslots - 1;
115 comp->xmit_oldest = 0;
116 comp->xmit_current = 255;
117 comp->recv_current = 255;
119 * don't accept any packets with implicit index until we get
120 * one with an explicit index. Otherwise the uncompress code
121 * will try to use connection 255, which is almost certainly
122 * out of range
124 comp->flags |= SLF_TOSS;
126 if ( tslots > 0 ) {
127 ts = comp->tstate;
128 for(i = comp->tslot_limit; i > 0; --i){
129 ts[i].cs_this = i;
130 ts[i].next = &(ts[i - 1]);
132 ts[0].next = &(ts[comp->tslot_limit]);
133 ts[0].cs_this = 0;
135 return comp;
139 /* Free a compression data structure */
140 void
141 slhc_free(struct slcompress *comp)
143 if ( comp == NULLSLCOMPR )
144 return;
146 if ( comp->rstate != NULLSLSTATE )
147 kfree( comp->rstate );
149 if ( comp->tstate != NULLSLSTATE )
150 kfree( comp->tstate );
152 kfree( comp );
156 /* Put a short in host order into a char array in network order */
157 static unsigned char *
158 put16(unsigned char *cp, unsigned short x)
160 *cp++ = x >> 8;
161 *cp++ = x;
163 return cp;
167 /* Encode a number */
168 unsigned char *
169 encode(unsigned char *cp, int n)
171 if(n >= 256 || n == 0){
172 *cp++ = 0;
173 cp = put16(cp,n);
174 } else {
175 *cp++ = n;
177 return cp;
180 /* Pull a 16-bit integer in host order from buffer in network byte order */
181 static unsigned short
182 pull16(unsigned char **cpp)
184 short rval;
186 rval = *(*cpp)++;
187 rval <<= 8;
188 rval |= *(*cpp)++;
189 return rval;
192 /* Decode a number */
193 long
194 decode(unsigned char **cpp)
196 register int x;
198 x = *(*cpp)++;
199 if(x == 0){
200 return pull16(cpp) & 0xffff; /* pull16 returns -1 on error */
201 } else {
202 return x & 0xff; /* -1 if PULLCHAR returned error */
207 * icp and isize are the original packet.
208 * ocp is a place to put a copy if necessary.
209 * cpp is initially a pointer to icp. If the copy is used,
210 * change it to ocp.
214 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
215 unsigned char *ocp, unsigned char **cpp, int compress_cid)
217 register struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
218 register struct cstate *lcs = ocs;
219 register struct cstate *cs = lcs->next;
220 register unsigned long deltaS, deltaA;
221 register short changes = 0;
222 int hlen;
223 unsigned char new_seq[16];
224 register unsigned char *cp = new_seq;
225 struct iphdr *ip;
226 struct tcphdr *th, *oth;
228 ip = (struct iphdr *) icp;
230 /* Bail if this packet isn't TCP, or is an IP fragment */
231 if(ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x1fff) ||
232 (ip->frag_off & 32)){
233 DPRINT(("comp: noncomp 1 %d %d %d\n", ip->protocol,
234 ntohs(ip->frag_off), ip->frag_off));
235 /* Send as regular IP */
236 if(ip->protocol != IPPROTO_TCP)
237 comp->sls_o_nontcp++;
238 else
239 comp->sls_o_tcp++;
240 return isize;
242 /* Extract TCP header */
244 th = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
245 hlen = ip->ihl*4 + th->doff*4;
247 /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
248 * some other control bit is set).
250 if(th->syn || th->fin || th->rst ||
251 ! (th->ack)){
252 DPRINT(("comp: noncomp 2 %x %x %d %d %d %d\n", ip, th,
253 th->syn, th->fin, th->rst, th->ack));
254 /* TCP connection stuff; send as regular IP */
255 comp->sls_o_tcp++;
256 return isize;
259 * Packet is compressible -- we're going to send either a
260 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
261 * we need to locate (or create) the connection state.
263 * States are kept in a circularly linked list with
264 * xmit_oldest pointing to the end of the list. The
265 * list is kept in lru order by moving a state to the
266 * head of the list whenever it is referenced. Since
267 * the list is short and, empirically, the connection
268 * we want is almost always near the front, we locate
269 * states via linear search. If we don't find a state
270 * for the datagram, the oldest state is (re-)used.
272 for ( ; ; ) {
273 if( ip->saddr == cs->cs_ip.saddr
274 && ip->daddr == cs->cs_ip.daddr
275 && th->source == cs->cs_tcp.source
276 && th->dest == cs->cs_tcp.dest)
277 goto found;
279 /* if current equal oldest, at end of list */
280 if ( cs == ocs )
281 break;
282 lcs = cs;
283 cs = cs->next;
284 comp->sls_o_searches++;
287 * Didn't find it -- re-use oldest cstate. Send an
288 * uncompressed packet that tells the other side what
289 * connection number we're using for this conversation.
291 * Note that since the state list is circular, the oldest
292 * state points to the newest and we only need to set
293 * xmit_oldest to update the lru linkage.
295 comp->sls_o_misses++;
296 comp->xmit_oldest = lcs->cs_this;
297 DPRINT(("comp: not found\n"));
298 goto uncompressed;
300 found:
302 * Found it -- move to the front on the connection list.
304 if(lcs == ocs) {
305 /* found at most recently used */
306 } else if (cs == ocs) {
307 /* found at least recently used */
308 comp->xmit_oldest = lcs->cs_this;
309 } else {
310 /* more than 2 elements */
311 lcs->next = cs->next;
312 cs->next = ocs->next;
313 ocs->next = cs;
317 * Make sure that only what we expect to change changed.
318 * Check the following:
319 * IP protocol version, header length & type of service.
320 * The "Don't fragment" bit.
321 * The time-to-live field.
322 * The TCP header length.
323 * IP options, if any.
324 * TCP options, if any.
325 * If any of these things are different between the previous &
326 * current datagram, we send the current datagram `uncompressed'.
328 oth = &cs->cs_tcp;
330 if(last_retran
331 || ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
332 || ip->tos != cs->cs_ip.tos
333 || (ip->frag_off & 64) != (cs->cs_ip.frag_off & 64)
334 || ip->ttl != cs->cs_ip.ttl
335 || th->doff != cs->cs_tcp.doff
336 || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
337 || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4 != 0))){
338 DPRINT(("comp: incompat\n"));
339 goto uncompressed;
343 * Figure out which of the changing fields changed. The
344 * receiver expects changes in the order: urgent, window,
345 * ack, seq (the order minimizes the number of temporaries
346 * needed in this section of code).
348 if(th->urg){
349 deltaS = ntohs(th->urg_ptr);
350 cp = encode(cp,deltaS);
351 changes |= NEW_U;
352 } else if(th->urg_ptr != oth->urg_ptr){
353 /* argh! URG not set but urp changed -- a sensible
354 * implementation should never do this but RFC793
355 * doesn't prohibit the change so we have to deal
356 * with it. */
357 DPRINT(("comp: urg incompat\n"));
358 goto uncompressed;
360 if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
361 cp = encode(cp,deltaS);
362 changes |= NEW_W;
364 if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
365 if(deltaA > 0x0000ffff)
366 goto uncompressed;
367 cp = encode(cp,deltaA);
368 changes |= NEW_A;
370 if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
371 if(deltaS > 0x0000ffff)
372 goto uncompressed;
373 cp = encode(cp,deltaS);
374 changes |= NEW_S;
377 switch(changes){
378 case 0: /* Nothing changed. If this packet contains data and the
379 * last one didn't, this is probably a data packet following
380 * an ack (normal on an interactive connection) and we send
381 * it compressed. Otherwise it's probably a retransmit,
382 * retransmitted ack or window probe. Send it uncompressed
383 * in case the other side missed the compressed version.
385 if(ip->tot_len != cs->cs_ip.tot_len &&
386 ntohs(cs->cs_ip.tot_len) == hlen)
387 break;
388 DPRINT(("comp: retrans\n"));
389 goto uncompressed;
390 break;
391 case SPECIAL_I:
392 case SPECIAL_D:
393 /* actual changes match one of our special case encodings --
394 * send packet uncompressed.
396 DPRINT(("comp: special\n"));
397 goto uncompressed;
398 case NEW_S|NEW_A:
399 if(deltaS == deltaA &&
400 deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
401 /* special case for echoed terminal traffic */
402 changes = SPECIAL_I;
403 cp = new_seq;
405 break;
406 case NEW_S:
407 if(deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
408 /* special case for data xfer */
409 changes = SPECIAL_D;
410 cp = new_seq;
412 break;
414 deltaS = ntohs(ip->id) - ntohs(cs->cs_ip.id);
415 if(deltaS != 1){
416 cp = encode(cp,deltaS);
417 changes |= NEW_I;
419 if(th->psh)
420 changes |= TCP_PUSH_BIT;
421 /* Grab the cksum before we overwrite it below. Then update our
422 * state with this packet's header.
424 deltaA = ntohs(th->check);
425 memcpy(&cs->cs_ip,ip,20);
426 memcpy(&cs->cs_tcp,th,20);
427 /* We want to use the original packet as our compressed packet.
428 * (cp - new_seq) is the number of bytes we need for compressed
429 * sequence numbers. In addition we need one byte for the change
430 * mask, one for the connection id and two for the tcp checksum.
431 * So, (cp - new_seq) + 4 bytes of header are needed.
433 deltaS = cp - new_seq;
434 if(compress_cid == 0 || comp->xmit_current != cs->cs_this){
435 cp = ocp;
436 *cpp = ocp;
437 *cp++ = changes | NEW_C;
438 *cp++ = cs->cs_this;
439 comp->xmit_current = cs->cs_this;
440 } else {
441 cp = ocp;
442 *cpp = ocp;
443 *cp++ = changes;
445 cp = put16(cp,(short)deltaA); /* Write TCP checksum */
446 /* deltaS is now the size of the change section of the compressed header */
447 DPRINT(("comp: %x %x %x %d %d\n", icp, cp, new_seq, hlen, deltaS));
448 memcpy(cp,new_seq,deltaS); /* Write list of deltas */
449 memcpy(cp+deltaS,icp+hlen,isize-hlen);
450 comp->sls_o_compressed++;
451 ocp[0] |= SL_TYPE_COMPRESSED_TCP;
452 return isize - hlen + deltaS + (cp - ocp);
454 /* Update connection state cs & send uncompressed packet (i.e.,
455 * a regular ip/tcp packet but with the 'conversation id' we hope
456 * to use on future compressed packets in the protocol field).
458 uncompressed:
459 memcpy(&cs->cs_ip,ip,20);
460 memcpy(&cs->cs_tcp,th,20);
461 if (ip->ihl > 5)
462 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
463 if (th->doff > 5)
464 memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
465 comp->xmit_current = cs->cs_this;
466 comp->sls_o_uncompressed++;
467 memcpy(ocp, icp, isize);
468 *cpp = ocp;
469 ocp[9] = cs->cs_this;
470 ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
471 return isize;
476 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
478 register int changes;
479 long x;
480 register struct tcphdr *thp;
481 register struct iphdr *ip;
482 register struct cstate *cs;
483 int len, hdrlen;
484 unsigned char *cp = icp;
486 /* We've got a compressed packet; read the change byte */
487 comp->sls_i_compressed++;
488 if(isize < 3){
489 comp->sls_i_error++;
490 DPRINT(("uncomp: runt\n"));
491 return 0;
493 changes = *cp++;
494 if(changes & NEW_C){
495 /* Make sure the state index is in range, then grab the state.
496 * If we have a good state index, clear the 'discard' flag.
498 x = *cp++; /* Read conn index */
499 if(x < 0 || x > comp->rslot_limit)
500 goto bad;
502 comp->flags &=~ SLF_TOSS;
503 comp->recv_current = x;
504 } else {
505 /* this packet has an implicit state index. If we've
506 * had a line error since the last time we got an
507 * explicit state index, we have to toss the packet. */
508 if(comp->flags & SLF_TOSS){
509 comp->sls_i_tossed++;
510 DPRINT(("uncomp: toss\n"));
511 return 0;
514 cs = &comp->rstate[comp->recv_current];
515 thp = &cs->cs_tcp;
516 ip = &cs->cs_ip;
518 if((x = pull16(&cp)) == -1) { /* Read the TCP checksum */
519 DPRINT(("uncomp: bad tcp chk\n"));
520 goto bad;
522 thp->check = htons(x);
524 thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
526 * we can use the same number for the length of the saved header and
527 * the current one, because the packet wouldn't have been sent
528 * as compressed unless the options were the same as the previous one
531 hdrlen = ip->ihl * 4 + thp->doff * 4;
533 switch(changes & SPECIALS_MASK){
534 case SPECIAL_I: /* Echoed terminal traffic */
536 register short i;
537 i = ntohs(ip->tot_len) - hdrlen;
538 thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
539 thp->seq = htonl( ntohl(thp->seq) + i);
541 break;
543 case SPECIAL_D: /* Unidirectional data */
544 thp->seq = htonl( ntohl(thp->seq) +
545 ntohs(ip->tot_len) - hdrlen);
546 break;
548 default:
549 if(changes & NEW_U){
550 thp->urg = 1;
551 if((x = decode(&cp)) == -1) {
552 DPRINT(("uncomp: bad U\n"));
553 goto bad;
555 thp->urg_ptr = htons(x);
556 } else
557 thp->urg = 0;
558 if(changes & NEW_W){
559 if((x = decode(&cp)) == -1) {
560 DPRINT(("uncomp: bad W\n"));
561 goto bad;
563 thp->window = htons( ntohs(thp->window) + x);
565 if(changes & NEW_A){
566 if((x = decode(&cp)) == -1) {
567 DPRINT(("uncomp: bad A\n"));
568 goto bad;
570 thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
572 if(changes & NEW_S){
573 if((x = decode(&cp)) == -1) {
574 DPRINT(("uncomp: bad S\n"));
575 goto bad;
577 thp->seq = htonl( ntohl(thp->seq) + x);
579 break;
581 if(changes & NEW_I){
582 if((x = decode(&cp)) == -1) {
583 DPRINT(("uncomp: bad I\n"));
584 goto bad;
586 ip->id = htons (ntohs (ip->id) + x);
587 } else
588 ip->id = htons (ntohs (ip->id) + 1);
591 * At this point, cp points to the first byte of data in the
592 * packet. Put the reconstructed TCP and IP headers back on the
593 * packet. Recalculate IP checksum (but not TCP checksum).
596 len = isize - (cp - icp);
597 if (len < 0)
598 goto bad;
599 len += hdrlen;
600 ip->tot_len = htons(len);
601 ip->check = 0;
603 DPRINT(("uncomp: %d %d %d %d\n", cp - icp, hdrlen, isize, len));
605 memmove(icp + hdrlen, cp, len - hdrlen);
607 cp = icp;
608 memcpy(cp, ip, 20);
609 cp += 20;
611 if (ip->ihl > 5) {
612 memcpy(cp, cs->cs_ipopt, ((ip->ihl) - 5) * 4);
613 cp += ((ip->ihl) - 5) * 4;
616 ((struct iphdr *)icp)->check = ip_csum((struct iphdr*)icp);
618 memcpy(cp, thp, 20);
619 cp += 20;
621 if (thp->doff > 5) {
622 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
623 cp += ((thp->doff) - 5) * 4;
626 if (inet_debug == DBG_SLIP) printk("\runcomp: change %x len %d\n", changes, len);
627 return len;
628 bad:
629 comp->sls_i_error++;
630 return slhc_toss( comp );
635 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
637 register struct cstate *cs;
638 short ip_len;
639 struct iphdr *ip;
640 struct tcphdr *thp;
642 unsigned char index;
644 if(isize < 20) {
645 /* The packet is shorter than a legal IP header */
646 comp->sls_i_runt++;
647 return slhc_toss( comp );
649 /* Sneak a peek at the IP header's IHL field to find its length */
650 ip_len = (icp[0] & 0xf) << 2;
651 if(ip_len < 20){
652 /* The IP header length field is too small */
653 comp->sls_i_runt++;
654 return slhc_toss( comp );
656 index = icp[9];
657 icp[9] = IPPROTO_TCP;
658 ip = (struct iphdr *) icp;
660 if (ip_csum(ip)) {
661 /* Bad IP header checksum; discard */
662 comp->sls_i_badcheck++;
663 return slhc_toss( comp );
665 thp = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
666 if(index > comp->rslot_limit) {
667 comp->sls_i_error++;
668 return slhc_toss(comp);
671 /* Update local state */
672 cs = &comp->rstate[comp->recv_current = index];
673 comp->flags &=~ SLF_TOSS;
674 memcpy(&cs->cs_ip,ip,20);
675 memcpy(&cs->cs_tcp,thp,20);
676 if (ip->ihl > 5)
677 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
678 if (thp->doff > 5)
679 memcpy(cs->cs_tcpopt, thp+1, ((thp->doff) - 5) * 4);
680 cs->cs_hsize = ip->ihl*2 + thp->doff*2;
681 /* Put headers back on packet
682 * Neither header checksum is recalculated
684 comp->sls_i_uncompressed++;
685 return isize;
690 slhc_toss(struct slcompress *comp)
692 if ( comp == NULLSLCOMPR )
693 return 0;
695 comp->flags |= SLF_TOSS;
696 return 0;
700 void slhc_i_status(struct slcompress *comp)
702 if (comp != NULLSLCOMPR) {
703 printk("\t%ld Cmp, %ld Uncmp, %ld Bad, %ld Tossed\n",
704 comp->sls_i_compressed,
705 comp->sls_i_uncompressed,
706 comp->sls_i_error,
707 comp->sls_i_tossed);
712 void slhc_o_status(struct slcompress *comp)
714 if (comp != NULLSLCOMPR) {
715 printk("\t%ld Cmp, %ld Uncmp, %ld AsIs, %ld NotTCP\n",
716 comp->sls_o_compressed,
717 comp->sls_o_uncompressed,
718 comp->sls_o_tcp,
719 comp->sls_o_nontcp);
720 printk("\t%10ld Searches, %10ld Misses\n",
721 comp->sls_o_searches,
722 comp->sls_o_misses);