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