slcompress.c revision 81634
199158Sdes/* 299158Sdes * Routines to compress and uncompess tcp packets (for transmission 399158Sdes * over low speed serial lines. 499158Sdes * 599158Sdes * Copyright (c) 1989 Regents of the University of California. 699158Sdes * All rights reserved. 799158Sdes * 899158Sdes * Redistribution and use in source and binary forms are permitted 999158Sdes * provided that the above copyright notice and this paragraph are 1099158Sdes * duplicated in all such forms and that any documentation, 1199158Sdes * advertising materials, and other materials related to such 1299158Sdes * distribution and use acknowledge that the software was developed 1399158Sdes * by the University of California, Berkeley. The name of the 1499158Sdes * University may not be used to endorse or promote products derived 1599158Sdes * from this software without specific prior written permission. 1699158Sdes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1799158Sdes * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1899158Sdes * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1999158Sdes * 2099158Sdes * $FreeBSD: head/usr.sbin/ppp/slcompress.c 81634 2001-08-14 16:05:52Z brian $ 2199158Sdes * 2299158Sdes * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989: 2399158Sdes * - Initial distribution. 2499158Sdes */ 2599158Sdes 2699158Sdes#include <sys/param.h> 2799158Sdes#include <netinet/in_systm.h> 2899158Sdes#include <netinet/in.h> 2999158Sdes#include <netinet/tcp.h> 3099158Sdes#include <netinet/ip.h> 3199158Sdes#include <sys/socket.h> 3299158Sdes#include <sys/un.h> 3399158Sdes 3499158Sdes#include <stdio.h> 3599158Sdes#include <string.h> 3699158Sdes#include <termios.h> 3799158Sdes 3899158Sdes#include "layer.h" 3999158Sdes#include "defs.h" 4099158Sdes#include "command.h" 4199158Sdes#include "mbuf.h" 4299158Sdes#include "log.h" 4399158Sdes#include "slcompress.h" 4499158Sdes#include "descriptor.h" 4599158Sdes#include "prompt.h" 4699158Sdes#include "timer.h" 4799158Sdes#include "fsm.h" 4899158Sdes#include "throughput.h" 4999158Sdes#include "iplist.h" 5099158Sdes#include "lqr.h" 5199158Sdes#include "hdlc.h" 5299158Sdes#include "ncpaddr.h" 5399158Sdes#include "ip.h" 5499158Sdes#include "ipcp.h" 5599158Sdes#include "filter.h" 5699158Sdes#include "lcp.h" 5799158Sdes#include "ccp.h" 5899158Sdes#include "link.h" 5999158Sdes#include "mp.h" 6099158Sdes#ifndef NORADIUS 6199158Sdes#include "radius.h" 6299158Sdes#endif 6399158Sdes#include "ipv6cp.h" 6499158Sdes#include "ncp.h" 6599158Sdes#include "bundle.h" 6699158Sdes 6799158Sdesvoid 6899158Sdessl_compress_init(struct slcompress *comp, int max_state) 6999158Sdes{ 7099158Sdes register u_int i; 7199158Sdes register struct cstate *tstate = comp->tstate; 7299158Sdes 7399158Sdes memset(comp, '\0', sizeof *comp); 7499158Sdes for (i = max_state; i > 0; --i) { 7599158Sdes tstate[i].cs_id = i; 7699158Sdes tstate[i].cs_next = &tstate[i - 1]; 7799158Sdes } 7899158Sdes tstate[0].cs_next = &tstate[max_state]; 7999158Sdes tstate[0].cs_id = 0; 8099158Sdes comp->last_cs = &tstate[0]; 8199158Sdes comp->last_recv = 255; 8299158Sdes comp->last_xmit = 255; 8399158Sdes comp->flags = SLF_TOSS; 8499158Sdes} 8599158Sdes 8699158Sdes 8799158Sdes/* ENCODE encodes a number that is known to be non-zero. ENCODEZ 8899158Sdes * checks for zero (since zero has to be encoded in the 32-bit, 3 byte 8999158Sdes * form). 9099158Sdes */ 9199158Sdes#define ENCODE(n) { \ 9299158Sdes if ((u_short)(n) >= 256) { \ 9399158Sdes *cp++ = 0; \ 9499158Sdes cp[1] = (n); \ 9599158Sdes cp[0] = (n) >> 8; \ 9699158Sdes cp += 2; \ 9799158Sdes } else { \ 9899158Sdes *cp++ = (n); \ 9999158Sdes } \ 10099158Sdes} 10199158Sdes#define ENCODEZ(n) { \ 10299158Sdes if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \ 10399158Sdes *cp++ = 0; \ 10499158Sdes cp[1] = (n); \ 10599158Sdes cp[0] = (n) >> 8; \ 10699158Sdes cp += 2; \ 10799158Sdes } else { \ 10899158Sdes *cp++ = (n); \ 10999158Sdes } \ 11099158Sdes} 11199158Sdes 11299158Sdes#define DECODEL(f) { \ 11399158Sdes if (*cp == 0) {\ 11499158Sdes (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \ 11599158Sdes cp += 3; \ 11699158Sdes } else { \ 11799158Sdes (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \ 11899158Sdes } \ 11999158Sdes} 12099158Sdes 12199158Sdes#define DECODES(f) { \ 12299158Sdes if (*cp == 0) {\ 12399158Sdes (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \ 12499158Sdes cp += 3; \ 12599158Sdes } else { \ 12699158Sdes (f) = htons(ntohs(f) + (u_int32_t)*cp++); \ 12799158Sdes } \ 12899158Sdes} 12999158Sdes 13099158Sdes#define DECODEU(f) { \ 13199158Sdes if (*cp == 0) {\ 13299158Sdes (f) = htons((cp[1] << 8) | cp[2]); \ 13399158Sdes cp += 3; \ 13499158Sdes } else { \ 13599158Sdes (f) = htons((u_int32_t)*cp++); \ 13699158Sdes } \ 13799158Sdes} 13899158Sdes 13999158Sdes 14099158Sdesu_char 14199158Sdessl_compress_tcp(struct mbuf * m, 14299158Sdes struct ip * ip, 14399158Sdes struct slcompress *comp, 14499158Sdes struct slstat *slstat, 14599158Sdes int compress_cid) 14699158Sdes{ 14799158Sdes register struct cstate *cs = comp->last_cs->cs_next; 14899158Sdes register u_int hlen = ip->ip_hl; 14999158Sdes register struct tcphdr *oth; 15099158Sdes register struct tcphdr *th; 15199158Sdes register u_int deltaS, deltaA; 15299158Sdes register u_int changes = 0; 15399158Sdes u_char new_seq[16]; 15499158Sdes register u_char *cp = new_seq; 15599158Sdes 15699158Sdes /* 15799158Sdes * Bail if this is an IP fragment or if the TCP packet isn't `compressible' 15899158Sdes * (i.e., ACK isn't set or some other control bit is set). (We assume that 15999158Sdes * the caller has already made sure the packet is IP proto TCP). 16099158Sdes */ 16199158Sdes if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) { 16299158Sdes log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n", 16399158Sdes ip->ip_off, (unsigned long)m->m_len); 164 log_DumpBp(LogDEBUG, "", m); 165 return (TYPE_IP); 166 } 167 th = (struct tcphdr *) & ((int *) ip)[hlen]; 168 if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) { 169 log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags); 170 log_DumpBp(LogDEBUG, "", m); 171 return (TYPE_IP); 172 } 173 174 /* 175 * Packet is compressible -- we're going to send either a COMPRESSED_TCP or 176 * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the 177 * connection state. Special case the most recently used connection since 178 * it's most likely to be used again & we don't have to do any reordering 179 * if it's used. 180 */ 181 slstat->sls_packets++; 182 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr || 183 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr || 184 *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) { 185 186 /* 187 * Wasn't the first -- search for it. 188 * 189 * States are kept in a circularly linked list with last_cs pointing to the 190 * end of the list. The list is kept in lru order by moving a state to 191 * the head of the list whenever it is referenced. Since the list is 192 * short and, empirically, the connection we want is almost always near 193 * the front, we locate states via linear search. If we don't find a 194 * state for the datagram, the oldest state is (re-)used. 195 */ 196 register struct cstate *lcs; 197 register struct cstate *lastcs = comp->last_cs; 198 199 do { 200 lcs = cs; 201 cs = cs->cs_next; 202 slstat->sls_searches++; 203 if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr 204 && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr 205 && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) 206 goto found; 207 } while (cs != lastcs); 208 209 /* 210 * Didn't find it -- re-use oldest cstate. Send an uncompressed packet 211 * that tells the other side what connection number we're using for this 212 * conversation. Note that since the state list is circular, the oldest 213 * state points to the newest and we only need to set last_cs to update 214 * the lru linkage. 215 */ 216 slstat->sls_misses++; 217 comp->last_cs = lcs; 218#define THOFFSET(th) (th->th_off) 219 hlen += th->th_off; 220 hlen <<= 2; 221 if (hlen > m->m_len) 222 return (TYPE_IP); 223 goto uncompressed; 224 225found: 226 227 /* 228 * Found it -- move to the front on the connection list. 229 */ 230 if (cs == lastcs) 231 comp->last_cs = lcs; 232 else { 233 lcs->cs_next = cs->cs_next; 234 cs->cs_next = lastcs->cs_next; 235 lastcs->cs_next = cs; 236 } 237 } 238 239 /* 240 * Make sure that only what we expect to change changed. The first line of 241 * the `if' checks the IP protocol version, header length & type of 242 * service. The 2nd line checks the "Don't fragment" bit. The 3rd line 243 * checks the time-to-live and protocol (the protocol check is unnecessary 244 * but costless). The 4th line checks the TCP header length. The 5th line 245 * checks IP options, if any. The 6th line checks TCP options, if any. If 246 * any of these things are different between the previous & current 247 * datagram, we send the current datagram `uncompressed'. 248 */ 249 oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen]; 250 deltaS = hlen; 251 hlen += th->th_off; 252 hlen <<= 2; 253 if (hlen > m->m_len) 254 return (TYPE_IP); 255 256 if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] || 257 ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] || 258 ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] || 259 THOFFSET(th) != THOFFSET(oth) || 260 (deltaS > 5 && 261 memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) || 262 (THOFFSET(th) > 5 && 263 memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) { 264 goto uncompressed; 265 } 266 267 /* 268 * Figure out which of the changing fields changed. The receiver expects 269 * changes in the order: urgent, window, ack, seq (the order minimizes the 270 * number of temporaries needed in this section of code). 271 */ 272 if (th->th_flags & TH_URG) { 273 deltaS = ntohs(th->th_urp); 274 ENCODEZ(deltaS); 275 changes |= NEW_U; 276 } else if (th->th_urp != oth->th_urp) { 277 278 /* 279 * argh! URG not set but urp changed -- a sensible implementation should 280 * never do this but RFC793 doesn't prohibit the change so we have to 281 * deal with it. 282 */ 283 goto uncompressed; 284 } 285 deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win)); 286 if (deltaS) { 287 ENCODE(deltaS); 288 changes |= NEW_W; 289 } 290 deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack); 291 if (deltaA) { 292 if (deltaA > 0xffff) { 293 goto uncompressed; 294 } 295 ENCODE(deltaA); 296 changes |= NEW_A; 297 } 298 deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq); 299 if (deltaS) { 300 if (deltaS > 0xffff) { 301 goto uncompressed; 302 } 303 ENCODE(deltaS); 304 changes |= NEW_S; 305 } 306 switch (changes) { 307 308 case 0: 309 310 /* 311 * Nothing changed. If this packet contains data and the last one didn't, 312 * this is probably a data packet following an ack (normal on an 313 * interactive connection) and we send it compressed. Otherwise it's 314 * probably a retransmit, retransmitted ack or window probe. Send it 315 * uncompressed in case the other side missed the compressed version. 316 */ 317 if (ip->ip_len != cs->cs_ip.ip_len && 318 ntohs(cs->cs_ip.ip_len) == hlen) 319 break; 320 321 /* (fall through) */ 322 323 case SPECIAL_I: 324 case SPECIAL_D: 325 326 /* 327 * actual changes match one of our special case encodings -- send packet 328 * uncompressed. 329 */ 330 goto uncompressed; 331 332 case NEW_S | NEW_A: 333 if (deltaS == deltaA && 334 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 335 /* special case for echoed terminal traffic */ 336 changes = SPECIAL_I; 337 cp = new_seq; 338 } 339 break; 340 341 case NEW_S: 342 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) { 343 /* special case for data xfer */ 344 changes = SPECIAL_D; 345 cp = new_seq; 346 } 347 break; 348 } 349 350 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id); 351 if (deltaS != 1) { 352 ENCODEZ(deltaS); 353 changes |= NEW_I; 354 } 355 if (th->th_flags & TH_PUSH) 356 changes |= TCP_PUSH_BIT; 357 358 /* 359 * Grab the cksum before we overwrite it below. Then update our state with 360 * this packet's header. 361 */ 362 deltaA = ntohs(th->th_sum); 363 memcpy(&cs->cs_ip, ip, hlen); 364 365 /* 366 * We want to use the original packet as our compressed packet. (cp - 367 * new_seq) is the number of bytes we need for compressed sequence numbers. 368 * In addition we need one byte for the change mask, one for the connection 369 * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header 370 * are needed. hlen is how many bytes of the original packet to toss so 371 * subtract the two to get the new packet size. 372 */ 373 deltaS = cp - new_seq; 374 cp = (u_char *) ip; 375 376 /* 377 * Since fastq traffic can jump ahead of the background traffic, we don't 378 * know what order packets will go on the line. In this case, we always 379 * send a "new" connection id so the receiver state stays synchronized. 380 */ 381 if (comp->last_xmit == cs->cs_id && compress_cid) { 382 hlen -= deltaS + 3; 383 cp += hlen; 384 *cp++ = changes; 385 } else { 386 comp->last_xmit = cs->cs_id; 387 hlen -= deltaS + 4; 388 cp += hlen; 389 *cp++ = changes | NEW_C; 390 *cp++ = cs->cs_id; 391 } 392 m->m_len -= hlen; 393 m->m_offset += hlen; 394 *cp++ = deltaA >> 8; 395 *cp++ = deltaA; 396 memcpy(cp, new_seq, deltaS); 397 slstat->sls_compressed++; 398 return (TYPE_COMPRESSED_TCP); 399 400 /* 401 * Update connection state cs & send uncompressed packet ('uncompressed' 402 * means a regular ip/tcp packet but with the 'conversation id' we hope to 403 * use on future compressed packets in the protocol field). 404 */ 405uncompressed: 406 memcpy(&cs->cs_ip, ip, hlen); 407 ip->ip_p = cs->cs_id; 408 comp->last_xmit = cs->cs_id; 409 return (TYPE_UNCOMPRESSED_TCP); 410} 411 412 413int 414sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp, 415 struct slstat *slstat, int max_state) 416{ 417 register u_char *cp; 418 register u_int hlen, changes; 419 register struct tcphdr *th; 420 register struct cstate *cs; 421 register struct ip *ip; 422 u_short *bp; 423 424 switch (type) { 425 426 case TYPE_UNCOMPRESSED_TCP: 427 ip = (struct ip *) * bufp; 428 if (ip->ip_p > max_state) 429 goto bad; 430 cs = &comp->rstate[comp->last_recv = ip->ip_p]; 431 comp->flags &= ~SLF_TOSS; 432 ip->ip_p = IPPROTO_TCP; 433 434 /* 435 * Calculate the size of the TCP/IP header and make sure that we don't 436 * overflow the space we have available for it. 437 */ 438 hlen = ip->ip_hl << 2; 439 if (hlen + sizeof(struct tcphdr) > len) 440 goto bad; 441 th = (struct tcphdr *) & ((char *) ip)[hlen]; 442 hlen += THOFFSET(th) << 2; 443 if (hlen > MAX_HDR) 444 goto bad; 445 memcpy(&cs->cs_ip, ip, hlen); 446 cs->cs_hlen = hlen; 447 slstat->sls_uncompressedin++; 448 return (len); 449 450 default: 451 goto bad; 452 453 case TYPE_COMPRESSED_TCP: 454 break; 455 } 456 457 /* We've got a compressed packet. */ 458 slstat->sls_compressedin++; 459 cp = *bufp; 460 changes = *cp++; 461 log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes); 462 463 if (changes & NEW_C) { 464 /* 465 * Make sure the state index is in range, then grab the state. If we have 466 * a good state index, clear the 'discard' flag. 467 */ 468 if (*cp > max_state || comp->last_recv == 255) 469 goto bad; 470 471 comp->flags &= ~SLF_TOSS; 472 comp->last_recv = *cp++; 473 } else { 474 /* 475 * this packet has an implicit state index. If we've had a line error 476 * since the last time we got an explicit state index, we have to toss 477 * the packet. 478 */ 479 if (comp->flags & SLF_TOSS) { 480 slstat->sls_tossed++; 481 return (0); 482 } 483 } 484 cs = &comp->rstate[comp->last_recv]; 485 hlen = cs->cs_ip.ip_hl << 2; 486 th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen]; 487 th->th_sum = htons((*cp << 8) | cp[1]); 488 cp += 2; 489 if (changes & TCP_PUSH_BIT) 490 th->th_flags |= TH_PUSH; 491 else 492 th->th_flags &= ~TH_PUSH; 493 494 switch (changes & SPECIALS_MASK) { 495 case SPECIAL_I: 496 { 497 register u_int i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen; 498 499 th->th_ack = htonl(ntohl(th->th_ack) + i); 500 th->th_seq = htonl(ntohl(th->th_seq) + i); 501 } 502 break; 503 504 case SPECIAL_D: 505 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) 506 - cs->cs_hlen); 507 break; 508 509 default: 510 if (changes & NEW_U) { 511 th->th_flags |= TH_URG; 512 DECODEU(th->th_urp) 513 } else 514 th->th_flags &= ~TH_URG; 515 if (changes & NEW_W) 516 DECODES(th->th_win) 517 if (changes & NEW_A) 518 DECODEL(th->th_ack) 519 if (changes & NEW_S) { 520 log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n", 521 *cp, cp[1], cp[2]); 522 DECODEL(th->th_seq) 523 } 524 break; 525 } 526 if (changes & NEW_I) { 527 DECODES(cs->cs_ip.ip_id) 528 } else 529 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1); 530 531 log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n", 532 cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq)); 533 534 /* 535 * At this point, cp points to the first byte of data in the packet. 536 * Back up cp by the tcp/ip header length to make room for the 537 * reconstructed header (we assume the packet we were handed has enough 538 * space to prepend 128 bytes of header). Adjust the length to account 539 * for the new header & fill in the IP total length. 540 */ 541 len -= (cp - *bufp); 542 if (len < 0) 543 /* 544 * we must have dropped some characters (crc should detect this but the 545 * old slip framing won't) 546 */ 547 goto bad; 548 549 *bufp = cp - cs->cs_hlen; 550 len += cs->cs_hlen; 551 cs->cs_ip.ip_len = htons(len); 552 553 /* recompute the ip header checksum */ 554 cs->cs_ip.ip_sum = 0; 555 bp = (u_short *)&cs->cs_ip; 556 for (changes = 0; hlen > 0; hlen -= 2) 557 changes += *bp++; 558 changes = (changes & 0xffff) + (changes >> 16); 559 changes = (changes & 0xffff) + (changes >> 16); 560 cs->cs_ip.ip_sum = ~changes; 561 562 /* And copy the result into our buffer */ 563 memcpy(*bufp, &cs->cs_ip, cs->cs_hlen); 564 565 return (len); 566bad: 567 comp->flags |= SLF_TOSS; 568 slstat->sls_errorin++; 569 return (0); 570} 571 572int 573sl_Show(struct cmdargs const *arg) 574{ 575 prompt_Printf(arg->prompt, "VJ compression statistics:\n"); 576 prompt_Printf(arg->prompt, " Out: %d (compress) / %d (total)", 577 arg->bundle->ncp.ipcp.vj.slstat.sls_compressed, 578 arg->bundle->ncp.ipcp.vj.slstat.sls_packets); 579 prompt_Printf(arg->prompt, " %d (miss) / %d (search)\n", 580 arg->bundle->ncp.ipcp.vj.slstat.sls_misses, 581 arg->bundle->ncp.ipcp.vj.slstat.sls_searches); 582 prompt_Printf(arg->prompt, " In: %d (compress), %d (uncompress)", 583 arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin, 584 arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin); 585 prompt_Printf(arg->prompt, " %d (error), %d (tossed)\n", 586 arg->bundle->ncp.ipcp.vj.slstat.sls_errorin, 587 arg->bundle->ncp.ipcp.vj.slstat.sls_tossed); 588 return 0; 589} 590