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.26 1999/03/16 22:54:38 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 = {
117 fsm ccp_fsm[NUM_PPP];
118 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
119 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
120 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
121 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
124 * Callbacks for fsm code.
126 static void ccp_resetci __P((fsm *));
127 static int ccp_cilen __P((fsm *));
128 static void ccp_addci __P((fsm *, u_char *, int *));
129 static int ccp_ackci __P((fsm *, u_char *, int));
130 static int ccp_nakci __P((fsm *, u_char *, int));
131 static int ccp_rejci __P((fsm *, u_char *, int));
132 static int ccp_reqci __P((fsm *, u_char *, int *, int));
133 static void ccp_up __P((fsm *));
134 static void ccp_down __P((fsm *));
135 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
136 static void ccp_rack_timeout __P((void *));
137 static char *method_name __P((ccp_options *, ccp_options *));
139 static fsm_callbacks ccp_callbacks = {
158 * Do we want / did we get any compression?
160 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
161 || (opt).predictor_1 || (opt).predictor_2)
164 * Local state (mainly for handling reset-reqs and reset-acks).
166 static int ccp_localstate[NUM_PPP];
167 #define RACK_PENDING 1 /* waiting for reset-ack */
168 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
170 #define RACKTIMEOUT 1 /* second */
172 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
185 abits = rbits = strtol(str, &endp, 0);
186 if (endp != str && *endp == ',') {
188 abits = strtol(str, &endp, 0);
190 if (*endp != 0 || endp == str) {
191 option_error("invalid parameter '%s' for bsdcomp option", *argv);
194 if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
195 || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
196 option_error("bsdcomp option values must be 0 or %d .. %d",
197 BSD_MIN_BITS, BSD_MAX_BITS);
201 ccp_wantoptions[0].bsd_compress = 1;
202 ccp_wantoptions[0].bsd_bits = rbits;
204 ccp_wantoptions[0].bsd_compress = 0;
206 ccp_allowoptions[0].bsd_compress = 1;
207 ccp_allowoptions[0].bsd_bits = abits;
209 ccp_allowoptions[0].bsd_compress = 0;
221 abits = rbits = strtol(str, &endp, 0);
222 if (endp != str && *endp == ',') {
224 abits = strtol(str, &endp, 0);
226 if (*endp != 0 || endp == str) {
227 option_error("invalid parameter '%s' for deflate option", *argv);
230 if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
231 || (abits != 0 && (abits < DEFLATE_MIN_SIZE
232 || abits > DEFLATE_MAX_SIZE))) {
233 option_error("deflate option values must be 0 or %d .. %d",
234 DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
238 ccp_wantoptions[0].deflate = 1;
239 ccp_wantoptions[0].deflate_size = rbits;
241 ccp_wantoptions[0].deflate = 0;
243 ccp_allowoptions[0].deflate = 1;
244 ccp_allowoptions[0].deflate_size = abits;
246 ccp_allowoptions[0].deflate = 0;
252 * ccp_init - initialize CCP.
258 fsm *f = &ccp_fsm[unit];
261 f->protocol = PPP_CCP;
262 f->callbacks = &ccp_callbacks;
265 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
266 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
267 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
268 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
270 ccp_wantoptions[0].deflate = 1;
271 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
272 ccp_wantoptions[0].deflate_correct = 1;
273 ccp_wantoptions[0].deflate_draft = 1;
274 ccp_allowoptions[0].deflate = 1;
275 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
276 ccp_allowoptions[0].deflate_correct = 1;
277 ccp_allowoptions[0].deflate_draft = 1;
279 ccp_wantoptions[0].bsd_compress = 1;
280 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
281 ccp_allowoptions[0].bsd_compress = 1;
282 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
284 ccp_allowoptions[0].predictor_1 = 1;
288 * ccp_open - CCP is allowed to come up.
294 fsm *f = &ccp_fsm[unit];
296 if (f->state != OPENED)
297 ccp_flags_set(unit, 1, 0);
300 * Find out which compressors the kernel supports before
301 * deciding whether to open in silent mode.
304 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
305 f->flags |= OPT_SILENT;
311 * ccp_close - Terminate CCP.
314 ccp_close(unit, reason)
318 ccp_flags_set(unit, 0, 0);
319 fsm_close(&ccp_fsm[unit], reason);
323 * ccp_lowerup - we may now transmit CCP packets.
329 fsm_lowerup(&ccp_fsm[unit]);
333 * ccp_lowerdown - we may not transmit CCP packets.
339 fsm_lowerdown(&ccp_fsm[unit]);
343 * ccp_input - process a received CCP packet.
346 ccp_input(unit, p, len)
351 fsm *f = &ccp_fsm[unit];
355 * Check for a terminate-request so we can print a message.
358 fsm_input(f, p, len);
359 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
360 notice("Compression disabled by peer.");
363 * If we get a terminate-ack and we're not asking for compression,
366 if (oldstate == REQSENT && p[0] == TERMACK
367 && !ANY_COMPRESS(ccp_gotoptions[unit]))
368 ccp_close(unit, "No compression negotiated");
372 * Handle a CCP-specific code.
375 ccp_extcode(f, code, id, p, len)
383 if (f->state != OPENED)
385 /* send a reset-ack, which the transmitter will see and
386 reset its compression state. */
387 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
391 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
392 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
393 UNTIMEOUT(ccp_rack_timeout, f);
405 * ccp_protrej - peer doesn't talk CCP.
411 ccp_flags_set(unit, 0, 0);
412 fsm_lowerdown(&ccp_fsm[unit]);
416 * ccp_resetci - initialize at start of negotiation.
422 ccp_options *go = &ccp_gotoptions[f->unit];
425 *go = ccp_wantoptions[f->unit];
426 all_rejected[f->unit] = 0;
429 * Check whether the kernel knows about the various
430 * compression methods we might request.
432 if (go->bsd_compress) {
433 opt_buf[0] = CI_BSD_COMPRESS;
434 opt_buf[1] = CILEN_BSD_COMPRESS;
435 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
436 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
437 go->bsd_compress = 0;
440 if (go->deflate_correct) {
441 opt_buf[0] = CI_DEFLATE;
442 opt_buf[1] = CILEN_DEFLATE;
443 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
444 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
445 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
446 go->deflate_correct = 0;
448 if (go->deflate_draft) {
449 opt_buf[0] = CI_DEFLATE_DRAFT;
450 opt_buf[1] = CILEN_DEFLATE;
451 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
452 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
453 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
454 go->deflate_draft = 0;
456 if (!go->deflate_correct && !go->deflate_draft)
459 if (go->predictor_1) {
460 opt_buf[0] = CI_PREDICTOR_1;
461 opt_buf[1] = CILEN_PREDICTOR_1;
462 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
465 if (go->predictor_2) {
466 opt_buf[0] = CI_PREDICTOR_2;
467 opt_buf[1] = CILEN_PREDICTOR_2;
468 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
474 * ccp_cilen - Return total length of our configuration info.
480 ccp_options *go = &ccp_gotoptions[f->unit];
482 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
483 + (go->deflate? CILEN_DEFLATE: 0)
484 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
485 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
489 * ccp_addci - put our requests in a packet.
492 ccp_addci(f, p, lenp)
498 ccp_options *go = &ccp_gotoptions[f->unit];
502 * Add the compression types that we can receive, in decreasing
503 * preference order. Get the kernel to allocate the first one
504 * in case it gets Acked.
507 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
508 p[1] = CILEN_DEFLATE;
509 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
510 p[3] = DEFLATE_CHK_SEQUENCE;
512 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
517 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
522 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
524 if (p != p0 && go->deflate_correct && go->deflate_draft) {
525 p[0] = CI_DEFLATE_DRAFT;
526 p[1] = CILEN_DEFLATE;
527 p[2] = p[2 - CILEN_DEFLATE];
528 p[3] = DEFLATE_CHK_SEQUENCE;
532 if (go->bsd_compress) {
533 p[0] = CI_BSD_COMPRESS;
534 p[1] = CILEN_BSD_COMPRESS;
535 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
537 p += CILEN_BSD_COMPRESS; /* not the first option */
540 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
542 p += CILEN_BSD_COMPRESS;
545 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
546 go->bsd_compress = 0;
550 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
554 /* XXX Should Predictor 2 be preferable to Predictor 1? */
555 if (go->predictor_1) {
556 p[0] = CI_PREDICTOR_1;
557 p[1] = CILEN_PREDICTOR_1;
558 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
561 p += CILEN_PREDICTOR_1;
564 if (go->predictor_2) {
565 p[0] = CI_PREDICTOR_2;
566 p[1] = CILEN_PREDICTOR_2;
567 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
570 p += CILEN_PREDICTOR_2;
574 go->method = (p > p0)? p0[0]: -1;
580 * ccp_ackci - process a received configure-ack, and return
581 * 1 iff the packet was OK.
589 ccp_options *go = &ccp_gotoptions[f->unit];
593 if (len < CILEN_DEFLATE
594 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
595 || p[1] != CILEN_DEFLATE
596 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
597 || p[3] != DEFLATE_CHK_SEQUENCE)
600 len -= CILEN_DEFLATE;
601 /* XXX Cope with first/fast ack */
604 if (go->deflate_correct && go->deflate_draft) {
605 if (len < CILEN_DEFLATE
606 || p[0] != CI_DEFLATE_DRAFT
607 || p[1] != CILEN_DEFLATE
608 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
609 || p[3] != DEFLATE_CHK_SEQUENCE)
612 len -= CILEN_DEFLATE;
615 if (go->bsd_compress) {
616 if (len < CILEN_BSD_COMPRESS
617 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
618 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
620 p += CILEN_BSD_COMPRESS;
621 len -= CILEN_BSD_COMPRESS;
622 /* XXX Cope with first/fast ack */
623 if (p == p0 && len == 0)
626 if (go->predictor_1) {
627 if (len < CILEN_PREDICTOR_1
628 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
630 p += CILEN_PREDICTOR_1;
631 len -= CILEN_PREDICTOR_1;
632 /* XXX Cope with first/fast ack */
633 if (p == p0 && len == 0)
636 if (go->predictor_2) {
637 if (len < CILEN_PREDICTOR_2
638 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
640 p += CILEN_PREDICTOR_2;
641 len -= CILEN_PREDICTOR_2;
642 /* XXX Cope with first/fast ack */
643 if (p == p0 && len == 0)
653 * ccp_nakci - process received configure-nak.
654 * Returns 1 iff the nak was OK.
662 ccp_options *go = &ccp_gotoptions[f->unit];
663 ccp_options no; /* options we've seen already */
664 ccp_options try; /* options to ask for next time */
666 memset(&no, 0, sizeof(no));
669 if (go->deflate && len >= CILEN_DEFLATE
670 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
671 && p[1] == CILEN_DEFLATE) {
674 * Peer wants us to use a different code size or something.
675 * Stop asking for Deflate if we don't understand his suggestion.
677 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
678 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
679 || p[3] != DEFLATE_CHK_SEQUENCE)
681 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
682 try.deflate_size = DEFLATE_SIZE(p[2]);
684 len -= CILEN_DEFLATE;
685 if (go->deflate_correct && go->deflate_draft
686 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
687 && p[1] == CILEN_DEFLATE) {
689 len -= CILEN_DEFLATE;
693 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
694 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
697 * Peer wants us to use a different number of bits
698 * or a different version.
700 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
701 try.bsd_compress = 0;
702 else if (BSD_NBITS(p[2]) < go->bsd_bits)
703 try.bsd_bits = BSD_NBITS(p[2]);
704 p += CILEN_BSD_COMPRESS;
705 len -= CILEN_BSD_COMPRESS;
709 * Predictor-1 and 2 have no options, so they can't be Naked.
711 * XXX What should we do with any remaining options?
717 if (f->state != OPENED)
723 * ccp_rejci - reject some of our suggested compression methods.
731 ccp_options *go = &ccp_gotoptions[f->unit];
732 ccp_options try; /* options to request next time */
737 * Cope with empty configure-rejects by ceasing to send
738 * configure-requests.
740 if (len == 0 && all_rejected[f->unit])
743 if (go->deflate && len >= CILEN_DEFLATE
744 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
745 && p[1] == CILEN_DEFLATE) {
746 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
747 || p[3] != DEFLATE_CHK_SEQUENCE)
748 return 0; /* Rej is bad */
749 if (go->deflate_correct)
750 try.deflate_correct = 0;
752 try.deflate_draft = 0;
754 len -= CILEN_DEFLATE;
755 if (go->deflate_correct && go->deflate_draft
756 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
757 && p[1] == CILEN_DEFLATE) {
758 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
759 || p[3] != DEFLATE_CHK_SEQUENCE)
760 return 0; /* Rej is bad */
761 try.deflate_draft = 0;
763 len -= CILEN_DEFLATE;
765 if (!try.deflate_correct && !try.deflate_draft)
768 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
769 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
770 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
772 try.bsd_compress = 0;
773 p += CILEN_BSD_COMPRESS;
774 len -= CILEN_BSD_COMPRESS;
776 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
777 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
779 p += CILEN_PREDICTOR_1;
780 len -= CILEN_PREDICTOR_1;
782 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
783 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
785 p += CILEN_PREDICTOR_2;
786 len -= CILEN_PREDICTOR_2;
792 if (f->state != OPENED)
799 * ccp_reqci - processed a received configure-request.
800 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
804 ccp_reqci(f, p, lenp, dont_nak)
810 int ret, newret, res;
812 int len, clen, type, nb;
813 ccp_options *ho = &ccp_hisoptions[f->unit];
814 ccp_options *ao = &ccp_allowoptions[f->unit];
820 memset(ho, 0, sizeof(ccp_options));
821 ho->method = (len > 0)? p[0]: -1;
825 if (len < 2 || p[1] < 2 || p[1] > len) {
836 case CI_DEFLATE_DRAFT:
837 if (!ao->deflate || clen != CILEN_DEFLATE
838 || (!ao->deflate_correct && type == CI_DEFLATE)
839 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
845 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
846 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
847 || p[3] != DEFLATE_CHK_SEQUENCE
848 || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
851 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
852 p[3] = DEFLATE_CHK_SEQUENCE;
853 /* fall through to test this #bits below */
859 * Check whether we can do Deflate with the window
860 * size they want. If the window is too big, reduce
861 * it until the kernel can cope and nak with that.
862 * We only check this for the first option.
866 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
868 break; /* it's OK now */
869 if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
871 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
876 p[2] = DEFLATE_MAKE_OPT(nb);
881 case CI_BSD_COMPRESS:
882 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
887 ho->bsd_compress = 1;
888 ho->bsd_bits = nb = BSD_NBITS(p[2]);
889 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
890 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
893 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
894 /* fall through to test this #bits below */
900 * Check whether we can do BSD-Compress with the code
901 * size they want. If the code size is too big, reduce
902 * it until the kernel can cope and nak with that.
903 * We only check this for the first option.
907 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
910 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
912 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
918 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
924 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
931 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
937 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
944 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
954 if (newret == CONFNAK && dont_nak)
956 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
957 /* we're returning this option */
958 if (newret == CONFREJ && ret == CONFNAK)
962 BCOPY(p, retp, clen);
970 if (ret != CONFACK) {
971 if (ret == CONFREJ && *lenp == retp - p0)
972 all_rejected[f->unit] = 1;
980 * Make a string name for a compression method (or 2).
983 method_name(opt, opt2)
984 ccp_options *opt, *opt2;
986 static char result[64];
988 if (!ANY_COMPRESS(*opt))
990 switch (opt->method) {
992 case CI_DEFLATE_DRAFT:
993 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
994 slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
995 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
996 opt->deflate_size, opt2->deflate_size);
998 slprintf(result, sizeof(result), "Deflate%s (%d)",
999 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1002 case CI_BSD_COMPRESS:
1003 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1004 slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1005 opt->bsd_bits, opt2->bsd_bits);
1007 slprintf(result, sizeof(result), "BSD-Compress (%d)",
1010 case CI_PREDICTOR_1:
1011 return "Predictor 1";
1012 case CI_PREDICTOR_2:
1013 return "Predictor 2";
1015 slprintf(result, sizeof(result), "Method %d", opt->method);
1021 * CCP has come up - inform the kernel driver and log a message.
1027 ccp_options *go = &ccp_gotoptions[f->unit];
1028 ccp_options *ho = &ccp_hisoptions[f->unit];
1031 ccp_flags_set(f->unit, 1, 1);
1032 if (ANY_COMPRESS(*go)) {
1033 if (ANY_COMPRESS(*ho)) {
1034 if (go->method == ho->method) {
1035 notice("%s compression enabled", method_name(go, ho));
1037 strlcpy(method1, sizeof(method1), method_name(go, NULL));
1038 notice("%s / %s compression enabled",
1039 method1, method_name(ho, NULL));
1042 notice("%s receive compression enabled", method_name(go, NULL));
1043 } else if (ANY_COMPRESS(*ho))
1044 notice("%s transmit compression enabled", method_name(ho, NULL));
1048 * CCP has gone down - inform the kernel driver.
1054 if (ccp_localstate[f->unit] & RACK_PENDING)
1055 UNTIMEOUT(ccp_rack_timeout, f);
1056 ccp_localstate[f->unit] = 0;
1057 ccp_flags_set(f->unit, 1, 0);
1061 * Print the contents of a CCP packet.
1063 static char *ccp_codenames[] = {
1064 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1065 "TermReq", "TermAck", "CodeRej",
1066 NULL, NULL, NULL, NULL, NULL, NULL,
1067 "ResetReq", "ResetAck",
1071 ccp_printpkt(p, plen, printer, arg)
1074 void (*printer) __P((void *, char *, ...));
1077 u_char *p0, *optend;
1082 if (plen < HEADERLEN)
1086 len = (p[2] << 8) + p[3];
1087 if (len < HEADERLEN || len > plen)
1090 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1091 && ccp_codenames[code-1] != NULL)
1092 printer(arg, " %s", ccp_codenames[code-1]);
1094 printer(arg, " code=0x%x", code);
1095 printer(arg, " id=0x%x", id);
1104 /* print list of possible compression methods */
1108 if (optlen < 2 || optlen > len)
1112 optend = p + optlen;
1115 case CI_DEFLATE_DRAFT:
1116 if (optlen >= CILEN_DEFLATE) {
1117 printer(arg, "deflate%s %d",
1118 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1119 DEFLATE_SIZE(p[2]));
1120 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1121 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1122 if (p[3] != DEFLATE_CHK_SEQUENCE)
1123 printer(arg, " check %d", p[3]);
1127 case CI_BSD_COMPRESS:
1128 if (optlen >= CILEN_BSD_COMPRESS) {
1129 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1131 p += CILEN_BSD_COMPRESS;
1134 case CI_PREDICTOR_1:
1135 if (optlen >= CILEN_PREDICTOR_1) {
1136 printer(arg, "predictor 1");
1137 p += CILEN_PREDICTOR_1;
1140 case CI_PREDICTOR_2:
1141 if (optlen >= CILEN_PREDICTOR_2) {
1142 printer(arg, "predictor 2");
1143 p += CILEN_PREDICTOR_2;
1148 printer(arg, " %.2x", *p++);
1155 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1156 print_string(p, len, printer, arg);
1163 /* dump out the rest of the packet in hex */
1165 printer(arg, " %.2x", *p++);
1171 * We have received a packet that the decompressor failed to
1172 * decompress. Here we would expect to issue a reset-request, but
1173 * Motorola has a patent on resetting the compressor as a result of
1174 * detecting an error in the decompressed data after decompression.
1175 * (See US patent 5,130,993; international patent publication number
1176 * WO 91/10289; Australian patent 73296/91.)
1178 * So we ask the kernel whether the error was detected after
1179 * decompression; if it was, we take CCP down, thus disabling
1180 * compression :-(, otherwise we issue the reset-request.
1183 ccp_datainput(unit, pkt, len)
1191 if (f->state == OPENED) {
1192 if (ccp_fatal_error(unit)) {
1194 * Disable compression by taking CCP down.
1196 error("Lost compression sync: disabling compression");
1197 ccp_close(unit, "Lost compression sync");
1200 * Send a reset-request to reset the peer's compressor.
1201 * We don't do that if we are still waiting for an
1202 * acknowledgement to a previous reset-request.
1204 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1205 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1206 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1207 ccp_localstate[f->unit] |= RACK_PENDING;
1209 ccp_localstate[f->unit] |= RREQ_REPEAT;
1215 * Timeout waiting for reset-ack.
1218 ccp_rack_timeout(arg)
1223 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1224 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1225 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1226 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1228 ccp_localstate[f->unit] &= ~RACK_PENDING;