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.
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.
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
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
39 * - Jul 1994 Dmitry Gorodchanin
40 * Fixes for memory leaks.
41 * - Oct 1994 Dmitry Gorodchanin
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
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
53 #include <linux/module.h>
54 #include <linux/types.h>
55 #include <linux/string.h>
56 #include <linux/errno.h>
57 #include <linux/kernel.h>
58 #include <net/slhc_vj.h>
61 /* Entire module is for IP only */
63 #include <linux/socket.h>
64 #include <linux/sockios.h>
65 #include <linux/termios.h>
67 #include <linux/fcntl.h>
68 #include <linux/inet.h>
69 #include <linux/netdevice.h>
71 #include <net/protocol.h>
74 #include <linux/skbuff.h>
76 #include <linux/timer.h>
77 #include <asm/system.h>
78 #include <asm/uaccess.h>
79 #include <net/checksum.h>
80 #include <asm/unaligned.h>
82 static unsigned char *encode(unsigned char *cp, unsigned short n);
83 static long decode(unsigned char **cpp);
84 static unsigned char * put16(unsigned char *cp, unsigned short x);
85 static unsigned short pull16(unsigned char **cpp);
87 /* Initialize compression data structure
88 * slots must be in range 0 to 255 (zero meaning no compression)
91 slhc_init(int rslots, int tslots)
94 register struct cstate *ts;
95 struct slcompress *comp;
97 comp = kzalloc(sizeof(struct slcompress), GFP_KERNEL);
101 if ( rslots > 0 && rslots < 256 ) {
102 size_t rsize = rslots * sizeof(struct cstate);
103 comp->rstate = kzalloc(rsize, GFP_KERNEL);
106 comp->rslot_limit = rslots - 1;
109 if ( tslots > 0 && tslots < 256 ) {
110 size_t tsize = tslots * sizeof(struct cstate);
111 comp->tstate = kzalloc(tsize, GFP_KERNEL);
114 comp->tslot_limit = tslots - 1;
117 comp->xmit_oldest = 0;
118 comp->xmit_current = 255;
119 comp->recv_current = 255;
121 * don't accept any packets with implicit index until we get
122 * one with an explicit index. Otherwise the uncompress code
123 * will try to use connection 255, which is almost certainly
126 comp->flags |= SLF_TOSS;
130 for(i = comp->tslot_limit; i > 0; --i){
132 ts[i].next = &(ts[i - 1]);
134 ts[0].next = &(ts[comp->tslot_limit]);
148 /* Free a compression data structure */
150 slhc_free(struct slcompress *comp)
152 if ( comp == NULLSLCOMPR )
155 if ( comp->tstate != NULLSLSTATE )
156 kfree( comp->tstate );
158 if ( comp->rstate != NULLSLSTATE )
159 kfree( comp->rstate );
165 /* Put a short in host order into a char array in network order */
166 static inline unsigned char *
167 put16(unsigned char *cp, unsigned short x)
176 /* Encode a number */
178 encode(unsigned char *cp, unsigned short n)
180 if(n >= 256 || n == 0){
189 /* Pull a 16-bit integer in host order from buffer in network byte order */
190 static unsigned short
191 pull16(unsigned char **cpp)
201 /* Decode a number */
203 decode(unsigned char **cpp)
209 return pull16(cpp) & 0xffff; /* pull16 returns -1 on error */
211 return x & 0xff; /* -1 if PULLCHAR returned error */
216 * icp and isize are the original packet.
217 * ocp is a place to put a copy if necessary.
218 * cpp is initially a pointer to icp. If the copy is used,
223 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
224 unsigned char *ocp, unsigned char **cpp, int compress_cid)
226 register struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
227 register struct cstate *lcs = ocs;
228 register struct cstate *cs = lcs->next;
229 register unsigned long deltaS, deltaA;
230 register short changes = 0;
232 unsigned char new_seq[16];
233 register unsigned char *cp = new_seq;
235 struct tcphdr *th, *oth;
239 * Don't play with runt packets.
242 if(isize<sizeof(struct iphdr))
245 ip = (struct iphdr *) icp;
247 /* Bail if this packet isn't TCP, or is an IP fragment */
248 if (ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x3fff)) {
249 /* Send as regular IP */
250 if(ip->protocol != IPPROTO_TCP)
251 comp->sls_o_nontcp++;
256 /* Extract TCP header */
258 th = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
259 hlen = ip->ihl*4 + th->doff*4;
261 /* Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
262 * some other control bit is set). Also uncompressible if
265 if(hlen > isize || th->syn || th->fin || th->rst ||
267 /* TCP connection stuff; send as regular IP */
272 * Packet is compressible -- we're going to send either a
273 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way,
274 * we need to locate (or create) the connection state.
276 * States are kept in a circularly linked list with
277 * xmit_oldest pointing to the end of the list. The
278 * list is kept in lru order by moving a state to the
279 * head of the list whenever it is referenced. Since
280 * the list is short and, empirically, the connection
281 * we want is almost always near the front, we locate
282 * states via linear search. If we don't find a state
283 * for the datagram, the oldest state is (re-)used.
286 if( ip->saddr == cs->cs_ip.saddr
287 && ip->daddr == cs->cs_ip.daddr
288 && th->source == cs->cs_tcp.source
289 && th->dest == cs->cs_tcp.dest)
292 /* if current equal oldest, at end of list */
297 comp->sls_o_searches++;
300 * Didn't find it -- re-use oldest cstate. Send an
301 * uncompressed packet that tells the other side what
302 * connection number we're using for this conversation.
304 * Note that since the state list is circular, the oldest
305 * state points to the newest and we only need to set
306 * xmit_oldest to update the lru linkage.
308 comp->sls_o_misses++;
309 comp->xmit_oldest = lcs->cs_this;
314 * Found it -- move to the front on the connection list.
317 /* found at most recently used */
318 } else if (cs == ocs) {
319 /* found at least recently used */
320 comp->xmit_oldest = lcs->cs_this;
322 /* more than 2 elements */
323 lcs->next = cs->next;
324 cs->next = ocs->next;
329 * Make sure that only what we expect to change changed.
330 * Check the following:
331 * IP protocol version, header length & type of service.
332 * The "Don't fragment" bit.
333 * The time-to-live field.
334 * The TCP header length.
335 * IP options, if any.
336 * TCP options, if any.
337 * If any of these things are different between the previous &
338 * current datagram, we send the current datagram `uncompressed'.
342 if(ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
343 || ip->tos != cs->cs_ip.tos
344 || (ip->frag_off & htons(0x4000)) != (cs->cs_ip.frag_off & htons(0x4000))
345 || ip->ttl != cs->cs_ip.ttl
346 || th->doff != cs->cs_tcp.doff
347 || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
348 || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4) != 0)){
353 * Figure out which of the changing fields changed. The
354 * receiver expects changes in the order: urgent, window,
355 * ack, seq (the order minimizes the number of temporaries
356 * needed in this section of code).
359 deltaS = ntohs(th->urg_ptr);
360 cp = encode(cp,deltaS);
362 } else if(th->urg_ptr != oth->urg_ptr){
363 /* argh! URG not set but urp changed -- a sensible
364 * implementation should never do this but RFC793
365 * doesn't prohibit the change so we have to deal
369 if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
370 cp = encode(cp,deltaS);
373 if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
374 if(deltaA > 0x0000ffff)
376 cp = encode(cp,deltaA);
379 if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
380 if(deltaS > 0x0000ffff)
382 cp = encode(cp,deltaS);
387 case 0: /* Nothing changed. If this packet contains data and the
388 * last one didn't, this is probably a data packet following
389 * an ack (normal on an interactive connection) and we send
390 * it compressed. Otherwise it's probably a retransmit,
391 * retransmitted ack or window probe. Send it uncompressed
392 * in case the other side missed the compressed version.
394 if(ip->tot_len != cs->cs_ip.tot_len &&
395 ntohs(cs->cs_ip.tot_len) == hlen)
401 /* actual changes match one of our special case encodings --
402 * send packet uncompressed.
406 if(deltaS == deltaA &&
407 deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
408 /* special case for echoed terminal traffic */
414 if(deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
415 /* special case for data xfer */
421 deltaS = ntohs(ip->id) - ntohs(cs->cs_ip.id);
423 cp = encode(cp,deltaS);
427 changes |= TCP_PUSH_BIT;
428 /* Grab the cksum before we overwrite it below. Then update our
429 * state with this packet's header.
431 deltaA = ntohs(th->check);
432 memcpy(&cs->cs_ip,ip,20);
433 memcpy(&cs->cs_tcp,th,20);
434 /* We want to use the original packet as our compressed packet.
435 * (cp - new_seq) is the number of bytes we need for compressed
436 * sequence numbers. In addition we need one byte for the change
437 * mask, one for the connection id and two for the tcp checksum.
438 * So, (cp - new_seq) + 4 bytes of header are needed.
440 deltaS = cp - new_seq;
441 if(compress_cid == 0 || comp->xmit_current != cs->cs_this){
444 *cp++ = changes | NEW_C;
446 comp->xmit_current = cs->cs_this;
452 cp = put16(cp,(short)deltaA); /* Write TCP checksum */
453 /* deltaS is now the size of the change section of the compressed header */
454 memcpy(cp,new_seq,deltaS); /* Write list of deltas */
455 memcpy(cp+deltaS,icp+hlen,isize-hlen);
456 comp->sls_o_compressed++;
457 ocp[0] |= SL_TYPE_COMPRESSED_TCP;
458 return isize - hlen + deltaS + (cp - ocp);
460 /* Update connection state cs & send uncompressed packet (i.e.,
461 * a regular ip/tcp packet but with the 'conversation id' we hope
462 * to use on future compressed packets in the protocol field).
465 memcpy(&cs->cs_ip,ip,20);
466 memcpy(&cs->cs_tcp,th,20);
468 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
470 memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
471 comp->xmit_current = cs->cs_this;
472 comp->sls_o_uncompressed++;
473 memcpy(ocp, icp, isize);
475 ocp[9] = cs->cs_this;
476 ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
482 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
484 register int changes;
486 register struct tcphdr *thp;
487 register struct iphdr *ip;
488 register struct cstate *cs;
490 unsigned char *cp = icp;
492 /* We've got a compressed packet; read the change byte */
493 comp->sls_i_compressed++;
500 /* Make sure the state index is in range, then grab the state.
501 * If we have a good state index, clear the 'discard' flag.
503 x = *cp++; /* Read conn index */
504 if(x < 0 || x > comp->rslot_limit)
507 comp->flags &=~ SLF_TOSS;
508 comp->recv_current = x;
510 /* this packet has an implicit state index. If we've
511 * had a line error since the last time we got an
512 * explicit state index, we have to toss the packet. */
513 if(comp->flags & SLF_TOSS){
514 comp->sls_i_tossed++;
518 cs = &comp->rstate[comp->recv_current];
522 if((x = pull16(&cp)) == -1) { /* Read the TCP checksum */
525 thp->check = htons(x);
527 thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
529 * we can use the same number for the length of the saved header and
530 * the current one, because the packet wouldn't have been sent
531 * as compressed unless the options were the same as the previous one
534 hdrlen = ip->ihl * 4 + thp->doff * 4;
536 switch(changes & SPECIALS_MASK){
537 case SPECIAL_I: /* Echoed terminal traffic */
540 i = ntohs(ip->tot_len) - hdrlen;
541 thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
542 thp->seq = htonl( ntohl(thp->seq) + i);
546 case SPECIAL_D: /* Unidirectional data */
547 thp->seq = htonl( ntohl(thp->seq) +
548 ntohs(ip->tot_len) - hdrlen);
554 if((x = decode(&cp)) == -1) {
557 thp->urg_ptr = htons(x);
561 if((x = decode(&cp)) == -1) {
564 thp->window = htons( ntohs(thp->window) + x);
567 if((x = decode(&cp)) == -1) {
570 thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
573 if((x = decode(&cp)) == -1) {
576 thp->seq = htonl( ntohl(thp->seq) + x);
581 if((x = decode(&cp)) == -1) {
584 ip->id = htons (ntohs (ip->id) + x);
586 ip->id = htons (ntohs (ip->id) + 1);
589 * At this point, cp points to the first byte of data in the
590 * packet. Put the reconstructed TCP and IP headers back on the
591 * packet. Recalculate IP checksum (but not TCP checksum).
594 len = isize - (cp - icp);
598 ip->tot_len = htons(len);
601 memmove(icp + hdrlen, cp, len - hdrlen);
608 memcpy(cp, cs->cs_ipopt, (ip->ihl - 5) * 4);
609 cp += (ip->ihl - 5) * 4;
612 put_unaligned(ip_fast_csum(icp, ip->ihl),
613 &((struct iphdr *)icp)->check);
619 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
620 cp += ((thp->doff) - 5) * 4;
626 return slhc_toss( comp );
631 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
633 register struct cstate *cs;
639 /* The packet is shorter than a legal IP header */
641 return slhc_toss( comp );
643 /* Peek at the IP header's IHL field to find its length */
646 /* The IP header length field is too small */
648 return slhc_toss( comp );
651 icp[9] = IPPROTO_TCP;
653 if (ip_fast_csum(icp, ihl)) {
654 /* Bad IP header checksum; discard */
655 comp->sls_i_badcheck++;
656 return slhc_toss( comp );
658 if(index > comp->rslot_limit) {
660 return slhc_toss(comp);
663 /* Update local state */
664 cs = &comp->rstate[comp->recv_current = index];
665 comp->flags &=~ SLF_TOSS;
666 memcpy(&cs->cs_ip,icp,20);
667 memcpy(&cs->cs_tcp,icp + ihl*4,20);
669 memcpy(cs->cs_ipopt, icp + sizeof(struct iphdr), (ihl - 5) * 4);
670 if (cs->cs_tcp.doff > 5)
671 memcpy(cs->cs_tcpopt, icp + ihl*4 + sizeof(struct tcphdr), (cs->cs_tcp.doff - 5) * 4);
672 cs->cs_hsize = ihl*2 + cs->cs_tcp.doff*2;
673 /* Put headers back on packet
674 * Neither header checksum is recalculated
676 comp->sls_i_uncompressed++;
681 slhc_toss(struct slcompress *comp)
683 if ( comp == NULLSLCOMPR )
686 comp->flags |= SLF_TOSS;
691 /* VJ header compression */
692 EXPORT_SYMBOL(slhc_init);
693 EXPORT_SYMBOL(slhc_free);
694 EXPORT_SYMBOL(slhc_remember);
695 EXPORT_SYMBOL(slhc_compress);
696 EXPORT_SYMBOL(slhc_uncompress);
697 EXPORT_SYMBOL(slhc_toss);
699 #else /* CONFIG_INET */
703 slhc_toss(struct slcompress *comp)
705 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_toss");
709 slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
711 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_uncompress");
715 slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
716 unsigned char *ocp, unsigned char **cpp, int compress_cid)
718 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_compress");
723 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
725 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_remember");
730 slhc_free(struct slcompress *comp)
732 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_free");
736 slhc_init(int rslots, int tslots)
738 printk(KERN_DEBUG "Called IP function on non IP-system: slhc_init");
741 EXPORT_SYMBOL(slhc_init);
742 EXPORT_SYMBOL(slhc_free);
743 EXPORT_SYMBOL(slhc_remember);
744 EXPORT_SYMBOL(slhc_compress);
745 EXPORT_SYMBOL(slhc_uncompress);
746 EXPORT_SYMBOL(slhc_toss);
748 #endif /* CONFIG_INET */
749 MODULE_LICENSE("Dual BSD/GPL");