vjcompress.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  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. * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
  21. * - Initial distribution.
  22. *
  23. * Modified June 1993 by Paul Mackerras, paulus@cs.anu.edu.au,
  24. * so that the entire packet being decompressed doesn't have
  25. * to be in contiguous memory (just the compressed header).
  26. */
  27. /*
  28. * This version is used under SunOS 4.x, Digital UNIX, AIX 4.x,
  29. * and SVR4 systems including Solaris 2.
  30. *
  31. * $Id: vjcompress.c,v 1.11 2004/01/17 05:47:55 carlsonj Exp $
  32. */
  33. #include <sys/types.h>
  34. #include <sys/param.h>
  35. #ifdef SVR4
  36. #ifndef __GNUC__
  37. #include <sys/byteorder.h> /* for ntohl, etc. */
  38. #else
  39. /* make sure we don't get the gnu "fixed" one! */
  40. #include "/usr/include/sys/byteorder.h"
  41. #endif
  42. #endif
  43. #ifdef __osf__
  44. #include <net/net_globals.h>
  45. #endif
  46. #include <netinet/in.h>
  47. #ifdef AIX4
  48. #define _NETINET_IN_SYSTM_H_
  49. typedef u_long n_long;
  50. #else
  51. #include <netinet/in_systm.h>
  52. #endif
  53. #ifdef SOL2
  54. #include <sys/sunddi.h>
  55. #endif
  56. #include <netinet/ip.h>
  57. #include <netinet/tcp.h>
  58. #include <net/ppp_defs.h>
  59. #include <net/vjcompress.h>
  60. #ifndef VJ_NO_STATS
  61. #define INCR(counter) ++comp->stats.counter
  62. #else
  63. #define INCR(counter)
  64. #endif
  65. #define BCMP(p1, p2, n) bcmp((char *)(p1), (char *)(p2), (int)(n))
  66. #undef BCOPY
  67. #define BCOPY(p1, p2, n) bcopy((char *)(p1), (char *)(p2), (int)(n))
  68. #ifndef KERNEL
  69. #define ovbcopy bcopy
  70. #endif
  71. #ifdef __osf__
  72. #define getip_hl(base) (((base).ip_vhl)&0xf)
  73. #define getth_off(base) ((((base).th_xoff)&0xf0)>>4)
  74. #else
  75. #define getip_hl(base) ((base).ip_hl)
  76. #define getth_off(base) ((base).th_off)
  77. #endif
  78. void
  79. vj_compress_init(comp, max_state)
  80. struct vjcompress *comp;
  81. int max_state;
  82. {
  83. register u_int i;
  84. register struct cstate *tstate = comp->tstate;
  85. if (max_state == -1)
  86. max_state = MAX_STATES - 1;
  87. bzero((char *)comp, sizeof(*comp));
  88. for (i = max_state; i > 0; --i) {
  89. tstate[i].cs_id = i;
  90. tstate[i].cs_next = &tstate[i - 1];
  91. }
  92. tstate[0].cs_next = &tstate[max_state];
  93. tstate[0].cs_id = 0;
  94. comp->last_cs = &tstate[0];
  95. comp->last_recv = 255;
  96. comp->last_xmit = 255;
  97. comp->flags = VJF_TOSS;
  98. }
  99. /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
  100. * checks for zero (since zero has to be encoded in the long, 3 byte
  101. * form).
  102. */
  103. #define ENCODE(n) { \
  104. if ((u_short)(n) >= 256) { \
  105. *cp++ = 0; \
  106. cp[1] = (n); \
  107. cp[0] = (n) >> 8; \
  108. cp += 2; \
  109. } else { \
  110. *cp++ = (n); \
  111. } \
  112. }
  113. #define ENCODEZ(n) { \
  114. if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
  115. *cp++ = 0; \
  116. cp[1] = (n); \
  117. cp[0] = (n) >> 8; \
  118. cp += 2; \
  119. } else { \
  120. *cp++ = (n); \
  121. } \
  122. }
  123. #define DECODEL(f) { \
  124. if (*cp == 0) {\
  125. u_int32_t tmp = ntohl(f) + ((cp[1] << 8) | cp[2]); \
  126. (f) = htonl(tmp); \
  127. cp += 3; \
  128. } else { \
  129. u_int32_t tmp = ntohl(f) + (u_int32_t)*cp++; \
  130. (f) = htonl(tmp); \
  131. } \
  132. }
  133. #define DECODES(f) { \
  134. if (*cp == 0) {\
  135. u_short tmp = ntohs(f) + ((cp[1] << 8) | cp[2]); \
  136. (f) = htons(tmp); \
  137. cp += 3; \
  138. } else { \
  139. u_short tmp = ntohs(f) + (u_int32_t)*cp++; \
  140. (f) = htons(tmp); \
  141. } \
  142. }
  143. #define DECODEU(f) { \
  144. if (*cp == 0) {\
  145. (f) = htons((cp[1] << 8) | cp[2]); \
  146. cp += 3; \
  147. } else { \
  148. (f) = htons((u_int32_t)*cp++); \
  149. } \
  150. }
  151. u_int
  152. vj_compress_tcp(ip, mlen, comp, compress_cid, vjhdrp)
  153. register struct ip *ip;
  154. u_int mlen;
  155. struct vjcompress *comp;
  156. int compress_cid;
  157. u_char **vjhdrp;
  158. {
  159. register struct cstate *cs = comp->last_cs->cs_next;
  160. register u_int hlen = getip_hl(*ip);
  161. register struct tcphdr *oth;
  162. register struct tcphdr *th;
  163. register u_int deltaS, deltaA;
  164. register u_int changes = 0;
  165. u_char new_seq[16];
  166. register u_char *cp = new_seq;
  167. /*
  168. * Bail if this is an IP fragment or if the TCP packet isn't
  169. * `compressible' (i.e., ACK isn't set or some other control bit is
  170. * set). (We assume that the caller has already made sure the
  171. * packet is IP proto TCP).
  172. */
  173. if ((ip->ip_off & htons(0x3fff)) || mlen < 40)
  174. return (TYPE_IP);
  175. th = (struct tcphdr *)&((int *)ip)[hlen];
  176. if ((th->th_flags & (TH_SYN|TH_FIN|TH_RST|TH_ACK)) != TH_ACK)
  177. return (TYPE_IP);
  178. /*
  179. * Packet is compressible -- we're going to send either a
  180. * COMPRESSED_TCP or UNCOMPRESSED_TCP packet. Either way we need
  181. * to locate (or create) the connection state. Special case the
  182. * most recently used connection since it's most likely to be used
  183. * again & we don't have to do any reordering if it's used.
  184. */
  185. INCR(vjs_packets);
  186. if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
  187. ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
  188. *(int *)th != ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)]) {
  189. /*
  190. * Wasn't the first -- search for it.
  191. *
  192. * States are kept in a circularly linked list with
  193. * last_cs pointing to the end of the list. The
  194. * list is kept in lru order by moving a state to the
  195. * head of the list whenever it is referenced. Since
  196. * the list is short and, empirically, the connection
  197. * we want is almost always near the front, we locate
  198. * states via linear search. If we don't find a state
  199. * for the datagram, the oldest state is (re-)used.
  200. */
  201. register struct cstate *lcs;
  202. register struct cstate *lastcs = comp->last_cs;
  203. do {
  204. lcs = cs; cs = cs->cs_next;
  205. INCR(vjs_searches);
  206. if (ip->ip_src.s_addr == cs->cs_ip.ip_src.s_addr
  207. && ip->ip_dst.s_addr == cs->cs_ip.ip_dst.s_addr
  208. && *(int *)th == ((int *)&cs->cs_ip)[getip_hl(cs->cs_ip)])
  209. goto found;
  210. } while (cs != lastcs);
  211. /*
  212. * Didn't find it -- re-use oldest cstate. Send an
  213. * uncompressed packet that tells the other side what
  214. * connection number we're using for this conversation.
  215. * Note that since the state list is circular, the oldest
  216. * state points to the newest and we only need to set
  217. * last_cs to update the lru linkage.
  218. */
  219. INCR(vjs_misses);
  220. comp->last_cs = lcs;
  221. hlen += getth_off(*th);
  222. hlen <<= 2;
  223. if (hlen > mlen)
  224. return (TYPE_IP);
  225. goto uncompressed;
  226. found:
  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. * Make sure that only what we expect to change changed. The first
  240. * line of the `if' checks the IP protocol version, header length &
  241. * type of service. The 2nd line checks the "Don't fragment" bit.
  242. * The 3rd line checks the time-to-live and protocol (the protocol
  243. * check is unnecessary but costless). The 4th line checks the TCP
  244. * header length. The 5th line checks IP options, if any. The 6th
  245. * line checks TCP options, if any. If any of these things are
  246. * different between the previous & current datagram, we send the
  247. * current datagram `uncompressed'.
  248. */
  249. oth = (struct tcphdr *)&((int *)&cs->cs_ip)[hlen];
  250. deltaS = hlen;
  251. hlen += getth_off(*th);
  252. hlen <<= 2;
  253. if (hlen > mlen)
  254. return (TYPE_IP);
  255. if (((u_short *)ip)[0] != ((u_short *)&cs->cs_ip)[0] ||
  256. ((u_short *)ip)[3] != ((u_short *)&cs->cs_ip)[3] ||
  257. ((u_short *)ip)[4] != ((u_short *)&cs->cs_ip)[4] ||
  258. getth_off(*th) != getth_off(*oth) ||
  259. (deltaS > 5 && BCMP(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
  260. (getth_off(*th) > 5 && BCMP(th + 1, oth + 1, (getth_off(*th) - 5) << 2)))
  261. goto uncompressed;
  262. /*
  263. * Figure out which of the changing fields changed. The
  264. * receiver expects changes in the order: urgent, window,
  265. * ack, seq (the order minimizes the number of temporaries
  266. * needed in this section of code).
  267. */
  268. if (th->th_flags & TH_URG) {
  269. deltaS = ntohs(th->th_urp);
  270. ENCODEZ(deltaS);
  271. changes |= NEW_U;
  272. } else if (th->th_urp != oth->th_urp)
  273. /* argh! URG not set but urp changed -- a sensible
  274. * implementation should never do this but RFC793
  275. * doesn't prohibit the change so we have to deal
  276. * with it. */
  277. goto uncompressed;
  278. if ((deltaS = (u_short)(ntohs(th->th_win) - ntohs(oth->th_win))) > 0) {
  279. ENCODE(deltaS);
  280. changes |= NEW_W;
  281. }
  282. if ((deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack)) > 0) {
  283. if (deltaA > 0xffff)
  284. goto uncompressed;
  285. ENCODE(deltaA);
  286. changes |= NEW_A;
  287. }
  288. if ((deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq)) > 0) {
  289. if (deltaS > 0xffff)
  290. goto uncompressed;
  291. ENCODE(deltaS);
  292. changes |= NEW_S;
  293. }
  294. switch(changes) {
  295. case 0:
  296. /*
  297. * Nothing changed. If this packet contains data and the
  298. * last one didn't, this is probably a data packet following
  299. * an ack (normal on an interactive connection) and we send
  300. * it compressed. Otherwise it's probably a retransmit,
  301. * retransmitted ack or window probe. Send it uncompressed
  302. * in case the other side missed the compressed version.
  303. */
  304. if (ip->ip_len != cs->cs_ip.ip_len &&
  305. ntohs(cs->cs_ip.ip_len) == hlen)
  306. break;
  307. /* (fall through) */
  308. case SPECIAL_I:
  309. case SPECIAL_D:
  310. /*
  311. * actual changes match one of our special case encodings --
  312. * send packet uncompressed.
  313. */
  314. goto uncompressed;
  315. case NEW_S|NEW_A:
  316. if (deltaS == deltaA && deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
  317. /* special case for echoed terminal traffic */
  318. changes = SPECIAL_I;
  319. cp = new_seq;
  320. }
  321. break;
  322. case NEW_S:
  323. if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
  324. /* special case for data xfer */
  325. changes = SPECIAL_D;
  326. cp = new_seq;
  327. }
  328. break;
  329. }
  330. deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
  331. if (deltaS != 1) {
  332. ENCODEZ(deltaS);
  333. changes |= NEW_I;
  334. }
  335. if (th->th_flags & TH_PUSH)
  336. changes |= TCP_PUSH_BIT;
  337. /*
  338. * Grab the cksum before we overwrite it below. Then update our
  339. * state with this packet's header.
  340. */
  341. deltaA = ntohs(th->th_sum);
  342. BCOPY(ip, &cs->cs_ip, hlen);
  343. /*
  344. * We want to use the original packet as our compressed packet.
  345. * (cp - new_seq) is the number of bytes we need for compressed
  346. * sequence numbers. In addition we need one byte for the change
  347. * mask, one for the connection id and two for the tcp checksum.
  348. * So, (cp - new_seq) + 4 bytes of header are needed. hlen is how
  349. * many bytes of the original packet to toss so subtract the two to
  350. * get the new packet size.
  351. */
  352. deltaS = cp - new_seq;
  353. cp = (u_char *)ip;
  354. if (compress_cid == 0 || comp->last_xmit != cs->cs_id) {
  355. comp->last_xmit = cs->cs_id;
  356. hlen -= deltaS + 4;
  357. *vjhdrp = (cp += hlen);
  358. *cp++ = changes | NEW_C;
  359. *cp++ = cs->cs_id;
  360. } else {
  361. hlen -= deltaS + 3;
  362. *vjhdrp = (cp += hlen);
  363. *cp++ = changes;
  364. }
  365. *cp++ = deltaA >> 8;
  366. *cp++ = deltaA;
  367. BCOPY(new_seq, cp, deltaS);
  368. INCR(vjs_compressed);
  369. return (TYPE_COMPRESSED_TCP);
  370. /*
  371. * Update connection state cs & send uncompressed packet (that is,
  372. * a regular ip/tcp packet but with the 'conversation id' we hope
  373. * to use on future compressed packets in the protocol field).
  374. */
  375. uncompressed:
  376. BCOPY(ip, &cs->cs_ip, hlen);
  377. ip->ip_p = cs->cs_id;
  378. comp->last_xmit = cs->cs_id;
  379. return (TYPE_UNCOMPRESSED_TCP);
  380. }
  381. /*
  382. * Called when we may have missed a packet.
  383. */
  384. void
  385. vj_uncompress_err(comp)
  386. struct vjcompress *comp;
  387. {
  388. comp->flags |= VJF_TOSS;
  389. INCR(vjs_errorin);
  390. }
  391. /*
  392. * "Uncompress" a packet of type TYPE_UNCOMPRESSED_TCP.
  393. */
  394. int
  395. vj_uncompress_uncomp(buf, buflen, comp)
  396. u_char *buf;
  397. int buflen;
  398. struct vjcompress *comp;
  399. {
  400. register u_int hlen;
  401. register struct cstate *cs;
  402. register struct ip *ip;
  403. ip = (struct ip *) buf;
  404. hlen = getip_hl(*ip) << 2;
  405. if (ip->ip_p >= MAX_STATES
  406. || hlen + sizeof(struct tcphdr) > buflen
  407. || (hlen += getth_off(*((struct tcphdr *)&((char *)ip)[hlen])) << 2)
  408. > buflen
  409. || hlen > MAX_HDR) {
  410. comp->flags |= VJF_TOSS;
  411. INCR(vjs_errorin);
  412. return (0);
  413. }
  414. cs = &comp->rstate[comp->last_recv = ip->ip_p];
  415. comp->flags &=~ VJF_TOSS;
  416. ip->ip_p = IPPROTO_TCP;
  417. BCOPY(ip, &cs->cs_ip, hlen);
  418. cs->cs_hlen = hlen;
  419. INCR(vjs_uncompressedin);
  420. return (1);
  421. }
  422. /*
  423. * Uncompress a packet of type TYPE_COMPRESSED_TCP.
  424. * The packet starts at buf and is of total length total_len.
  425. * The first buflen bytes are at buf; this must include the entire
  426. * compressed TCP/IP header. This procedure returns the length
  427. * of the VJ header, with a pointer to the uncompressed IP header
  428. * in *hdrp and its length in *hlenp.
  429. */
  430. int
  431. vj_uncompress_tcp(buf, buflen, total_len, comp, hdrp, hlenp)
  432. u_char *buf;
  433. int buflen, total_len;
  434. struct vjcompress *comp;
  435. u_char **hdrp;
  436. u_int *hlenp;
  437. {
  438. register u_char *cp;
  439. register u_int hlen, changes;
  440. register struct tcphdr *th;
  441. register struct cstate *cs;
  442. register u_short *bp;
  443. register u_int vjlen;
  444. register u_int32_t tmp;
  445. INCR(vjs_compressedin);
  446. cp = buf;
  447. changes = *cp++;
  448. if (changes & NEW_C) {
  449. /* Make sure the state index is in range, then grab the state.
  450. * If we have a good state index, clear the 'discard' flag. */
  451. if (*cp >= MAX_STATES)
  452. goto bad;
  453. comp->flags &=~ VJF_TOSS;
  454. comp->last_recv = *cp++;
  455. } else {
  456. /* this packet has an implicit state index. If we've
  457. * had a line error since the last time we got an
  458. * explicit state index, we have to toss the packet. */
  459. if (comp->flags & VJF_TOSS) {
  460. INCR(vjs_tossed);
  461. return (-1);
  462. }
  463. }
  464. cs = &comp->rstate[comp->last_recv];
  465. hlen = getip_hl(cs->cs_ip) << 2;
  466. th = (struct tcphdr *)&((u_char *)&cs->cs_ip)[hlen];
  467. th->th_sum = htons((*cp << 8) | cp[1]);
  468. cp += 2;
  469. if (changes & TCP_PUSH_BIT)
  470. th->th_flags |= TH_PUSH;
  471. else
  472. th->th_flags &=~ TH_PUSH;
  473. switch (changes & SPECIALS_MASK) {
  474. case SPECIAL_I:
  475. {
  476. register u_int32_t i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
  477. /* some compilers can't nest inline assembler.. */
  478. tmp = ntohl(th->th_ack) + i;
  479. th->th_ack = htonl(tmp);
  480. tmp = ntohl(th->th_seq) + i;
  481. th->th_seq = htonl(tmp);
  482. }
  483. break;
  484. case SPECIAL_D:
  485. /* some compilers can't nest inline assembler.. */
  486. tmp = ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
  487. th->th_seq = htonl(tmp);
  488. break;
  489. default:
  490. if (changes & NEW_U) {
  491. th->th_flags |= TH_URG;
  492. DECODEU(th->th_urp);
  493. } else
  494. th->th_flags &=~ TH_URG;
  495. if (changes & NEW_W)
  496. DECODES(th->th_win);
  497. if (changes & NEW_A)
  498. DECODEL(th->th_ack);
  499. if (changes & NEW_S)
  500. DECODEL(th->th_seq);
  501. break;
  502. }
  503. if (changes & NEW_I) {
  504. DECODES(cs->cs_ip.ip_id);
  505. } else {
  506. cs->cs_ip.ip_id = ntohs(cs->cs_ip.ip_id) + 1;
  507. cs->cs_ip.ip_id = htons(cs->cs_ip.ip_id);
  508. }
  509. /*
  510. * At this point, cp points to the first byte of data in the
  511. * packet. Fill in the IP total length and update the IP
  512. * header checksum.
  513. */
  514. vjlen = cp - buf;
  515. buflen -= vjlen;
  516. if (buflen < 0)
  517. /* we must have dropped some characters (crc should detect
  518. * this but the old slip framing won't) */
  519. goto bad;
  520. total_len += cs->cs_hlen - vjlen;
  521. cs->cs_ip.ip_len = htons(total_len);
  522. /* recompute the ip header checksum */
  523. bp = (u_short *) &cs->cs_ip;
  524. cs->cs_ip.ip_sum = 0;
  525. for (changes = 0; hlen > 0; hlen -= 2)
  526. changes += *bp++;
  527. changes = (changes & 0xffff) + (changes >> 16);
  528. changes = (changes & 0xffff) + (changes >> 16);
  529. cs->cs_ip.ip_sum = ~ changes;
  530. *hdrp = (u_char *) &cs->cs_ip;
  531. *hlenp = cs->cs_hlen;
  532. return vjlen;
  533. bad:
  534. comp->flags |= VJF_TOSS;
  535. INCR(vjs_errorin);
  536. return (-1);
  537. }