1 /* $NetBSD: slcompress.c,v 1.9 1994/06/29 06:36:48 cgd Exp $ */ 2 3 /* 4 * Copyright (c) 1989, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)slcompress.c 8.2 (Berkeley) 4/16/94 36 */ 37 38 /* 39 * Routines to compress and uncompess tcp packets (for transmission 40 * over low speed serial lines. 41 * 42 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: 43 * - Initial distribution. 44 */ 45 46 #include <sys/param.h> 47 #include <sys/mbuf.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_systm.h> 51 #include <netinet/ip.h> 52 #include <netinet/tcp.h> 53 54 #include <net/slcompress.h> 55 56 #ifndef SL_NO_STATS 57 #define INCR(counter) ++comp->counter; 58 #else 59 #define INCR(counter) 60 #endif 61 62 #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n)) 63 #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n)) 64 #ifndef KERNEL 65 #define ovbcopy bcopy 66 #endif 67 68 void 69 sl_compress_init(comp, max_state) 70 struct slcompress *comp; 71 int max_state; 72 { 73 register u_int i; 74 register struct cstate *tstate = comp->tstate; 75 76 if (max_state == -1) 77 max_state = MAX_STATES - 1; 78 bzero((char *)comp, sizeof(*comp)); 79 for (i = max_state; i > 0; --i) { 80 tstate[i].cs_id = i; 81 tstate[i].cs_next = &tstate[i - 1]; 82 } 83 tstate[0].cs_next = &tstate[max_state]; 84 tstate[0].cs_id = 0; 85 comp->last_cs = &tstate[0]; 86 comp->last_recv = 255; 87 comp->last_xmit = 255; 88 comp->flags = SLF_TOSS; 89 } 90 91 92 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ 93 * checks for zero (since zero has to be encoded in the long, 3 byte 94 * form). 95 */ 96 #define ENCODE(n) { \ 97 if ((u_short)(n) >= 256) { \ 98 *cp++ = 0; \ 99 cp[1] = (n); \ 100 cp[0] = (n) >> 8; \ 101 cp += 2; \ 102 } else { \ 103 *cp++ = (n); \ 104 } \ 105 } 106 #define ENCODEZ(n) { \ 107 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \ 108 *cp++ = 0; \ 109 cp[1] = (n); \ 110 cp[0] = (n) >> 8; \ 111 cp += 2; \ 112 } else { \ 113 *cp++ = (n); \ 114 } \ 115 } 116 117 #define DECODEL(f) { \ 118 if (*cp == 0) {\ 119 (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \ 120 cp += 3; \ 121 } else { \ 122 (f) = htonl(ntohl(f) + (u_long)*cp++); \ 123 } \ 124 } 125 126 #define DECODES(f) { \ 127 if (*cp == 0) {\ 128 (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \ 129 cp += 3; \ 130 } else { \ 131 (f) = htons(ntohs(f) + (u_long)*cp++); \ 132 } \ 133 } 134 135 #define DECODEU(f) { \ 136 if (*cp == 0) {\ 137 (f) = htons((cp[1] << 8) | cp[2]); \ 138 cp += 3; \ 139 } else { \ 140 (f) = htons((u_long)*cp++); \ 141 } \ 142 } 143 144 u_int 145 sl_compress_tcp(m, ip, comp, compress_cid) 146 struct mbuf *m; 147 register struct ip *ip; 148 struct slcompress *comp; 149 int compress_cid; 150 { 151 register struct cstate *cs = comp->last_cs->cs_next; 152 register u_int hlen = ip->ip_hl; 153 register struct tcphdr *oth; 154 register struct tcphdr *th; 155 register u_int deltaS, deltaA; 156 register u_int changes = 0; 157 u_char new_seq[16]; 158 register u_char *cp = new_seq; 159 160 /* 161 * Bail if this is an IP fragment or if the TCP packet isn't 162 * `compressible' (i.e., ACK isn't set or some other control bit is 163 * set). (We assume that the caller has already made sure the 164 * packet is IP proto TCP). 165 */ 166 if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) 167 return (TYPE_IP); 168 169 th = (struct tcphdr *)&((int *)ip)[hlen]; 170 if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK) 171 return (TYPE_IP); 172 /* 173 * Packet is compressible -- we're going to send either a 174 * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need 175 * to locate (or create) the connection state. Special case the 176 * most recently used connection since it's most likely to be used 177 * again & we don't have to do any reordering if it's used. 178 */ 179 INCR(sls_packets) 180 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr || 181 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr || 182 *(int *)th != ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl]) { 183 /* 184 * Wasn't the first -- search for it. 185 * 186 * States are kept in a circularly linked list with 187 * last_cs pointing to the end of the list. The 188 * list is kept in lru order by moving a state to the 189 * head of the list whenever it is referenced. Since 190 * the list is short and, empirically, the connection 191 * we want is almost always near the front, we locate 192 * states via linear search. If we don't find a state 193 * for the datagram, the oldest state is (re-)used. 194 */ 195 register struct cstate *lcs; 196 register struct cstate *lastcs = comp->last_cs; 197 198 do { 199 lcs = cs; cs = cs->cs_next; 200 INCR(sls_searches) 201 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr 202 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr 203 && *(int *)th == ((int *)&cs->cs_ip)[cs->cs_ip.ip_hl]) 204 goto found; 205 } while (cs != lastcs); 206 207 /* 208 * Didn't find it -- re-use oldest cstate. Send an 209 * uncompressed packet that tells the other side what 210 * connection number we're using for this conversation. 211 * Note that since the state list is circular, the oldest 212 * state points to the newest and we only need to set 213 * last_cs to update the lru linkage. 214 */ 215 INCR(sls_misses) 216 comp->last_cs = lcs; 217 hlen += th->th_off; 218 hlen <<= 2; 219 goto uncompressed; 220 221 found: 222 /* 223 * Found it -- move to the front on the connection list. 224 */ 225 if (cs == lastcs) 226 comp->last_cs = lcs; 227 else { 228 lcs->cs_next = cs->cs_next; 229 cs->cs_next = lastcs->cs_next; 230 lastcs->cs_next = cs; 231 } 232 } 233 234 /* 235 * Make sure that only what we expect to change changed. The first 236 * line of the `if' checks the IP protocol version, header length & 237 * type of service. The 2nd line checks the "Don't fragment" bit. 238 * The 3rd line checks the time-to-live and protocol (the protocol 239 * check is unnecessary but costless). The 4th line checks the TCP 240 * header length. The 5th line checks IP options, if any. The 6th 241 * line checks TCP options, if any. If any of these things are 242 * different between the previous & current datagram, we send the 243 * current datagram `uncompressed'. 244 */ 245 oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen]; 246 deltaS = hlen; 247 hlen += th->th_off; 248 hlen <<= 2; 249 250 if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] || 251 ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] || 252 ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] || 253 th->th_off != oth->th_off || 254 (deltaS > 5 && 255 BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) || 256 (th->th_off > 5 && 257 BCMP(th + 1, oth + 1, (th->th_off - 5) << 2))) 258 goto uncompressed; 259 260 /* 261 * Figure out which of the changing fields changed. The 262 * receiver expects changes in the order: urgent, window, 263 * ack, seq (the order minimizes the number of temporaries 264 * needed in this section of code). 265 */ 266 if (th->th_flags & TH_URG) { 267 deltaS = ntohs(th->th_urp); 268 ENCODEZ(deltaS); 269 changes |= NEW_U; 270 } else if (th->th_urp != oth->th_urp) 271 /* argh! URG not set but urp changed -- a sensible 272 * implementation should never do this but RFC793 273 * doesn't prohibit the change so we have to deal 274 * with it. */ 275 goto uncompressed; 276 277 if (deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) { 278 ENCODE(deltaS); 279 changes |= NEW_W; 280 } 281 282 if (deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) { 283 if (deltaA > 0xffff) 284 goto uncompressed; 285 ENCODE(deltaA); 286 changes |= NEW_A; 287 } 288 289 if (deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) { 290 if (deltaS > 0xffff) 291 goto uncompressed; 292 ENCODE(deltaS); 293 changes |= NEW_S; 294 } 295 296 switch(changes) { 297 298 case 0: 299 /* 300 * Nothing changed. If this packet contains data and the 301 * last one didn't, this is probably a data packet following 302 * an ack (normal on an interactive connection) and we send 303 * it compressed. Otherwise it's probably a retransmit, 304 * retransmitted ack or window probe. Send it uncompressed 305 * in case the other side missed the compressed version. 306 */ 307 if (ip->ip_len != cs->cs_ip.ip_len && 308 ntohs(cs->cs_ip.ip_len) == hlen) 309 break; 310 311 /* (fall through) */ 312 313 case SPECIAL_I: 314 case SPECIAL_D: 315 /* 316 * actual changes match one of our special case encodings -- 317 * send packet uncompressed. 318 */ 319 goto uncompressed; 320 321 case NEW_S|NEW_A: 322 if (deltaS == deltaA && 323 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 324 /* special case for echoed terminal traffic */ 325 changes = SPECIAL_I; 326 cp = new_seq; 327 } 328 break; 329 330 case NEW_S: 331 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 332 /* special case for data xfer */ 333 changes = SPECIAL_D; 334 cp = new_seq; 335 } 336 break; 337 } 338 339 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id); 340 if (deltaS != 1) { 341 ENCODEZ(deltaS); 342 changes |= NEW_I; 343 } 344 if (th->th_flags & TH_PUSH) 345 changes |= TCP_PUSH_BIT; 346 /* 347 * Grab the cksum before we overwrite it below. Then update our 348 * state with this packet's header. 349 */ 350 deltaA = ntohs(th->th_sum); 351 BCOPY(ip, &cs->cs_ip, hlen); 352 353 /* 354 * We want to use the original packet as our compressed packet. 355 * (cp - new_seq) is the number of bytes we need for compressed 356 * sequence numbers. In addition we need one byte for the change 357 * mask, one for the connection id and two for the tcp checksum. 358 * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how 359 * many bytes of the original packet to toss so subtract the two to 360 * get the new packet size. 361 */ 362 deltaS = cp - new_seq; 363 cp = (u_char *)ip; 364 if (compress_cid == 0 || comp->last_xmit != cs->cs_id) { 365 comp->last_xmit = cs->cs_id; 366 hlen -= deltaS + 4; 367 cp += hlen; 368 *cp++ = changes | NEW_C; 369 *cp++ = cs->cs_id; 370 } else { 371 hlen -= deltaS + 3; 372 cp += hlen; 373 *cp++ = changes; 374 } 375 m->m_len -= hlen; 376 m->m_data += hlen; 377 *cp++ = deltaA >> 8; 378 *cp++ = deltaA; 379 BCOPY(new_seq, cp, deltaS); 380 INCR(sls_compressed) 381 return (TYPE_COMPRESSED_TCP); 382 383 /* 384 * Update connection state cs & send uncompressed packet ('uncompressed' 385 * means a regular ip/tcp packet but with the 'conversation id' we hope 386 * to use on future compressed packets in the protocol field). 387 */ 388 uncompressed: 389 BCOPY(ip, &cs->cs_ip, hlen); 390 ip->ip_p = cs->cs_id; 391 comp->last_xmit = cs->cs_id; 392 return (TYPE_UNCOMPRESSED_TCP); 393 } 394 395 396 int 397 sl_uncompress_tcp(bufp, len, type, comp) 398 u_char **bufp; 399 int len; 400 u_int type; 401 struct slcompress *comp; 402 { 403 404 return sl_uncompress_tcp_part(bufp, len, len, type, comp); 405 } 406 407 /* 408 * Uncompress a packet of total length total_len. The first buflen 409 * bytes are at *bufp; this must include the entire (compressed or 410 * uncompressed) TCP/IP header. In addition, there must be enough 411 * clear space before *bufp to build a full-length TCP/IP header. 412 */ 413 int 414 sl_uncompress_tcp_part(bufp, buflen, total_len, type, comp) 415 u_char **bufp; 416 int buflen, total_len; 417 u_int type; 418 struct slcompress *comp; 419 { 420 register u_char *cp; 421 register u_int hlen, changes; 422 register struct tcphdr *th; 423 register struct cstate *cs; 424 register struct ip *ip; 425 426 switch (type) { 427 428 case TYPE_UNCOMPRESSED_TCP: 429 ip = (struct ip *) *bufp; 430 if (ip->ip_p >= MAX_STATES) 431 goto bad; 432 cs = &comp->rstate[comp->last_recv = ip->ip_p]; 433 comp->flags &=~ SLF_TOSS; 434 ip->ip_p = IPPROTO_TCP; 435 hlen = ip->ip_hl; 436 hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off; 437 hlen <<= 2; 438 BCOPY(ip, &cs->cs_ip, hlen); 439 cs->cs_ip.ip_sum = 0; 440 cs->cs_hlen = hlen; 441 INCR(sls_uncompressedin) 442 return (total_len); 443 444 default: 445 goto bad; 446 447 case TYPE_COMPRESSED_TCP: 448 break; 449 } 450 /* We've got a compressed packet. */ 451 INCR(sls_compressedin) 452 cp = *bufp; 453 changes = *cp++; 454 if (changes & NEW_C) { 455 /* Make sure the state index is in range, then grab the state. 456 * If we have a good state index, clear the 'discard' flag. */ 457 if (*cp >= MAX_STATES) 458 goto bad; 459 460 comp->flags &=~ SLF_TOSS; 461 comp->last_recv = *cp++; 462 } else { 463 /* this packet has an implicit state index. If we've 464 * had a line error since the last time we got an 465 * explicit state index, we have to toss the packet. */ 466 if (comp->flags & SLF_TOSS) { 467 INCR(sls_tossed) 468 return (0); 469 } 470 } 471 cs = &comp->rstate[comp->last_recv]; 472 hlen = cs->cs_ip.ip_hl << 2; 473 th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen]; 474 th->th_sum = htons((*cp << 8) | cp[1]); 475 cp += 2; 476 if (changes & TCP_PUSH_BIT) 477 th->th_flags |= TH_PUSH; 478 else 479 th->th_flags &=~ TH_PUSH; 480 481 switch (changes & SPECIALS_MASK) { 482 case SPECIAL_I: 483 { 484 register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; 485 th->th_ack = htonl(ntohl(th->th_ack) + i); 486 th->th_seq = htonl(ntohl(th->th_seq) + i); 487 } 488 break; 489 490 case SPECIAL_D: 491 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) 492 - cs->cs_hlen); 493 break; 494 495 default: 496 if (changes & NEW_U) { 497 th->th_flags |= TH_URG; 498 DECODEU(th->th_urp) 499 } else 500 th->th_flags &=~ TH_URG; 501 if (changes & NEW_W) 502 DECODES(th->th_win) 503 if (changes & NEW_A) 504 DECODEL(th->th_ack) 505 if (changes & NEW_S) 506 DECODEL(th->th_seq) 507 break; 508 } 509 if (changes & NEW_I) { 510 DECODES(cs->cs_ip.ip_id) 511 } else 512 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1); 513 514 /* 515 * At this point, cp points to the first byte of data in the 516 * packet. If we're not aligned on a 4-byte boundary, copy the 517 * data down so the ip & tcp headers will be aligned. Then back up 518 * cp by the tcp/ip header length to make room for the reconstructed 519 * header (we assume the packet we were handed has enough space to 520 * prepend 128 bytes of header). Adjust the length to account for 521 * the new header & fill in the IP total length. 522 */ 523 buflen -= (cp - *bufp); 524 total_len -= (cp - *bufp); 525 if (buflen < 0) 526 /* we must have dropped some characters (crc should detect 527 * this but the old slip framing won't) */ 528 goto bad; 529 530 if ((int)cp & 3) { 531 if (buflen > 0) 532 (void) ovbcopy(cp, (caddr_t)((int)cp &~ 3), buflen); 533 cp = (u_char *)((int)cp &~ 3); 534 } 535 cp -= cs->cs_hlen; 536 total_len += cs->cs_hlen; 537 cs->cs_ip.ip_len = htons(total_len); 538 BCOPY(&cs->cs_ip, cp, cs->cs_hlen); 539 *bufp = cp; 540 541 /* recompute the ip header checksum */ 542 { 543 register u_short *bp = (u_short *)cp; 544 for (changes = 0; hlen > 0; hlen -= 2) 545 changes += *bp++; 546 changes = (changes & 0xffff) + (changes >> 16); 547 changes = (changes & 0xffff) + (changes >> 16); 548 ((struct ip *)cp)->ip_sum = ~ changes; 549 } 550 return (total_len); 551 bad: 552 comp->flags |= SLF_TOSS; 553 INCR(sls_errorin) 554 return (0); 555 } 556