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