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