NFE - Change default RX ring size from 128 -> 256, Adjust moderation timer.
[dragonfly.git] / usr.sbin / ppp / slcompress.c
blob048daa82984ef9f2ee61c65ba1ec514b504633ff
1 /*
2 * Routines to compress and uncompess tcp packets (for transmission
3 * over low speed serial lines.
5 * Copyright (c) 1989 Regents of the University of California.
6 * All rights reserved.
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.
20 * $FreeBSD: src/usr.sbin/ppp/slcompress.c,v 1.31.2.2 2002/09/01 02:12:32 brian Exp $
21 * $DragonFly: src/usr.sbin/ppp/slcompress.c,v 1.3 2004/03/27 01:39:13 cpressey Exp $
23 * Van Jacobson (van@helios.ee.lbl.gov), Dec 31, 1989:
24 * - Initial distribution.
27 #include <sys/param.h>
28 #include <netinet/in_systm.h>
29 #include <netinet/in.h>
30 #include <netinet/tcp.h>
31 #include <netinet/ip.h>
32 #include <sys/socket.h>
33 #include <sys/un.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <termios.h>
40 #include "layer.h"
41 #include "defs.h"
42 #include "command.h"
43 #include "mbuf.h"
44 #include "log.h"
45 #include "slcompress.h"
46 #include "descriptor.h"
47 #include "prompt.h"
48 #include "timer.h"
49 #include "fsm.h"
50 #include "throughput.h"
51 #include "iplist.h"
52 #include "lqr.h"
53 #include "hdlc.h"
54 #include "ncpaddr.h"
55 #include "ipcp.h"
56 #include "filter.h"
57 #include "lcp.h"
58 #include "ccp.h"
59 #include "link.h"
60 #include "mp.h"
61 #ifndef NORADIUS
62 #include "radius.h"
63 #endif
64 #include "ipv6cp.h"
65 #include "ncp.h"
66 #include "bundle.h"
68 void
69 sl_compress_init(struct slcompress *comp, int max_state)
71 u_int i;
72 struct cstate *tstate = comp->tstate;
74 memset(comp, '\0', sizeof *comp);
75 for (i = max_state; i > 0; --i) {
76 tstate[i].cs_id = i;
77 tstate[i].cs_next = &tstate[i - 1];
79 tstate[0].cs_next = &tstate[max_state];
80 tstate[0].cs_id = 0;
81 comp->last_cs = &tstate[0];
82 comp->last_recv = 255;
83 comp->last_xmit = 255;
84 comp->flags = SLF_TOSS;
88 /* ENCODE encodes a number that is known to be non-zero. ENCODEZ
89 * checks for zero (since zero has to be encoded in the 32-bit, 3 byte
90 * form).
92 #define ENCODE(n) { \
93 if ((u_short)(n) >= 256) { \
94 *cp++ = 0; \
95 cp[1] = (n); \
96 cp[0] = (n) >> 8; \
97 cp += 2; \
98 } else { \
99 *cp++ = (n); \
102 #define ENCODEZ(n) { \
103 if ((u_short)(n) >= 256 || (u_short)(n) == 0) { \
104 *cp++ = 0; \
105 cp[1] = (n); \
106 cp[0] = (n) >> 8; \
107 cp += 2; \
108 } else { \
109 *cp++ = (n); \
113 #define DECODEL(f) { \
114 if (*cp == 0) {\
115 (f) = htonl(ntohl(f) + ((cp[1] << 8) | cp[2])); \
116 cp += 3; \
117 } else { \
118 (f) = htonl(ntohl(f) + (u_int32_t)*cp++); \
122 #define DECODES(f) { \
123 if (*cp == 0) {\
124 (f) = htons(ntohs(f) + ((cp[1] << 8) | cp[2])); \
125 cp += 3; \
126 } else { \
127 (f) = htons(ntohs(f) + (u_int32_t)*cp++); \
131 #define DECODEU(f) { \
132 if (*cp == 0) {\
133 (f) = htons((cp[1] << 8) | cp[2]); \
134 cp += 3; \
135 } else { \
136 (f) = htons((u_int32_t)*cp++); \
141 u_char
142 sl_compress_tcp(struct mbuf * m,
143 struct ip * ip,
144 struct slcompress *comp,
145 struct slstat *slstat,
146 int compress_cid)
148 struct cstate *cs = comp->last_cs->cs_next;
149 u_int hlen = ip->ip_hl;
150 struct tcphdr *oth;
151 struct tcphdr *th;
152 u_int deltaS, deltaA;
153 u_int changes = 0;
154 u_char new_seq[16];
155 u_char *cp = new_seq;
158 * Bail if this is an IP fragment or if the TCP packet isn't `compressible'
159 * (i.e., ACK isn't set or some other control bit is set). (We assume that
160 * the caller has already made sure the packet is IP proto TCP).
162 if ((ip->ip_off & htons(0x3fff)) || m->m_len < 40) {
163 log_Printf(LogDEBUG, "??? 1 ip_off = %x, m_len = %lu\n",
164 ip->ip_off, (unsigned long)m->m_len);
165 log_DumpBp(LogDEBUG, "", m);
166 return (TYPE_IP);
168 th = (struct tcphdr *) & ((int *) ip)[hlen];
169 if ((th->th_flags & (TH_SYN | TH_FIN | TH_RST | TH_ACK)) != TH_ACK) {
170 log_Printf(LogDEBUG, "??? 2 th_flags = %x\n", th->th_flags);
171 log_DumpBp(LogDEBUG, "", m);
172 return (TYPE_IP);
176 * Packet is compressible -- we're going to send either a COMPRESSED_TCP or
177 * UNCOMPRESSED_TCP packet. Either way we need to locate (or create) the
178 * connection state. Special case the most recently used connection since
179 * it's most likely to be used again & we don't have to do any reordering
180 * if it's used.
182 slstat->sls_packets++;
183 if (ip->ip_src.s_addr != cs->cs_ip.ip_src.s_addr ||
184 ip->ip_dst.s_addr != cs->cs_ip.ip_dst.s_addr ||
185 *(int *) th != ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl]) {
188 * Wasn't the first -- search for it.
190 * States are kept in a circularly linked list with last_cs pointing to the
191 * end of the list. The list is kept in lru order by moving a state to
192 * the head of the list whenever it is referenced. Since the list is
193 * short and, empirically, the connection we want is almost always near
194 * the front, we locate states via linear search. If we don't find a
195 * state for the datagram, the oldest state is (re-)used.
197 struct cstate *lcs;
198 struct cstate *lastcs = comp->last_cs;
200 do {
201 lcs = cs;
202 cs = cs->cs_next;
203 slstat->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 && *(int *) th == ((int *) &cs->cs_ip)[cs->cs_ip.ip_hl])
207 goto found;
208 } while (cs != lastcs);
211 * Didn't find it -- re-use oldest cstate. Send an uncompressed packet
212 * that tells the other side what connection number we're using for this
213 * conversation. Note that since the state list is circular, the oldest
214 * state points to the newest and we only need to set last_cs to update
215 * the lru linkage.
217 slstat->sls_misses++;
218 comp->last_cs = lcs;
219 #define THOFFSET(th) (th->th_off)
220 hlen += th->th_off;
221 hlen <<= 2;
222 if (hlen > m->m_len)
223 return (TYPE_IP);
224 goto uncompressed;
226 found:
229 * Found it -- move to the front on the connection list.
231 if (cs == lastcs)
232 comp->last_cs = lcs;
233 else {
234 lcs->cs_next = cs->cs_next;
235 cs->cs_next = lastcs->cs_next;
236 lastcs->cs_next = cs;
241 * Make sure that only what we expect to change changed. The first line of
242 * the `if' checks the IP protocol version, header length & type of
243 * service. The 2nd line checks the "Don't fragment" bit. The 3rd line
244 * checks the time-to-live and protocol (the protocol check is unnecessary
245 * but costless). The 4th line checks the TCP header length. The 5th line
246 * checks IP options, if any. The 6th line checks TCP options, if any. If
247 * any of these things are different between the previous & current
248 * datagram, we send the current datagram `uncompressed'.
250 oth = (struct tcphdr *) & ((int *) &cs->cs_ip)[hlen];
251 deltaS = hlen;
252 hlen += th->th_off;
253 hlen <<= 2;
254 if (hlen > m->m_len)
255 return (TYPE_IP);
257 if (((u_short *) ip)[0] != ((u_short *) & cs->cs_ip)[0] ||
258 ((u_short *) ip)[3] != ((u_short *) & cs->cs_ip)[3] ||
259 ((u_short *) ip)[4] != ((u_short *) & cs->cs_ip)[4] ||
260 THOFFSET(th) != THOFFSET(oth) ||
261 (deltaS > 5 &&
262 memcmp(ip + 1, &cs->cs_ip + 1, (deltaS - 5) << 2)) ||
263 (THOFFSET(th) > 5 &&
264 memcmp(th + 1, oth + 1, (THOFFSET(th) - 5) << 2))) {
265 goto uncompressed;
269 * Figure out which of the changing fields changed. The receiver expects
270 * changes in the order: urgent, window, ack, seq (the order minimizes the
271 * number of temporaries needed in this section of code).
273 if (th->th_flags & TH_URG) {
274 deltaS = ntohs(th->th_urp);
275 ENCODEZ(deltaS);
276 changes |= NEW_U;
277 } else if (th->th_urp != oth->th_urp) {
280 * argh! URG not set but urp changed -- a sensible implementation should
281 * never do this but RFC793 doesn't prohibit the change so we have to
282 * deal with it.
284 goto uncompressed;
286 deltaS = (u_short) (ntohs(th->th_win) - ntohs(oth->th_win));
287 if (deltaS) {
288 ENCODE(deltaS);
289 changes |= NEW_W;
291 deltaA = ntohl(th->th_ack) - ntohl(oth->th_ack);
292 if (deltaA) {
293 if (deltaA > 0xffff) {
294 goto uncompressed;
296 ENCODE(deltaA);
297 changes |= NEW_A;
299 deltaS = ntohl(th->th_seq) - ntohl(oth->th_seq);
300 if (deltaS) {
301 if (deltaS > 0xffff) {
302 goto uncompressed;
304 ENCODE(deltaS);
305 changes |= NEW_S;
307 switch (changes) {
309 case 0:
312 * Nothing changed. If this packet contains data and the last one didn't,
313 * this is probably a data packet following an ack (normal on an
314 * interactive connection) and we send it compressed. Otherwise it's
315 * probably a retransmit, retransmitted ack or window probe. Send it
316 * uncompressed in case the other side missed the compressed version.
318 if (ip->ip_len != cs->cs_ip.ip_len &&
319 ntohs(cs->cs_ip.ip_len) == hlen)
320 break;
322 /* FALLTHROUGH */
324 case SPECIAL_I:
325 case SPECIAL_D:
328 * actual changes match one of our special case encodings -- send packet
329 * uncompressed.
331 goto uncompressed;
333 case NEW_S | NEW_A:
334 if (deltaS == deltaA &&
335 deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
336 /* special case for echoed terminal traffic */
337 changes = SPECIAL_I;
338 cp = new_seq;
340 break;
342 case NEW_S:
343 if (deltaS == ntohs(cs->cs_ip.ip_len) - hlen) {
344 /* special case for data xfer */
345 changes = SPECIAL_D;
346 cp = new_seq;
348 break;
351 deltaS = ntohs(ip->ip_id) - ntohs(cs->cs_ip.ip_id);
352 if (deltaS != 1) {
353 ENCODEZ(deltaS);
354 changes |= NEW_I;
356 if (th->th_flags & TH_PUSH)
357 changes |= TCP_PUSH_BIT;
360 * Grab the cksum before we overwrite it below. Then update our state with
361 * this packet's header.
363 deltaA = ntohs(th->th_sum);
364 memcpy(&cs->cs_ip, ip, hlen);
367 * We want to use the original packet as our compressed packet. (cp -
368 * new_seq) is the number of bytes we need for compressed sequence numbers.
369 * In addition we need one byte for the change mask, one for the connection
370 * id and two for the tcp checksum. So, (cp - new_seq) + 4 bytes of header
371 * are needed. hlen is how many bytes of the original packet to toss so
372 * subtract the two to get the new packet size.
374 deltaS = cp - new_seq;
375 cp = (u_char *) ip;
378 * Since fastq traffic can jump ahead of the background traffic, we don't
379 * know what order packets will go on the line. In this case, we always
380 * send a "new" connection id so the receiver state stays synchronized.
382 if (comp->last_xmit == cs->cs_id && compress_cid) {
383 hlen -= deltaS + 3;
384 cp += hlen;
385 *cp++ = changes;
386 } else {
387 comp->last_xmit = cs->cs_id;
388 hlen -= deltaS + 4;
389 cp += hlen;
390 *cp++ = changes | NEW_C;
391 *cp++ = cs->cs_id;
393 m->m_len -= hlen;
394 m->m_offset += hlen;
395 *cp++ = deltaA >> 8;
396 *cp++ = deltaA;
397 memcpy(cp, new_seq, deltaS);
398 slstat->sls_compressed++;
399 return (TYPE_COMPRESSED_TCP);
402 * Update connection state cs & send uncompressed packet ('uncompressed'
403 * means a regular ip/tcp packet but with the 'conversation id' we hope to
404 * use on future compressed packets in the protocol field).
406 uncompressed:
407 memcpy(&cs->cs_ip, ip, hlen);
408 ip->ip_p = cs->cs_id;
409 comp->last_xmit = cs->cs_id;
410 return (TYPE_UNCOMPRESSED_TCP);
415 sl_uncompress_tcp(u_char ** bufp, int len, u_int type, struct slcompress *comp,
416 struct slstat *slstat, int max_state)
418 u_char *cp;
419 u_int hlen, changes;
420 struct tcphdr *th;
421 struct cstate *cs;
422 struct ip *ip;
423 u_short *bp;
425 switch (type) {
427 case TYPE_UNCOMPRESSED_TCP:
428 ip = (struct ip *) * bufp;
429 if (ip->ip_p > max_state)
430 goto bad;
431 cs = &comp->rstate[comp->last_recv = ip->ip_p];
432 comp->flags &= ~SLF_TOSS;
433 ip->ip_p = IPPROTO_TCP;
436 * Calculate the size of the TCP/IP header and make sure that we don't
437 * overflow the space we have available for it.
439 hlen = ip->ip_hl << 2;
440 if (hlen + sizeof(struct tcphdr) > len)
441 goto bad;
442 th = (struct tcphdr *) & ((char *) ip)[hlen];
443 hlen += THOFFSET(th) << 2;
444 if (hlen > MAX_HDR)
445 goto bad;
446 memcpy(&cs->cs_ip, ip, hlen);
447 cs->cs_hlen = hlen;
448 slstat->sls_uncompressedin++;
449 return (len);
451 default:
452 goto bad;
454 case TYPE_COMPRESSED_TCP:
455 break;
458 /* We've got a compressed packet. */
459 slstat->sls_compressedin++;
460 cp = *bufp;
461 changes = *cp++;
462 log_Printf(LogDEBUG, "compressed: changes = %02x\n", changes);
464 if (changes & NEW_C) {
466 * Make sure the state index is in range, then grab the state. If we have
467 * a good state index, clear the 'discard' flag.
469 if (*cp > max_state || comp->last_recv == 255)
470 goto bad;
472 comp->flags &= ~SLF_TOSS;
473 comp->last_recv = *cp++;
474 } else {
476 * this packet has an implicit state index. If we've had a line error
477 * since the last time we got an explicit state index, we have to toss
478 * the packet.
480 if (comp->flags & SLF_TOSS) {
481 slstat->sls_tossed++;
482 return (0);
485 cs = &comp->rstate[comp->last_recv];
486 hlen = cs->cs_ip.ip_hl << 2;
487 th = (struct tcphdr *) & ((u_char *) & cs->cs_ip)[hlen];
488 th->th_sum = htons((*cp << 8) | cp[1]);
489 cp += 2;
490 if (changes & TCP_PUSH_BIT)
491 th->th_flags |= TH_PUSH;
492 else
493 th->th_flags &= ~TH_PUSH;
495 switch (changes & SPECIALS_MASK) {
496 case SPECIAL_I:
498 u_int i;
500 i = ntohs(cs->cs_ip.ip_len) - cs->cs_hlen;
501 th->th_ack = htonl(ntohl(th->th_ack) + i);
502 th->th_seq = htonl(ntohl(th->th_seq) + i);
504 break;
506 case SPECIAL_D:
507 th->th_seq = htonl(ntohl(th->th_seq) + ntohs(cs->cs_ip.ip_len)
508 - cs->cs_hlen);
509 break;
511 default:
512 if (changes & NEW_U) {
513 th->th_flags |= TH_URG;
514 DECODEU(th->th_urp)
515 } else
516 th->th_flags &= ~TH_URG;
517 if (changes & NEW_W)
518 DECODES(th->th_win)
519 if (changes & NEW_A)
520 DECODEL(th->th_ack)
521 if (changes & NEW_S) {
522 log_Printf(LogDEBUG, "NEW_S: %02x, %02x, %02x\n",
523 *cp, cp[1], cp[2]);
524 DECODEL(th->th_seq)
526 break;
528 if (changes & NEW_I) {
529 DECODES(cs->cs_ip.ip_id)
530 } else
531 cs->cs_ip.ip_id = htons(ntohs(cs->cs_ip.ip_id) + 1);
533 log_Printf(LogDEBUG, "Uncompress: id = %04x, seq = %08lx\n",
534 cs->cs_ip.ip_id, (u_long)ntohl(th->th_seq));
537 * At this point, cp points to the first byte of data in the packet.
538 * Back up cp by the tcp/ip header length to make room for the
539 * reconstructed header (we assume the packet we were handed has enough
540 * space to prepend 128 bytes of header). Adjust the length to account
541 * for the new header & fill in the IP total length.
543 len -= (cp - *bufp);
544 if (len < 0)
546 * we must have dropped some characters (crc should detect this but the
547 * old slip framing won't)
549 goto bad;
551 *bufp = cp - cs->cs_hlen;
552 len += cs->cs_hlen;
553 cs->cs_ip.ip_len = htons(len);
555 /* recompute the ip header checksum */
556 cs->cs_ip.ip_sum = 0;
557 bp = (u_short *)&cs->cs_ip;
558 for (changes = 0; hlen > 0; hlen -= 2)
559 changes += *bp++;
560 changes = (changes & 0xffff) + (changes >> 16);
561 changes = (changes & 0xffff) + (changes >> 16);
562 cs->cs_ip.ip_sum = ~changes;
564 /* And copy the result into our buffer */
565 memcpy(*bufp, &cs->cs_ip, cs->cs_hlen);
567 return (len);
568 bad:
569 comp->flags |= SLF_TOSS;
570 slstat->sls_errorin++;
571 return (0);
575 sl_Show(struct cmdargs const *arg)
577 prompt_Printf(arg->prompt, "VJ compression statistics:\n");
578 prompt_Printf(arg->prompt, " Out: %d (compress) / %d (total)",
579 arg->bundle->ncp.ipcp.vj.slstat.sls_compressed,
580 arg->bundle->ncp.ipcp.vj.slstat.sls_packets);
581 prompt_Printf(arg->prompt, " %d (miss) / %d (search)\n",
582 arg->bundle->ncp.ipcp.vj.slstat.sls_misses,
583 arg->bundle->ncp.ipcp.vj.slstat.sls_searches);
584 prompt_Printf(arg->prompt, " In: %d (compress), %d (uncompress)",
585 arg->bundle->ncp.ipcp.vj.slstat.sls_compressedin,
586 arg->bundle->ncp.ipcp.vj.slstat.sls_uncompressedin);
587 prompt_Printf(arg->prompt, " %d (error), %d (tossed)\n",
588 arg->bundle->ncp.ipcp.vj.slstat.sls_errorin,
589 arg->bundle->ncp.ipcp.vj.slstat.sls_tossed);
590 return 0;