Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2)  * Routines to compress and uncompress tcp packets (for transmission
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * over low speed serial lines).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (c) 1989 Regents of the University of California.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * Redistribution and use in source and binary forms are permitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * provided that the above copyright notice and this paragraph are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * duplicated in all such forms and that any documentation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * advertising materials, and other materials related to such
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * distribution and use acknowledge that the software was developed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * by the University of California, Berkeley.  The name of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * University may not be used to endorse or promote products derived
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * from this software without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *	Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *	- Initial distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * modified for KA9Q Internet Software Package by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Katie Stevens (dkstevens@ucdavis.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * University of California, Davis
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * Computing Services
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  *	- 01-31-90	initial adaptation (from 1.19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  *	PPP.05	02-15-90 [ks]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  *	PPP.08	05-02-90 [ks]	use PPP protocol field to signal compression
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  *	PPP.15	09-90	 [ks]	improve mbuf handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *	PPP.16	11-02	 [karn]	substantially rewritten to use NOS facilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  *	- Feb 1991	Bill_Simpson@um.cc.umich.edu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  *			variable number of conversation slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  *			allow zero or one slots
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *			separate routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *			status display
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *	- Jul 1994	Dmitry Gorodchanin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *			Fixes for memory leaks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  *      - Oct 1994      Dmitry Gorodchanin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *                      Modularization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *	- Jan 1995	Bjorn Ekwall
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *			Use ip_fast_csum from ip.h
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *	- July 1995	Christos A. Polyzols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *			Spotted bug in tcp option checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	This module is a difficult issue. It's clearly inet code but it's also clearly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  *	driver code belonging close to PPP and SLIP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) #include <net/slhc_vj.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) #ifdef CONFIG_INET
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) /* Entire module is for IP only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #include <linux/socket.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) #include <linux/sockios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) #include <linux/termios.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) #include <linux/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) #include <linux/inet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) #include <net/ip.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) #include <net/protocol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) #include <net/icmp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) #include <net/tcp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) #include <net/checksum.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) static unsigned char *encode(unsigned char *cp, unsigned short n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) static long decode(unsigned char **cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static unsigned char * put16(unsigned char *cp, unsigned short x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static unsigned short pull16(unsigned char **cpp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) /* Allocate compression data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *	slots must be in range 0 to 255 (zero meaning no compression)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  * Returns pointer to structure or ERR_PTR() on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) struct slcompress *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) slhc_init(int rslots, int tslots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	short i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct cstate *ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	struct slcompress *comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (rslots < 0 || rslots > 255 || tslots < 0 || tslots > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	comp = kzalloc(sizeof(struct slcompress), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (! comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (rslots > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		size_t rsize = rslots * sizeof(struct cstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		comp->rstate = kzalloc(rsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		if (! comp->rstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 			goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		comp->rslot_limit = rslots - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (tslots > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		size_t tsize = tslots * sizeof(struct cstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		comp->tstate = kzalloc(tsize, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		if (! comp->tstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 			goto out_free2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		comp->tslot_limit = tslots - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	comp->xmit_oldest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	comp->xmit_current = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	comp->recv_current = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	 * don't accept any packets with implicit index until we get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	 * one with an explicit index.  Otherwise the uncompress code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	 * will try to use connection 255, which is almost certainly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	 * out of range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	comp->flags |= SLF_TOSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if ( tslots > 0 ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		ts = comp->tstate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		for(i = comp->tslot_limit; i > 0; --i){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			ts[i].cs_this = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 			ts[i].next = &(ts[i - 1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		ts[0].next = &(ts[comp->tslot_limit]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		ts[0].cs_this = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	return comp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) out_free2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	kfree(comp->rstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	kfree(comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) /* Free a compression data structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) slhc_free(struct slcompress *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if ( IS_ERR_OR_NULL(comp) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	if ( comp->tstate != NULLSLSTATE )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		kfree( comp->tstate );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	if ( comp->rstate != NULLSLSTATE )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		kfree( comp->rstate );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	kfree( comp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Put a short in host order into a char array in network order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static inline unsigned char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) put16(unsigned char *cp, unsigned short x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	*cp++ = x >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	*cp++ = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Encode a number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static unsigned char *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) encode(unsigned char *cp, unsigned short n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if(n >= 256 || n == 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		*cp++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		cp = put16(cp,n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		*cp++ = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	return cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* Pull a 16-bit integer in host order from buffer in network byte order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static unsigned short
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) pull16(unsigned char **cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	short rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	rval = *(*cpp)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	rval <<= 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	rval |= *(*cpp)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	return rval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) /* Decode a number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) decode(unsigned char **cpp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	int x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	x = *(*cpp)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if(x == 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return pull16(cpp) & 0xffff;	/* pull16 returns -1 on error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		return x & 0xff;		/* -1 if PULLCHAR returned error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * icp and isize are the original packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  * ocp is a place to put a copy if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)  * cpp is initially a pointer to icp.  If the copy is used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)  *    change it to ocp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	unsigned char *ocp, unsigned char **cpp, int compress_cid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	struct cstate *ocs = &(comp->tstate[comp->xmit_oldest]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	struct cstate *lcs = ocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	struct cstate *cs = lcs->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	unsigned long deltaS, deltaA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	short changes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	int nlen, hlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	unsigned char new_seq[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	unsigned char *cp = new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	struct iphdr *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	struct tcphdr *th, *oth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	__sum16 csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 *	Don't play with runt packets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if(isize<sizeof(struct iphdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	ip = (struct iphdr *) icp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (ip->version != 4 || ip->ihl < 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	/* Bail if this packet isn't TCP, or is an IP fragment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (ip->protocol != IPPROTO_TCP || (ntohs(ip->frag_off) & 0x3fff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		/* Send as regular IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		if(ip->protocol != IPPROTO_TCP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			comp->sls_o_nontcp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			comp->sls_o_tcp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	nlen = ip->ihl * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if (isize < nlen + sizeof(*th))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	th = (struct tcphdr *)(icp + nlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	if (th->doff < sizeof(struct tcphdr) / 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	hlen = nlen + th->doff * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	/*  Bail if the TCP packet isn't `compressible' (i.e., ACK isn't set or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	 *  some other control bit is set). Also uncompressible if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	 *  it's a runt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	if(hlen > isize || th->syn || th->fin || th->rst ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	    ! (th->ack)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		/* TCP connection stuff; send as regular IP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		comp->sls_o_tcp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	 * Packet is compressible -- we're going to send either a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet.  Either way,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	 * we need to locate (or create) the connection state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	 * States are kept in a circularly linked list with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	 * xmit_oldest pointing to the end of the list.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	 * list is kept in lru order by moving a state to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	 * head of the list whenever it is referenced.  Since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	 * the list is short and, empirically, the connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	 * we want is almost always near the front, we locate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	 * states via linear search.  If we don't find a state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	 * for the datagram, the oldest state is (re-)used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	for ( ; ; ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		if( ip->saddr == cs->cs_ip.saddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		 && ip->daddr == cs->cs_ip.daddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		 && th->source == cs->cs_tcp.source
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 		 && th->dest == cs->cs_tcp.dest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 			goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		/* if current equal oldest, at end of list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		if ( cs == ocs )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		lcs = cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		cs = cs->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		comp->sls_o_searches++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * Didn't find it -- re-use oldest cstate.  Send an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 * uncompressed packet that tells the other side what
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	 * connection number we're using for this conversation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 * Note that since the state list is circular, the oldest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	 * state points to the newest and we only need to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	 * xmit_oldest to update the lru linkage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	comp->sls_o_misses++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	comp->xmit_oldest = lcs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	 * Found it -- move to the front on the connection list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	if(lcs == ocs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  		/* found at most recently used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	} else if (cs == ocs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		/* found at least recently used */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		comp->xmit_oldest = lcs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		/* more than 2 elements */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		lcs->next = cs->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		cs->next = ocs->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		ocs->next = cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	 * Make sure that only what we expect to change changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 * Check the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	 * IP protocol version, header length & type of service.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	 * The "Don't fragment" bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	 * The time-to-live field.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	 * The TCP header length.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	 * IP options, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	 * TCP options, if any.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	 * If any of these things are different between the previous &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	 * current datagram, we send the current datagram `uncompressed'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	oth = &cs->cs_tcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if(ip->version != cs->cs_ip.version || ip->ihl != cs->cs_ip.ihl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	 || ip->tos != cs->cs_ip.tos
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	 || (ip->frag_off & htons(0x4000)) != (cs->cs_ip.frag_off & htons(0x4000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	 || ip->ttl != cs->cs_ip.ttl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	 || th->doff != cs->cs_tcp.doff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	 || (ip->ihl > 5 && memcmp(ip+1,cs->cs_ipopt,((ip->ihl)-5)*4) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	 || (th->doff > 5 && memcmp(th+1,cs->cs_tcpopt,((th->doff)-5)*4) != 0)){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	 * Figure out which of the changing fields changed.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	 * receiver expects changes in the order: urgent, window,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	 * ack, seq (the order minimizes the number of temporaries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	 * needed in this section of code).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	if(th->urg){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 		deltaS = ntohs(th->urg_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		cp = encode(cp,deltaS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		changes |= NEW_U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	} else if(th->urg_ptr != oth->urg_ptr){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		/* argh! URG not set but urp changed -- a sensible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		 * implementation should never do this but RFC793
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		 * doesn't prohibit the change so we have to deal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		 * with it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if((deltaS = ntohs(th->window) - ntohs(oth->window)) != 0){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		cp = encode(cp,deltaS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		changes |= NEW_W;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	if((deltaA = ntohl(th->ack_seq) - ntohl(oth->ack_seq)) != 0L){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		if(deltaA > 0x0000ffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		cp = encode(cp,deltaA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		changes |= NEW_A;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if((deltaS = ntohl(th->seq) - ntohl(oth->seq)) != 0L){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		if(deltaS > 0x0000ffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 			goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		cp = encode(cp,deltaS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		changes |= NEW_S;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	switch(changes){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	case 0:	/* Nothing changed. If this packet contains data and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		 * last one didn't, this is probably a data packet following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		 * an ack (normal on an interactive connection) and we send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		 * it compressed.  Otherwise it's probably a retransmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		 * retransmitted ack or window probe.  Send it uncompressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		 * in case the other side missed the compressed version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if(ip->tot_len != cs->cs_ip.tot_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		   ntohs(cs->cs_ip.tot_len) == hlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	case SPECIAL_I:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	case SPECIAL_D:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		/* actual changes match one of our special case encodings --
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		 * send packet uncompressed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		goto uncompressed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	case NEW_S|NEW_A:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		if(deltaS == deltaA &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 		    deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 			/* special case for echoed terminal traffic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			changes = SPECIAL_I;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			cp = new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	case NEW_S:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		if(deltaS == ntohs(cs->cs_ip.tot_len) - hlen){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 			/* special case for data xfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			changes = SPECIAL_D;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			cp = new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	deltaS = ntohs(ip->id) - ntohs(cs->cs_ip.id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if(deltaS != 1){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		cp = encode(cp,deltaS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		changes |= NEW_I;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if(th->psh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		changes |= TCP_PUSH_BIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	/* Grab the cksum before we overwrite it below.  Then update our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	 * state with this packet's header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	csum = th->check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	memcpy(&cs->cs_ip,ip,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	memcpy(&cs->cs_tcp,th,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	/* We want to use the original packet as our compressed packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	 * (cp - new_seq) is the number of bytes we need for compressed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	 * sequence numbers.  In addition we need one byte for the change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * mask, one for the connection id and two for the tcp checksum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 * So, (cp - new_seq) + 4 bytes of header are needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	deltaS = cp - new_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if(compress_cid == 0 || comp->xmit_current != cs->cs_this){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		cp = ocp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		*cpp = ocp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		*cp++ = changes | NEW_C;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		*cp++ = cs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		comp->xmit_current = cs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		cp = ocp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		*cpp = ocp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		*cp++ = changes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	*(__sum16 *)cp = csum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) /* deltaS is now the size of the change section of the compressed header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	memcpy(cp,new_seq,deltaS);	/* Write list of deltas */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	memcpy(cp+deltaS,icp+hlen,isize-hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	comp->sls_o_compressed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	ocp[0] |= SL_TYPE_COMPRESSED_TCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	return isize - hlen + deltaS + (cp - ocp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	/* Update connection state cs & send uncompressed packet (i.e.,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	 * a regular ip/tcp packet but with the 'conversation id' we hope
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	 * to use on future compressed packets in the protocol field).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) uncompressed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	memcpy(&cs->cs_ip,ip,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	memcpy(&cs->cs_tcp,th,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	if (ip->ihl > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	  memcpy(cs->cs_ipopt, ip+1, ((ip->ihl) - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (th->doff > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	  memcpy(cs->cs_tcpopt, th+1, ((th->doff) - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	comp->xmit_current = cs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	comp->sls_o_uncompressed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	memcpy(ocp, icp, isize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	*cpp = ocp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	ocp[9] = cs->cs_this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	ocp[0] |= SL_TYPE_UNCOMPRESSED_TCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	int changes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	long x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	struct tcphdr *thp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct iphdr *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	struct cstate *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	int len, hdrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	unsigned char *cp = icp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	/* We've got a compressed packet; read the change byte */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	comp->sls_i_compressed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	if(isize < 3){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		comp->sls_i_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	changes = *cp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	if(changes & NEW_C){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		/* Make sure the state index is in range, then grab the state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 		 * If we have a good state index, clear the 'discard' flag.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		x = *cp++;	/* Read conn index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		if(x < 0 || x > comp->rslot_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		/* Check if the cstate is initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (!comp->rstate[x].initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		comp->flags &=~ SLF_TOSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		comp->recv_current = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		/* this packet has an implicit state index.  If we've
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 		 * had a line error since the last time we got an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		 * explicit state index, we have to toss the packet. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 		if(comp->flags & SLF_TOSS){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 			comp->sls_i_tossed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	cs = &comp->rstate[comp->recv_current];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	thp = &cs->cs_tcp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	ip = &cs->cs_ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	thp->check = *(__sum16 *)cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	cp += 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	thp->psh = (changes & TCP_PUSH_BIT) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  * we can use the same number for the length of the saved header and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)  * the current one, because the packet wouldn't have been sent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  * as compressed unless the options were the same as the previous one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	hdrlen = ip->ihl * 4 + thp->doff * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	switch(changes & SPECIALS_MASK){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	case SPECIAL_I:		/* Echoed terminal traffic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		short i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		i = ntohs(ip->tot_len) - hdrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		thp->ack_seq = htonl( ntohl(thp->ack_seq) + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 		thp->seq = htonl( ntohl(thp->seq) + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	case SPECIAL_D:			/* Unidirectional data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		thp->seq = htonl( ntohl(thp->seq) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 				  ntohs(ip->tot_len) - hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		if(changes & NEW_U){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 			thp->urg = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			if((x = decode(&cp)) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 			thp->urg_ptr = htons(x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			thp->urg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		if(changes & NEW_W){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			if((x = decode(&cp)) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			thp->window = htons( ntohs(thp->window) + x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		if(changes & NEW_A){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			if((x = decode(&cp)) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 			thp->ack_seq = htonl( ntohl(thp->ack_seq) + x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		if(changes & NEW_S){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			if((x = decode(&cp)) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 				goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			thp->seq = htonl( ntohl(thp->seq) + x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	if(changes & NEW_I){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		if((x = decode(&cp)) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 		ip->id = htons (ntohs (ip->id) + x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		ip->id = htons (ntohs (ip->id) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	 * At this point, cp points to the first byte of data in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	 * packet.  Put the reconstructed TCP and IP headers back on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	 * packet.  Recalculate IP checksum (but not TCP checksum).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	len = isize - (cp - icp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 	if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 		goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	len += hdrlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	ip->tot_len = htons(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 	ip->check = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	memmove(icp + hdrlen, cp, len - hdrlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	cp = icp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	memcpy(cp, ip, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	cp += 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	if (ip->ihl > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	  memcpy(cp, cs->cs_ipopt, (ip->ihl - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	  cp += (ip->ihl - 5) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	put_unaligned(ip_fast_csum(icp, ip->ihl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 		      &((struct iphdr *)icp)->check);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 	memcpy(cp, thp, 20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	cp += 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (thp->doff > 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 	  memcpy(cp, cs->cs_tcpopt, ((thp->doff) - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	  cp += ((thp->doff) - 5) * 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	comp->sls_i_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	return slhc_toss( comp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 	struct cstate *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 	unsigned ihl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	unsigned char index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	if(isize < 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 		/* The packet is shorter than a legal IP header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 		comp->sls_i_runt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 		return slhc_toss( comp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	/* Peek at the IP header's IHL field to find its length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	ihl = icp[0] & 0xf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	if(ihl < 20 / 4){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		/* The IP header length field is too small */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		comp->sls_i_runt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 		return slhc_toss( comp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	index = icp[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	icp[9] = IPPROTO_TCP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	if (ip_fast_csum(icp, ihl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 		/* Bad IP header checksum; discard */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		comp->sls_i_badcheck++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 		return slhc_toss( comp );
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 	if(index > comp->rslot_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 		comp->sls_i_error++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		return slhc_toss(comp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	/* Update local state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	cs = &comp->rstate[comp->recv_current = index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	comp->flags &=~ SLF_TOSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	memcpy(&cs->cs_ip,icp,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	memcpy(&cs->cs_tcp,icp + ihl*4,20);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (ihl > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	  memcpy(cs->cs_ipopt, icp + sizeof(struct iphdr), (ihl - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	if (cs->cs_tcp.doff > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	  memcpy(cs->cs_tcpopt, icp + ihl*4 + sizeof(struct tcphdr), (cs->cs_tcp.doff - 5) * 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	cs->cs_hsize = ihl*2 + cs->cs_tcp.doff*2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	cs->initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	/* Put headers back on packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	 * Neither header checksum is recalculated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	comp->sls_i_uncompressed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	return isize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) slhc_toss(struct slcompress *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	if ( comp == NULLSLCOMPR )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	comp->flags |= SLF_TOSS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) #else /* CONFIG_INET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) slhc_toss(struct slcompress *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_toss");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)   return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) slhc_uncompress(struct slcompress *comp, unsigned char *icp, int isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_uncompress");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)   return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) slhc_compress(struct slcompress *comp, unsigned char *icp, int isize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	unsigned char *ocp, unsigned char **cpp, int compress_cid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_compress");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)   return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) slhc_remember(struct slcompress *comp, unsigned char *icp, int isize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_remember");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)   return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) slhc_free(struct slcompress *comp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_free");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) struct slcompress *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) slhc_init(int rslots, int tslots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)   printk(KERN_DEBUG "Called IP function on non IP-system: slhc_init");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)   return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) #endif /* CONFIG_INET */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) /* VJ header compression */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) EXPORT_SYMBOL(slhc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) EXPORT_SYMBOL(slhc_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) EXPORT_SYMBOL(slhc_remember);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) EXPORT_SYMBOL(slhc_compress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) EXPORT_SYMBOL(slhc_uncompress);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) EXPORT_SYMBOL(slhc_toss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) MODULE_LICENSE("Dual BSD/GPL");