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 *
46 *
47 * This module is a difficult issue. It's clearly inet code but it's also clearly
48 * driver code belonging close to PPP and SLIP
49 */
50
51 #include <linux/config.h>
52 #ifdef CONFIG_INET
53 /* Entire module is for IP only */
54 #ifdef MODULE
55 #include <linux/module.h>
56 #include <linux/version.h>
57 #endif
58
59 #include <linux/types.h>
60 #include <linux/sched.h>
61 #include <linux/mm.h>
62 #include <linux/string.h>
63 #include <linux/socket.h>
64 #include <linux/sockios.h>
65 #include <linux/termios.h>
66 #include <linux/in.h>
67 #include <linux/fcntl.h>
68 #include <linux/inet.h>
69 #include <linux/netdevice.h>
70 #include <net/ip.h>
71 #include <net/protocol.h>
72 #include <net/icmp.h>
73 #include <net/tcp.h>
74 #include <linux/skbuff.h>
75 #include <net/sock.h>
76 #include <linux/errno.h>
77 #include <linux/timer.h>
78 #include <asm/system.h>
79 #include <asm/segment.h>
80 #include <linux/mm.h>
81 #include <net/checksum.h>
82 #include "slhc.h"
83
84 int last_retran;
85
86 static unsigned char *encode(unsigned char *cp, unsigned short n);
87 static long decode(unsigned char **cpp);
88 static unsigned char * put16(unsigned char *cp, unsigned short x);
89 static unsigned short pull16(unsigned char **cpp);
90
91 /* Initialize compression data structure
92 * slots must be in range 0 to 255 (zero meaning no compression)
93 */
94 struct slcompress *
95 slhc_init(int rslots, int tslots)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
96 {
97 register short i;
98 register struct cstate *ts;
99 struct slcompress *comp;
100
101 comp = (struct slcompress *)kmalloc(sizeof(struct slcompress),
102 GFP_KERNEL);
103 if (! comp)
104 return NULL;
105
106 memset(comp, 0, sizeof(struct slcompress));
107
108 if ( rslots > 0 && rslots < 256 ) {
109 comp->rstate =
110 (struct cstate *)kmalloc(rslots * sizeof(struct cstate),
111 GFP_KERNEL);
112 if (! comp->rstate)
113 {
114 kfree((unsigned char *)comp);
115 return NULL;
116 }
117 memset(comp->rstate, 0, rslots * sizeof(struct cstate));
118 comp->rslot_limit = rslots - 1;
119 }
120
121 if ( tslots > 0 && tslots < 256 ) {
122 comp->tstate =
123 (struct cstate *)kmalloc(tslots * sizeof(struct cstate),
124 GFP_KERNEL);
125 if (! comp->tstate)
126 {
127 kfree((unsigned char *)comp->rstate);
128 kfree((unsigned char *)comp);
129 return NULL;
130 }
131 memset(comp->tstate, 0, rslots * sizeof(struct cstate));
132 comp->tslot_limit = tslots - 1;
133 }
134
135 comp->xmit_oldest = 0;
136 comp->xmit_current = 255;
137 comp->recv_current = 255;
138 /*
139 * don't accept any packets with implicit index until we get
140 * one with an explicit index. Otherwise the uncompress code
141 * will try to use connection 255, which is almost certainly
142 * out of range
143 */
144 comp->flags |= SLF_TOSS;
145
146 if ( tslots > 0 ) {
147 ts = comp->tstate;
148 for(i = comp->tslot_limit; i > 0; --i){
149 ts[i].cs_this = i;
150 ts[i].next = &(ts[i - 1]);
151 }
152 ts[0].next = &(ts[comp->tslot_limit]);
153 ts[0].cs_this = 0;
154 }
155 #ifdef MODULE
156 MOD_INC_USE_COUNT;
157 #endif
158 return comp;
159 }
160
161
162 /* Free a compression data structure */
163 void
164 slhc_free(struct slcompress *comp)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
165 {
166 if ( comp == NULLSLCOMPR )
167 return;
168
169 if ( comp->rstate != NULLSLSTATE )
170 kfree( comp->rstate );
171
172 if ( comp->tstate != NULLSLSTATE )
173 kfree( comp->tstate );
174
175 #ifdef MODULE
176 MOD_DEC_USE_COUNT;
177 #endif
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 ((struct iphdr *)icp)->check = ip_fast_csum(icp, ((struct iphdr*)icp)->ihl);
623
624 memcpy(cp, thp, 20);
625 cp += 20;
626
627 if (thp->doff > 5) {
628 memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
629 cp += ((thp->doff) - 5) * 4;
630 }
631
632 return len;
633 bad:
634 comp->sls_i_error++;
635 return slhc_toss( comp );
636 }
637
638
639 int
640 slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
641 {
642 register struct cstate *cs;
643 short ip_len;
644 struct iphdr *ip;
645 struct tcphdr *thp;
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 /* Sneak a peek at the IP header's IHL field to find its length */
655 ip_len = (icp[0] & 0xf) << 2;
656 if(ip_len < 20){
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 ip = (struct iphdr *) icp;
664
665 if (ip_fast_csum(icp, ip->ihl)) {
666 /* Bad IP header checksum; discard */
667 comp->sls_i_badcheck++;
668 return slhc_toss( comp );
669 }
670 thp = (struct tcphdr *)(((unsigned char *)ip) + ip->ihl*4);
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,ip,20);
680 memcpy(&cs->cs_tcp,thp,20);
681 if (ip->ihl > 5)
682 memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
683 if (thp->doff > 5)
684 memcpy(cs->cs_tcpopt, thp+1, ((thp->doff) - 5) * 4);
685 cs->cs_hsize = ip->ihl*2 + thp->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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
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 #ifdef MODULE
732 char kernel_version[] = UTS_RELEASE;
733
734 int init_module(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
735 {
736 printk("CSLIP: code copyright 1989 Regents of the University of California\n");
737 return 0;
738 }
739
740 void cleanup_module(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
741 {
742 if (MOD_IN_USE) {
743 printk("CSLIP: module in use, remove delayed");
744 }
745 return;
746 }
747 #endif /* MODULE */
748 #endif /* CONFIG_INET */