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,
28 #define RCSID "$Id: ccp.c,v 1.31 2001/02/22 03:10:06 paulus Exp $"
36 #include <net/ppp-comp.h>
38 static const char rcsid[] = RCSID;
41 * Unfortunately there is a bug in zlib which means that using a
42 * size of 8 (window size = 256) for Deflate compression will cause
43 * buffer overruns and kernel crashes in the deflate module.
44 * Until this is fixed we only accept sizes in the range 9 .. 15.
45 * Thanks to James Carlson for pointing this out.
47 #define DEFLATE_MIN_WORKS 9
50 * Command-line options.
52 static int setbsdcomp __P((char **));
53 static int setdeflate __P((char **));
55 static option_t ccp_option_list[] = {
56 { "noccp", o_bool, &ccp_protent.enabled_flag,
57 "Disable CCP negotiation" },
58 { "-ccp", o_bool, &ccp_protent.enabled_flag,
59 "Disable CCP negotiation" },
60 { "bsdcomp", o_special, (void *)setbsdcomp,
61 "Request BSD-Compress packet compression" },
62 { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
63 "don't allow BSD-Compress", OPT_A2COPY,
64 &ccp_allowoptions[0].bsd_compress },
65 { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
66 "don't allow BSD-Compress", OPT_A2COPY,
67 &ccp_allowoptions[0].bsd_compress },
68 { "deflate", o_special, (void *)setdeflate,
69 "request Deflate compression" },
70 { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
71 "don't allow Deflate compression", OPT_A2COPY,
72 &ccp_allowoptions[0].deflate },
73 { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
74 "don't allow Deflate compression", OPT_A2COPY,
75 &ccp_allowoptions[0].deflate },
76 { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
77 "don't use draft deflate #", OPT_A2COPY,
78 &ccp_allowoptions[0].deflate_draft },
79 { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
80 "request Predictor-1", 1, &ccp_allowoptions[0].predictor_1 },
81 { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
82 "don't allow Predictor-1", OPT_A2COPY,
83 &ccp_allowoptions[0].predictor_1 },
84 { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
85 "don't allow Predictor-1", OPT_A2COPY,
86 &ccp_allowoptions[0].predictor_1 },
92 * Protocol entry points from main code.
94 static void ccp_init __P((int unit));
95 static void ccp_open __P((int unit));
96 static void ccp_close __P((int unit, char *));
97 static void ccp_lowerup __P((int unit));
98 static void ccp_lowerdown __P((int));
99 static void ccp_input __P((int unit, u_char *pkt, int len));
100 static void ccp_protrej __P((int unit));
101 static int ccp_printpkt __P((u_char *pkt, int len,
102 void (*printer) __P((void *, char *, ...)),
104 static void ccp_datainput __P((int unit, u_char *pkt, int len));
106 struct protent ccp_protent = {
126 fsm ccp_fsm[NUM_PPP];
127 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
128 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
129 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
130 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
133 * Callbacks for fsm code.
135 static void ccp_resetci __P((fsm *));
136 static int ccp_cilen __P((fsm *));
137 static void ccp_addci __P((fsm *, u_char *, int *));
138 static int ccp_ackci __P((fsm *, u_char *, int));
139 static int ccp_nakci __P((fsm *, u_char *, int));
140 static int ccp_rejci __P((fsm *, u_char *, int));
141 static int ccp_reqci __P((fsm *, u_char *, int *, int));
142 static void ccp_up __P((fsm *));
143 static void ccp_down __P((fsm *));
144 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
145 static void ccp_rack_timeout __P((void *));
146 static char *method_name __P((ccp_options *, ccp_options *));
148 static fsm_callbacks ccp_callbacks = {
167 * Do we want / did we get any compression?
169 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
170 || (opt).predictor_1 || (opt).predictor_2)
173 * Local state (mainly for handling reset-reqs and reset-acks).
175 static int ccp_localstate[NUM_PPP];
176 #define RACK_PENDING 1 /* waiting for reset-ack */
177 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
179 #define RACKTIMEOUT 1 /* second */
181 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
194 abits = rbits = strtol(str, &endp, 0);
195 if (endp != str && *endp == ',') {
197 abits = strtol(str, &endp, 0);
199 if (*endp != 0 || endp == str) {
200 option_error("invalid parameter '%s' for bsdcomp option", *argv);
203 if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
204 || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
205 option_error("bsdcomp option values must be 0 or %d .. %d",
206 BSD_MIN_BITS, BSD_MAX_BITS);
210 ccp_wantoptions[0].bsd_compress = 1;
211 ccp_wantoptions[0].bsd_bits = rbits;
213 ccp_wantoptions[0].bsd_compress = 0;
215 ccp_allowoptions[0].bsd_compress = 1;
216 ccp_allowoptions[0].bsd_bits = abits;
218 ccp_allowoptions[0].bsd_compress = 0;
230 abits = rbits = strtol(str, &endp, 0);
231 if (endp != str && *endp == ',') {
233 abits = strtol(str, &endp, 0);
235 if (*endp != 0 || endp == str) {
236 option_error("invalid parameter '%s' for deflate option", *argv);
239 if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
240 || (abits != 0 && (abits < DEFLATE_MIN_SIZE
241 || abits > DEFLATE_MAX_SIZE))) {
242 option_error("deflate option values must be 0 or %d .. %d",
243 DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
246 if (rbits == DEFLATE_MIN_SIZE || abits == DEFLATE_MIN_SIZE) {
247 if (rbits == DEFLATE_MIN_SIZE)
248 rbits = DEFLATE_MIN_WORKS;
249 if (abits == DEFLATE_MIN_SIZE)
250 abits = DEFLATE_MIN_WORKS;
251 warn("deflate option value of %d changed to %d to avoid zlib bug",
252 DEFLATE_MIN_SIZE, DEFLATE_MIN_WORKS);
255 ccp_wantoptions[0].deflate = 1;
256 ccp_wantoptions[0].deflate_size = rbits;
258 ccp_wantoptions[0].deflate = 0;
260 ccp_allowoptions[0].deflate = 1;
261 ccp_allowoptions[0].deflate_size = abits;
263 ccp_allowoptions[0].deflate = 0;
269 * ccp_init - initialize CCP.
275 fsm *f = &ccp_fsm[unit];
278 f->protocol = PPP_CCP;
279 f->callbacks = &ccp_callbacks;
282 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
283 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
284 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
285 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
287 ccp_wantoptions[0].deflate = 1;
288 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
289 ccp_wantoptions[0].deflate_correct = 1;
290 ccp_wantoptions[0].deflate_draft = 1;
291 ccp_allowoptions[0].deflate = 1;
292 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
293 ccp_allowoptions[0].deflate_correct = 1;
294 ccp_allowoptions[0].deflate_draft = 1;
296 ccp_wantoptions[0].bsd_compress = 1;
297 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
298 ccp_allowoptions[0].bsd_compress = 1;
299 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
301 ccp_allowoptions[0].predictor_1 = 1;
305 * ccp_open - CCP is allowed to come up.
311 fsm *f = &ccp_fsm[unit];
313 if (f->state != OPENED)
314 ccp_flags_set(unit, 1, 0);
317 * Find out which compressors the kernel supports before
318 * deciding whether to open in silent mode.
321 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
322 f->flags |= OPT_SILENT;
328 * ccp_close - Terminate CCP.
331 ccp_close(unit, reason)
335 ccp_flags_set(unit, 0, 0);
336 fsm_close(&ccp_fsm[unit], reason);
340 * ccp_lowerup - we may now transmit CCP packets.
346 fsm_lowerup(&ccp_fsm[unit]);
350 * ccp_lowerdown - we may not transmit CCP packets.
356 fsm_lowerdown(&ccp_fsm[unit]);
360 * ccp_input - process a received CCP packet.
363 ccp_input(unit, p, len)
368 fsm *f = &ccp_fsm[unit];
372 * Check for a terminate-request so we can print a message.
375 fsm_input(f, p, len);
376 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
377 notice("Compression disabled by peer.");
380 * If we get a terminate-ack and we're not asking for compression,
383 if (oldstate == REQSENT && p[0] == TERMACK
384 && !ANY_COMPRESS(ccp_gotoptions[unit]))
385 ccp_close(unit, "No compression negotiated");
389 * Handle a CCP-specific code.
392 ccp_extcode(f, code, id, p, len)
400 if (f->state != OPENED)
402 /* send a reset-ack, which the transmitter will see and
403 reset its compression state. */
404 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
408 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
409 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
410 UNTIMEOUT(ccp_rack_timeout, f);
422 * ccp_protrej - peer doesn't talk CCP.
428 ccp_flags_set(unit, 0, 0);
429 fsm_lowerdown(&ccp_fsm[unit]);
433 * ccp_resetci - initialize at start of negotiation.
439 ccp_options *go = &ccp_gotoptions[f->unit];
442 *go = ccp_wantoptions[f->unit];
443 all_rejected[f->unit] = 0;
446 * Check whether the kernel knows about the various
447 * compression methods we might request.
449 if (go->bsd_compress) {
450 opt_buf[0] = CI_BSD_COMPRESS;
451 opt_buf[1] = CILEN_BSD_COMPRESS;
452 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
453 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
454 go->bsd_compress = 0;
457 if (go->deflate_correct) {
458 opt_buf[0] = CI_DEFLATE;
459 opt_buf[1] = CILEN_DEFLATE;
460 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
461 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
462 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
463 go->deflate_correct = 0;
465 if (go->deflate_draft) {
466 opt_buf[0] = CI_DEFLATE_DRAFT;
467 opt_buf[1] = CILEN_DEFLATE;
468 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_WORKS);
469 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
470 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
471 go->deflate_draft = 0;
473 if (!go->deflate_correct && !go->deflate_draft)
476 if (go->predictor_1) {
477 opt_buf[0] = CI_PREDICTOR_1;
478 opt_buf[1] = CILEN_PREDICTOR_1;
479 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
482 if (go->predictor_2) {
483 opt_buf[0] = CI_PREDICTOR_2;
484 opt_buf[1] = CILEN_PREDICTOR_2;
485 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
491 * ccp_cilen - Return total length of our configuration info.
497 ccp_options *go = &ccp_gotoptions[f->unit];
499 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
500 + (go->deflate? CILEN_DEFLATE: 0)
501 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
502 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
506 * ccp_addci - put our requests in a packet.
509 ccp_addci(f, p, lenp)
515 ccp_options *go = &ccp_gotoptions[f->unit];
519 * Add the compression types that we can receive, in decreasing
520 * preference order. Get the kernel to allocate the first one
521 * in case it gets Acked.
524 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
525 p[1] = CILEN_DEFLATE;
526 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
527 p[3] = DEFLATE_CHK_SEQUENCE;
529 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
534 if (res < 0 || go->deflate_size <= DEFLATE_MIN_WORKS) {
539 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
541 if (p != p0 && go->deflate_correct && go->deflate_draft) {
542 p[0] = CI_DEFLATE_DRAFT;
543 p[1] = CILEN_DEFLATE;
544 p[2] = p[2 - CILEN_DEFLATE];
545 p[3] = DEFLATE_CHK_SEQUENCE;
549 if (go->bsd_compress) {
550 p[0] = CI_BSD_COMPRESS;
551 p[1] = CILEN_BSD_COMPRESS;
552 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
554 p += CILEN_BSD_COMPRESS; /* not the first option */
557 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
559 p += CILEN_BSD_COMPRESS;
562 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
563 go->bsd_compress = 0;
567 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
571 /* XXX Should Predictor 2 be preferable to Predictor 1? */
572 if (go->predictor_1) {
573 p[0] = CI_PREDICTOR_1;
574 p[1] = CILEN_PREDICTOR_1;
575 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
578 p += CILEN_PREDICTOR_1;
581 if (go->predictor_2) {
582 p[0] = CI_PREDICTOR_2;
583 p[1] = CILEN_PREDICTOR_2;
584 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
587 p += CILEN_PREDICTOR_2;
591 go->method = (p > p0)? p0[0]: -1;
597 * ccp_ackci - process a received configure-ack, and return
598 * 1 iff the packet was OK.
606 ccp_options *go = &ccp_gotoptions[f->unit];
610 if (len < CILEN_DEFLATE
611 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
612 || p[1] != CILEN_DEFLATE
613 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
614 || p[3] != DEFLATE_CHK_SEQUENCE)
617 len -= CILEN_DEFLATE;
618 /* XXX Cope with first/fast ack */
621 if (go->deflate_correct && go->deflate_draft) {
622 if (len < CILEN_DEFLATE
623 || p[0] != CI_DEFLATE_DRAFT
624 || p[1] != CILEN_DEFLATE
625 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
626 || p[3] != DEFLATE_CHK_SEQUENCE)
629 len -= CILEN_DEFLATE;
632 if (go->bsd_compress) {
633 if (len < CILEN_BSD_COMPRESS
634 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
635 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
637 p += CILEN_BSD_COMPRESS;
638 len -= CILEN_BSD_COMPRESS;
639 /* XXX Cope with first/fast ack */
640 if (p == p0 && len == 0)
643 if (go->predictor_1) {
644 if (len < CILEN_PREDICTOR_1
645 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
647 p += CILEN_PREDICTOR_1;
648 len -= CILEN_PREDICTOR_1;
649 /* XXX Cope with first/fast ack */
650 if (p == p0 && len == 0)
653 if (go->predictor_2) {
654 if (len < CILEN_PREDICTOR_2
655 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
657 p += CILEN_PREDICTOR_2;
658 len -= CILEN_PREDICTOR_2;
659 /* XXX Cope with first/fast ack */
660 if (p == p0 && len == 0)
670 * ccp_nakci - process received configure-nak.
671 * Returns 1 iff the nak was OK.
679 ccp_options *go = &ccp_gotoptions[f->unit];
680 ccp_options no; /* options we've seen already */
681 ccp_options try; /* options to ask for next time */
683 memset(&no, 0, sizeof(no));
686 if (go->deflate && len >= CILEN_DEFLATE
687 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
688 && p[1] == CILEN_DEFLATE) {
691 * Peer wants us to use a different code size or something.
692 * Stop asking for Deflate if we don't understand his suggestion.
694 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
695 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_WORKS
696 || p[3] != DEFLATE_CHK_SEQUENCE)
698 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
699 try.deflate_size = DEFLATE_SIZE(p[2]);
701 len -= CILEN_DEFLATE;
702 if (go->deflate_correct && go->deflate_draft
703 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
704 && p[1] == CILEN_DEFLATE) {
706 len -= CILEN_DEFLATE;
710 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
711 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
714 * Peer wants us to use a different number of bits
715 * or a different version.
717 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
718 try.bsd_compress = 0;
719 else if (BSD_NBITS(p[2]) < go->bsd_bits)
720 try.bsd_bits = BSD_NBITS(p[2]);
721 p += CILEN_BSD_COMPRESS;
722 len -= CILEN_BSD_COMPRESS;
726 * Predictor-1 and 2 have no options, so they can't be Naked.
728 * There may be remaining options but we ignore them.
731 if (f->state != OPENED)
737 * ccp_rejci - reject some of our suggested compression methods.
745 ccp_options *go = &ccp_gotoptions[f->unit];
746 ccp_options try; /* options to request next time */
751 * Cope with empty configure-rejects by ceasing to send
752 * configure-requests.
754 if (len == 0 && all_rejected[f->unit])
757 if (go->deflate && len >= CILEN_DEFLATE
758 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
759 && p[1] == CILEN_DEFLATE) {
760 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
761 || p[3] != DEFLATE_CHK_SEQUENCE)
762 return 0; /* Rej is bad */
763 if (go->deflate_correct)
764 try.deflate_correct = 0;
766 try.deflate_draft = 0;
768 len -= CILEN_DEFLATE;
769 if (go->deflate_correct && go->deflate_draft
770 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
771 && p[1] == CILEN_DEFLATE) {
772 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
773 || p[3] != DEFLATE_CHK_SEQUENCE)
774 return 0; /* Rej is bad */
775 try.deflate_draft = 0;
777 len -= CILEN_DEFLATE;
779 if (!try.deflate_correct && !try.deflate_draft)
782 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
783 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
784 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
786 try.bsd_compress = 0;
787 p += CILEN_BSD_COMPRESS;
788 len -= CILEN_BSD_COMPRESS;
790 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
791 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
793 p += CILEN_PREDICTOR_1;
794 len -= CILEN_PREDICTOR_1;
796 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
797 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
799 p += CILEN_PREDICTOR_2;
800 len -= CILEN_PREDICTOR_2;
806 if (f->state != OPENED)
813 * ccp_reqci - processed a received configure-request.
814 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
818 ccp_reqci(f, p, lenp, dont_nak)
824 int ret, newret, res;
826 int len, clen, type, nb;
827 ccp_options *ho = &ccp_hisoptions[f->unit];
828 ccp_options *ao = &ccp_allowoptions[f->unit];
834 memset(ho, 0, sizeof(ccp_options));
835 ho->method = (len > 0)? p[0]: -1;
839 if (len < 2 || p[1] < 2 || p[1] > len) {
850 case CI_DEFLATE_DRAFT:
851 if (!ao->deflate || clen != CILEN_DEFLATE
852 || (!ao->deflate_correct && type == CI_DEFLATE)
853 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
859 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
860 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
861 || p[3] != DEFLATE_CHK_SEQUENCE
862 || nb > ao->deflate_size || nb < DEFLATE_MIN_WORKS) {
865 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
866 p[3] = DEFLATE_CHK_SEQUENCE;
867 /* fall through to test this #bits below */
873 * Check whether we can do Deflate with the window
874 * size they want. If the window is too big, reduce
875 * it until the kernel can cope and nak with that.
876 * We only check this for the first option.
880 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
882 break; /* it's OK now */
883 if (res < 0 || nb == DEFLATE_MIN_WORKS || dont_nak) {
885 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
890 p[2] = DEFLATE_MAKE_OPT(nb);
895 case CI_BSD_COMPRESS:
896 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
901 ho->bsd_compress = 1;
902 ho->bsd_bits = nb = BSD_NBITS(p[2]);
903 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
904 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
907 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
908 /* fall through to test this #bits below */
914 * Check whether we can do BSD-Compress with the code
915 * size they want. If the code size is too big, reduce
916 * it until the kernel can cope and nak with that.
917 * We only check this for the first option.
921 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
924 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
926 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
932 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
938 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
945 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
951 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
958 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
968 if (newret == CONFNAK && dont_nak)
970 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
971 /* we're returning this option */
972 if (newret == CONFREJ && ret == CONFNAK)
976 BCOPY(p, retp, clen);
984 if (ret != CONFACK) {
985 if (ret == CONFREJ && *lenp == retp - p0)
986 all_rejected[f->unit] = 1;
994 * Make a string name for a compression method (or 2).
997 method_name(opt, opt2)
998 ccp_options *opt, *opt2;
1000 static char result[64];
1002 if (!ANY_COMPRESS(*opt))
1004 switch (opt->method) {
1006 case CI_DEFLATE_DRAFT:
1007 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
1008 slprintf(result, sizeof(result), "Deflate%s (%d/%d)",
1009 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1010 opt->deflate_size, opt2->deflate_size);
1012 slprintf(result, sizeof(result), "Deflate%s (%d)",
1013 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1016 case CI_BSD_COMPRESS:
1017 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1018 slprintf(result, sizeof(result), "BSD-Compress (%d/%d)",
1019 opt->bsd_bits, opt2->bsd_bits);
1021 slprintf(result, sizeof(result), "BSD-Compress (%d)",
1024 case CI_PREDICTOR_1:
1025 return "Predictor 1";
1026 case CI_PREDICTOR_2:
1027 return "Predictor 2";
1029 slprintf(result, sizeof(result), "Method %d", opt->method);
1035 * CCP has come up - inform the kernel driver and log a message.
1041 ccp_options *go = &ccp_gotoptions[f->unit];
1042 ccp_options *ho = &ccp_hisoptions[f->unit];
1045 ccp_flags_set(f->unit, 1, 1);
1046 if (ANY_COMPRESS(*go)) {
1047 if (ANY_COMPRESS(*ho)) {
1048 if (go->method == ho->method) {
1049 notice("%s compression enabled", method_name(go, ho));
1051 strlcpy(method1, method_name(go, NULL), sizeof(method1));
1052 notice("%s / %s compression enabled",
1053 method1, method_name(ho, NULL));
1056 notice("%s receive compression enabled", method_name(go, NULL));
1057 } else if (ANY_COMPRESS(*ho))
1058 notice("%s transmit compression enabled", method_name(ho, NULL));
1062 * CCP has gone down - inform the kernel driver.
1068 if (ccp_localstate[f->unit] & RACK_PENDING)
1069 UNTIMEOUT(ccp_rack_timeout, f);
1070 ccp_localstate[f->unit] = 0;
1071 ccp_flags_set(f->unit, 1, 0);
1075 * Print the contents of a CCP packet.
1077 static char *ccp_codenames[] = {
1078 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1079 "TermReq", "TermAck", "CodeRej",
1080 NULL, NULL, NULL, NULL, NULL, NULL,
1081 "ResetReq", "ResetAck",
1085 ccp_printpkt(p, plen, printer, arg)
1088 void (*printer) __P((void *, char *, ...));
1091 u_char *p0, *optend;
1096 if (plen < HEADERLEN)
1100 len = (p[2] << 8) + p[3];
1101 if (len < HEADERLEN || len > plen)
1104 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1105 && ccp_codenames[code-1] != NULL)
1106 printer(arg, " %s", ccp_codenames[code-1]);
1108 printer(arg, " code=0x%x", code);
1109 printer(arg, " id=0x%x", id);
1118 /* print list of possible compression methods */
1122 if (optlen < 2 || optlen > len)
1126 optend = p + optlen;
1129 case CI_DEFLATE_DRAFT:
1130 if (optlen >= CILEN_DEFLATE) {
1131 printer(arg, "deflate%s %d",
1132 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1133 DEFLATE_SIZE(p[2]));
1134 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1135 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1136 if (p[3] != DEFLATE_CHK_SEQUENCE)
1137 printer(arg, " check %d", p[3]);
1141 case CI_BSD_COMPRESS:
1142 if (optlen >= CILEN_BSD_COMPRESS) {
1143 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1145 p += CILEN_BSD_COMPRESS;
1148 case CI_PREDICTOR_1:
1149 if (optlen >= CILEN_PREDICTOR_1) {
1150 printer(arg, "predictor 1");
1151 p += CILEN_PREDICTOR_1;
1154 case CI_PREDICTOR_2:
1155 if (optlen >= CILEN_PREDICTOR_2) {
1156 printer(arg, "predictor 2");
1157 p += CILEN_PREDICTOR_2;
1162 printer(arg, " %.2x", *p++);
1169 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1170 print_string((char *)p, len, printer, arg);
1177 /* dump out the rest of the packet in hex */
1179 printer(arg, " %.2x", *p++);
1185 * We have received a packet that the decompressor failed to
1186 * decompress. Here we would expect to issue a reset-request, but
1187 * Motorola has a patent on resetting the compressor as a result of
1188 * detecting an error in the decompressed data after decompression.
1189 * (See US patent 5,130,993; international patent publication number
1190 * WO 91/10289; Australian patent 73296/91.)
1192 * So we ask the kernel whether the error was detected after
1193 * decompression; if it was, we take CCP down, thus disabling
1194 * compression :-(, otherwise we issue the reset-request.
1197 ccp_datainput(unit, pkt, len)
1205 if (f->state == OPENED) {
1206 if (ccp_fatal_error(unit)) {
1208 * Disable compression by taking CCP down.
1210 error("Lost compression sync: disabling compression");
1211 ccp_close(unit, "Lost compression sync");
1214 * Send a reset-request to reset the peer's compressor.
1215 * We don't do that if we are still waiting for an
1216 * acknowledgement to a previous reset-request.
1218 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1219 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1220 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1221 ccp_localstate[f->unit] |= RACK_PENDING;
1223 ccp_localstate[f->unit] |= RREQ_REPEAT;
1229 * Timeout waiting for reset-ack.
1232 ccp_rack_timeout(arg)
1237 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1238 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1239 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1240 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1242 ccp_localstate[f->unit] &= ~RACK_PENDING;