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