slcompress.c 16 KB

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