2 * ccp.c - PPP Compression Control Protocol.
4 * Copyright (c) 1994 The Australian National University.
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation is hereby granted, provided that the above copyright
9 * notice appears in all copies. This software is provided without any
10 * warranty, express or implied. The Australian National University
11 * makes no representations about the suitability of this software for
14 * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17 * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
20 * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
23 * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
29 static char rcsid[] = "$Id: ccp.c,v 1.25 1999/03/16 03:15:12 paulus Exp $";
38 #include <net/ppp-comp.h>
41 * Command-line options.
43 static int setbsdcomp __P((char **));
44 static int setdeflate __P((char **));
46 static option_t ccp_option_list[] = {
47 { "noccp", o_bool, &ccp_protent.enabled_flag,
48 "Disable CCP negotiation" },
49 { "-ccp", o_bool, &ccp_protent.enabled_flag,
50 "Disable CCP negotiation" },
51 { "bsdcomp", o_special, setbsdcomp,
52 "Request BSD-Compress packet compression" },
53 { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
54 "don't allow BSD-Compress", OPT_A2COPY,
55 &ccp_allowoptions[0].bsd_compress },
56 { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
57 "don't allow BSD-Compress", OPT_A2COPY,
58 &ccp_allowoptions[0].bsd_compress },
59 { "deflate", 1, setdeflate,
60 "request Deflate compression" },
61 { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
62 "don't allow Deflate compression", OPT_A2COPY,
63 &ccp_allowoptions[0].deflate },
64 { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
65 "don't allow Deflate compression", OPT_A2COPY,
66 &ccp_allowoptions[0].deflate },
67 { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
68 "don't use draft deflate #", OPT_A2COPY,
69 &ccp_allowoptions[0].deflate_draft },
70 { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
71 "request Predictor-1", 1, &ccp_allowoptions[0].predictor_1 },
72 { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
73 "don't allow Predictor-1", OPT_A2COPY,
74 &ccp_allowoptions[0].predictor_1 },
75 { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
76 "don't allow Predictor-1", OPT_A2COPY,
77 &ccp_allowoptions[0].predictor_1 },
83 * Protocol entry points from main code.
85 static void ccp_init __P((int unit));
86 static void ccp_open __P((int unit));
87 static void ccp_close __P((int unit, char *));
88 static void ccp_lowerup __P((int unit));
89 static void ccp_lowerdown __P((int));
90 static void ccp_input __P((int unit, u_char *pkt, int len));
91 static void ccp_protrej __P((int unit));
92 static int ccp_printpkt __P((u_char *pkt, int len,
93 void (*printer) __P((void *, char *, ...)),
95 static void ccp_datainput __P((int unit, u_char *pkt, int len));
97 struct protent ccp_protent = {
116 fsm ccp_fsm[NUM_PPP];
117 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
118 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
119 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
120 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
123 * Callbacks for fsm code.
125 static void ccp_resetci __P((fsm *));
126 static int ccp_cilen __P((fsm *));
127 static void ccp_addci __P((fsm *, u_char *, int *));
128 static int ccp_ackci __P((fsm *, u_char *, int));
129 static int ccp_nakci __P((fsm *, u_char *, int));
130 static int ccp_rejci __P((fsm *, u_char *, int));
131 static int ccp_reqci __P((fsm *, u_char *, int *, int));
132 static void ccp_up __P((fsm *));
133 static void ccp_down __P((fsm *));
134 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
135 static void ccp_rack_timeout __P((void *));
136 static char *method_name __P((ccp_options *, ccp_options *));
138 static fsm_callbacks ccp_callbacks = {
157 * Do we want / did we get any compression?
159 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
160 || (opt).predictor_1 || (opt).predictor_2)
163 * Local state (mainly for handling reset-reqs and reset-acks).
165 static int ccp_localstate[NUM_PPP];
166 #define RACK_PENDING 1 /* waiting for reset-ack */
167 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
169 #define RACKTIMEOUT 1 /* second */
171 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
184 abits = rbits = strtol(str, &endp, 0);
185 if (endp != str && *endp == ',') {
187 abits = strtol(str, &endp, 0);
189 if (*endp != 0 || endp == str) {
190 option_error("invalid parameter '%s' for bsdcomp option", *argv);
193 if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
194 || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
195 option_error("bsdcomp option values must be 0 or %d .. %d",
196 BSD_MIN_BITS, BSD_MAX_BITS);
200 ccp_wantoptions[0].bsd_compress = 1;
201 ccp_wantoptions[0].bsd_bits = rbits;
203 ccp_wantoptions[0].bsd_compress = 0;
205 ccp_allowoptions[0].bsd_compress = 1;
206 ccp_allowoptions[0].bsd_bits = abits;
208 ccp_allowoptions[0].bsd_compress = 0;
220 abits = rbits = strtol(str, &endp, 0);
221 if (endp != str && *endp == ',') {
223 abits = strtol(str, &endp, 0);
225 if (*endp != 0 || endp == str) {
226 option_error("invalid parameter '%s' for deflate option", *argv);
229 if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
230 || (abits != 0 && (abits < DEFLATE_MIN_SIZE
231 || abits > DEFLATE_MAX_SIZE))) {
232 option_error("deflate option values must be 0 or %d .. %d",
233 DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
237 ccp_wantoptions[0].deflate = 1;
238 ccp_wantoptions[0].deflate_size = rbits;
240 ccp_wantoptions[0].deflate = 0;
242 ccp_allowoptions[0].deflate = 1;
243 ccp_allowoptions[0].deflate_size = abits;
245 ccp_allowoptions[0].deflate = 0;
251 * ccp_init - initialize CCP.
257 fsm *f = &ccp_fsm[unit];
260 f->protocol = PPP_CCP;
261 f->callbacks = &ccp_callbacks;
264 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
265 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
266 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
267 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
269 ccp_wantoptions[0].deflate = 1;
270 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
271 ccp_wantoptions[0].deflate_correct = 1;
272 ccp_wantoptions[0].deflate_draft = 1;
273 ccp_allowoptions[0].deflate = 1;
274 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
275 ccp_allowoptions[0].deflate_correct = 1;
276 ccp_allowoptions[0].deflate_draft = 1;
278 ccp_wantoptions[0].bsd_compress = 1;
279 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
280 ccp_allowoptions[0].bsd_compress = 1;
281 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
283 ccp_allowoptions[0].predictor_1 = 1;
287 * ccp_open - CCP is allowed to come up.
293 fsm *f = &ccp_fsm[unit];
295 if (f->state != OPENED)
296 ccp_flags_set(unit, 1, 0);
299 * Find out which compressors the kernel supports before
300 * deciding whether to open in silent mode.
303 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
304 f->flags |= OPT_SILENT;
310 * ccp_close - Terminate CCP.
313 ccp_close(unit, reason)
317 ccp_flags_set(unit, 0, 0);
318 fsm_close(&ccp_fsm[unit], reason);
322 * ccp_lowerup - we may now transmit CCP packets.
328 fsm_lowerup(&ccp_fsm[unit]);
332 * ccp_lowerdown - we may not transmit CCP packets.
338 fsm_lowerdown(&ccp_fsm[unit]);
342 * ccp_input - process a received CCP packet.
345 ccp_input(unit, p, len)
350 fsm *f = &ccp_fsm[unit];
354 * Check for a terminate-request so we can print a message.
357 fsm_input(f, p, len);
358 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
359 notice("Compression disabled by peer.");
362 * If we get a terminate-ack and we're not asking for compression,
365 if (oldstate == REQSENT && p[0] == TERMACK
366 && !ANY_COMPRESS(ccp_gotoptions[unit]))
367 ccp_close(unit, "No compression negotiated");
371 * Handle a CCP-specific code.
374 ccp_extcode(f, code, id, p, len)
382 if (f->state != OPENED)
384 /* send a reset-ack, which the transmitter will see and
385 reset its compression state. */
386 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
390 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
391 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
392 UNTIMEOUT(ccp_rack_timeout, f);
404 * ccp_protrej - peer doesn't talk CCP.
410 ccp_flags_set(unit, 0, 0);
411 fsm_lowerdown(&ccp_fsm[unit]);
415 * ccp_resetci - initialize at start of negotiation.
421 ccp_options *go = &ccp_gotoptions[f->unit];
424 *go = ccp_wantoptions[f->unit];
425 all_rejected[f->unit] = 0;
428 * Check whether the kernel knows about the various
429 * compression methods we might request.
431 if (go->bsd_compress) {
432 opt_buf[0] = CI_BSD_COMPRESS;
433 opt_buf[1] = CILEN_BSD_COMPRESS;
434 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
435 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
436 go->bsd_compress = 0;
439 if (go->deflate_correct) {
440 opt_buf[0] = CI_DEFLATE;
441 opt_buf[1] = CILEN_DEFLATE;
442 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
443 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
444 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
445 go->deflate_correct = 0;
447 if (go->deflate_draft) {
448 opt_buf[0] = CI_DEFLATE_DRAFT;
449 opt_buf[1] = CILEN_DEFLATE;
450 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
451 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
452 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
453 go->deflate_draft = 0;
455 if (!go->deflate_correct && !go->deflate_draft)
458 if (go->predictor_1) {
459 opt_buf[0] = CI_PREDICTOR_1;
460 opt_buf[1] = CILEN_PREDICTOR_1;
461 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
464 if (go->predictor_2) {
465 opt_buf[0] = CI_PREDICTOR_2;
466 opt_buf[1] = CILEN_PREDICTOR_2;
467 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
473 * ccp_cilen - Return total length of our configuration info.
479 ccp_options *go = &ccp_gotoptions[f->unit];
481 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
482 + (go->deflate? CILEN_DEFLATE: 0)
483 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
484 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
488 * ccp_addci - put our requests in a packet.
491 ccp_addci(f, p, lenp)
497 ccp_options *go = &ccp_gotoptions[f->unit];
501 * Add the compression types that we can receive, in decreasing
502 * preference order. Get the kernel to allocate the first one
503 * in case it gets Acked.
506 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
507 p[1] = CILEN_DEFLATE;
508 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
509 p[3] = DEFLATE_CHK_SEQUENCE;
511 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
516 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
521 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
523 if (p != p0 && go->deflate_correct && go->deflate_draft) {
524 p[0] = CI_DEFLATE_DRAFT;
525 p[1] = CILEN_DEFLATE;
526 p[2] = p[2 - CILEN_DEFLATE];
527 p[3] = DEFLATE_CHK_SEQUENCE;
531 if (go->bsd_compress) {
532 p[0] = CI_BSD_COMPRESS;
533 p[1] = CILEN_BSD_COMPRESS;
534 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
536 p += CILEN_BSD_COMPRESS; /* not the first option */
539 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
541 p += CILEN_BSD_COMPRESS;
544 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
545 go->bsd_compress = 0;
549 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
553 /* XXX Should Predictor 2 be preferable to Predictor 1? */
554 if (go->predictor_1) {
555 p[0] = CI_PREDICTOR_1;
556 p[1] = CILEN_PREDICTOR_1;
557 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
560 p += CILEN_PREDICTOR_1;
563 if (go->predictor_2) {
564 p[0] = CI_PREDICTOR_2;
565 p[1] = CILEN_PREDICTOR_2;
566 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
569 p += CILEN_PREDICTOR_2;
573 go->method = (p > p0)? p0[0]: -1;
579 * ccp_ackci - process a received configure-ack, and return
580 * 1 iff the packet was OK.
588 ccp_options *go = &ccp_gotoptions[f->unit];
592 if (len < CILEN_DEFLATE
593 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
594 || p[1] != CILEN_DEFLATE
595 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
596 || p[3] != DEFLATE_CHK_SEQUENCE)
599 len -= CILEN_DEFLATE;
600 /* XXX Cope with first/fast ack */
603 if (go->deflate_correct && go->deflate_draft) {
604 if (len < CILEN_DEFLATE
605 || p[0] != CI_DEFLATE_DRAFT
606 || p[1] != CILEN_DEFLATE
607 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
608 || p[3] != DEFLATE_CHK_SEQUENCE)
611 len -= CILEN_DEFLATE;
614 if (go->bsd_compress) {
615 if (len < CILEN_BSD_COMPRESS
616 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
617 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
619 p += CILEN_BSD_COMPRESS;
620 len -= CILEN_BSD_COMPRESS;
621 /* XXX Cope with first/fast ack */
622 if (p == p0 && len == 0)
625 if (go->predictor_1) {
626 if (len < CILEN_PREDICTOR_1
627 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
629 p += CILEN_PREDICTOR_1;
630 len -= CILEN_PREDICTOR_1;
631 /* XXX Cope with first/fast ack */
632 if (p == p0 && len == 0)
635 if (go->predictor_2) {
636 if (len < CILEN_PREDICTOR_2
637 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
639 p += CILEN_PREDICTOR_2;
640 len -= CILEN_PREDICTOR_2;
641 /* XXX Cope with first/fast ack */
642 if (p == p0 && len == 0)
652 * ccp_nakci - process received configure-nak.
653 * Returns 1 iff the nak was OK.
661 ccp_options *go = &ccp_gotoptions[f->unit];
662 ccp_options no; /* options we've seen already */
663 ccp_options try; /* options to ask for next time */
665 memset(&no, 0, sizeof(no));
668 if (go->deflate && len >= CILEN_DEFLATE
669 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
670 && p[1] == CILEN_DEFLATE) {
673 * Peer wants us to use a different code size or something.
674 * Stop asking for Deflate if we don't understand his suggestion.
676 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
677 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
678 || p[3] != DEFLATE_CHK_SEQUENCE)
680 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
681 try.deflate_size = DEFLATE_SIZE(p[2]);
683 len -= CILEN_DEFLATE;
684 if (go->deflate_correct && go->deflate_draft
685 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
686 && p[1] == CILEN_DEFLATE) {
688 len -= CILEN_DEFLATE;
692 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
693 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
696 * Peer wants us to use a different number of bits
697 * or a different version.
699 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
700 try.bsd_compress = 0;
701 else if (BSD_NBITS(p[2]) < go->bsd_bits)
702 try.bsd_bits = BSD_NBITS(p[2]);
703 p += CILEN_BSD_COMPRESS;
704 len -= CILEN_BSD_COMPRESS;
708 * Predictor-1 and 2 have no options, so they can't be Naked.
710 * XXX What should we do with any remaining options?
716 if (f->state != OPENED)
722 * ccp_rejci - reject some of our suggested compression methods.
730 ccp_options *go = &ccp_gotoptions[f->unit];
731 ccp_options try; /* options to request next time */
736 * Cope with empty configure-rejects by ceasing to send
737 * configure-requests.
739 if (len == 0 && all_rejected[f->unit])
742 if (go->deflate && len >= CILEN_DEFLATE
743 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
744 && p[1] == CILEN_DEFLATE) {
745 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
746 || p[3] != DEFLATE_CHK_SEQUENCE)
747 return 0; /* Rej is bad */
748 if (go->deflate_correct)
749 try.deflate_correct = 0;
751 try.deflate_draft = 0;
753 len -= CILEN_DEFLATE;
754 if (go->deflate_correct && go->deflate_draft
755 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
756 && p[1] == CILEN_DEFLATE) {
757 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
758 || p[3] != DEFLATE_CHK_SEQUENCE)
759 return 0; /* Rej is bad */
760 try.deflate_draft = 0;
762 len -= CILEN_DEFLATE;
764 if (!try.deflate_correct && !try.deflate_draft)
767 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
768 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
769 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
771 try.bsd_compress = 0;
772 p += CILEN_BSD_COMPRESS;
773 len -= CILEN_BSD_COMPRESS;
775 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
776 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
778 p += CILEN_PREDICTOR_1;
779 len -= CILEN_PREDICTOR_1;
781 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
782 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
784 p += CILEN_PREDICTOR_2;
785 len -= CILEN_PREDICTOR_2;
791 if (f->state != OPENED)
798 * ccp_reqci - processed a received configure-request.
799 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
803 ccp_reqci(f, p, lenp, dont_nak)
809 int ret, newret, res;
811 int len, clen, type, nb;
812 ccp_options *ho = &ccp_hisoptions[f->unit];
813 ccp_options *ao = &ccp_allowoptions[f->unit];
819 memset(ho, 0, sizeof(ccp_options));
820 ho->method = (len > 0)? p[0]: -1;
824 if (len < 2 || p[1] < 2 || p[1] > len) {
835 case CI_DEFLATE_DRAFT:
836 if (!ao->deflate || clen != CILEN_DEFLATE
837 || (!ao->deflate_correct && type == CI_DEFLATE)
838 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
844 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
845 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
846 || p[3] != DEFLATE_CHK_SEQUENCE
847 || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
850 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
851 p[3] = DEFLATE_CHK_SEQUENCE;
852 /* fall through to test this #bits below */
858 * Check whether we can do Deflate with the window
859 * size they want. If the window is too big, reduce
860 * it until the kernel can cope and nak with that.
861 * We only check this for the first option.
865 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
867 break; /* it's OK now */
868 if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
870 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
875 p[2] = DEFLATE_MAKE_OPT(nb);
880 case CI_BSD_COMPRESS:
881 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
886 ho->bsd_compress = 1;
887 ho->bsd_bits = nb = BSD_NBITS(p[2]);
888 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
889 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
892 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
893 /* fall through to test this #bits below */
899 * Check whether we can do BSD-Compress with the code
900 * size they want. If the code size is too big, reduce
901 * it until the kernel can cope and nak with that.
902 * We only check this for the first option.
906 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
909 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
911 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
917 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
923 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
930 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
936 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
943 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
953 if (newret == CONFNAK && dont_nak)
955 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
956 /* we're returning this option */
957 if (newret == CONFREJ && ret == CONFNAK)
961 BCOPY(p, retp, clen);
969 if (ret != CONFACK) {
970 if (ret == CONFREJ && *lenp == retp - p0)
971 all_rejected[f->unit] = 1;
979 * Make a string name for a compression method (or 2).
982 method_name(opt, opt2)
983 ccp_options *opt, *opt2;
985 static char result[64];
987 if (!ANY_COMPRESS(*opt))
989 switch (opt->method) {
991 case CI_DEFLATE_DRAFT:
992 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
993 slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
994 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
995 opt->deflate_size, opt2->deflate_size);
997 slprintf(result, sizeof(result), "Deflate%s (%d)",
998 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1001 case CI_BSD_COMPRESS:
1002 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1003 slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1004 opt->bsd_bits, opt2->bsd_bits);
1006 slprintf(result, sizeof(result), "BSD-Compress (%d)",
1009 case CI_PREDICTOR_1:
1010 return "Predictor 1";
1011 case CI_PREDICTOR_2:
1012 return "Predictor 2";
1014 slprintf(result, sizeof(result), "Method %d", opt->method);
1020 * CCP has come up - inform the kernel driver and log a message.
1026 ccp_options *go = &ccp_gotoptions[f->unit];
1027 ccp_options *ho = &ccp_hisoptions[f->unit];
1030 ccp_flags_set(f->unit, 1, 1);
1031 if (ANY_COMPRESS(*go)) {
1032 if (ANY_COMPRESS(*ho)) {
1033 if (go->method == ho->method) {
1034 notice("%s compression enabled", method_name(go, ho));
1036 strlcpy(method1, sizeof(method1), method_name(go, NULL));
1037 notice("%s / %s compression enabled",
1038 method1, method_name(ho, NULL));
1041 notice("%s receive compression enabled", method_name(go, NULL));
1042 } else if (ANY_COMPRESS(*ho))
1043 notice("%s transmit compression enabled", method_name(ho, NULL));
1047 * CCP has gone down - inform the kernel driver.
1053 if (ccp_localstate[f->unit] & RACK_PENDING)
1054 UNTIMEOUT(ccp_rack_timeout, f);
1055 ccp_localstate[f->unit] = 0;
1056 ccp_flags_set(f->unit, 1, 0);
1060 * Print the contents of a CCP packet.
1062 static char *ccp_codenames[] = {
1063 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1064 "TermReq", "TermAck", "CodeRej",
1065 NULL, NULL, NULL, NULL, NULL, NULL,
1066 "ResetReq", "ResetAck",
1070 ccp_printpkt(p, plen, printer, arg)
1073 void (*printer) __P((void *, char *, ...));
1076 u_char *p0, *optend;
1081 if (plen < HEADERLEN)
1085 len = (p[2] << 8) + p[3];
1086 if (len < HEADERLEN || len > plen)
1089 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1090 && ccp_codenames[code-1] != NULL)
1091 printer(arg, " %s", ccp_codenames[code-1]);
1093 printer(arg, " code=0x%x", code);
1094 printer(arg, " id=0x%x", id);
1103 /* print list of possible compression methods */
1107 if (optlen < 2 || optlen > len)
1111 optend = p + optlen;
1114 case CI_DEFLATE_DRAFT:
1115 if (optlen >= CILEN_DEFLATE) {
1116 printer(arg, "deflate%s %d",
1117 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1118 DEFLATE_SIZE(p[2]));
1119 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1120 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1121 if (p[3] != DEFLATE_CHK_SEQUENCE)
1122 printer(arg, " check %d", p[3]);
1126 case CI_BSD_COMPRESS:
1127 if (optlen >= CILEN_BSD_COMPRESS) {
1128 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1130 p += CILEN_BSD_COMPRESS;
1133 case CI_PREDICTOR_1:
1134 if (optlen >= CILEN_PREDICTOR_1) {
1135 printer(arg, "predictor 1");
1136 p += CILEN_PREDICTOR_1;
1139 case CI_PREDICTOR_2:
1140 if (optlen >= CILEN_PREDICTOR_2) {
1141 printer(arg, "predictor 2");
1142 p += CILEN_PREDICTOR_2;
1147 printer(arg, " %.2x", *p++);
1154 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1155 print_string(p, len, printer, arg);
1162 /* dump out the rest of the packet in hex */
1164 printer(arg, " %.2x", *p++);
1170 * We have received a packet that the decompressor failed to
1171 * decompress. Here we would expect to issue a reset-request, but
1172 * Motorola has a patent on resetting the compressor as a result of
1173 * detecting an error in the decompressed data after decompression.
1174 * (See US patent 5,130,993; international patent publication number
1175 * WO 91/10289; Australian patent 73296/91.)
1177 * So we ask the kernel whether the error was detected after
1178 * decompression; if it was, we take CCP down, thus disabling
1179 * compression :-(, otherwise we issue the reset-request.
1182 ccp_datainput(unit, pkt, len)
1190 if (f->state == OPENED) {
1191 if (ccp_fatal_error(unit)) {
1193 * Disable compression by taking CCP down.
1195 error("Lost compression sync: disabling compression");
1196 ccp_close(unit, "Lost compression sync");
1199 * Send a reset-request to reset the peer's compressor.
1200 * We don't do that if we are still waiting for an
1201 * acknowledgement to a previous reset-request.
1203 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1204 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1205 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1206 ccp_localstate[f->unit] |= RACK_PENDING;
1208 ccp_localstate[f->unit] |= RREQ_REPEAT;
1214 * Timeout waiting for reset-ack.
1217 ccp_rack_timeout(arg)
1222 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1223 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1224 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1225 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1227 ccp_localstate[f->unit] &= ~RACK_PENDING;