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.24 1999/03/12 06:07:14 paulus Exp $";
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
41 #include <net/ppp-comp.h>
44 * Command-line options.
46 static int setbsdcomp __P((char **));
47 static int setdeflate __P((char **));
49 static option_t ccp_option_list[] = {
50 { "noccp", o_bool, &ccp_protent.enabled_flag,
51 "Disable CCP negotiation" },
52 { "-ccp", o_bool, &ccp_protent.enabled_flag,
53 "Disable CCP negotiation" },
54 { "bsdcomp", o_special, setbsdcomp,
55 "Request BSD-Compress packet compression" },
56 { "nobsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
57 "don't allow BSD-Compress", OPT_A2COPY,
58 &ccp_allowoptions[0].bsd_compress },
59 { "-bsdcomp", o_bool, &ccp_wantoptions[0].bsd_compress,
60 "don't allow BSD-Compress", OPT_A2COPY,
61 &ccp_allowoptions[0].bsd_compress },
62 { "deflate", 1, setdeflate,
63 "request Deflate compression" },
64 { "nodeflate", o_bool, &ccp_wantoptions[0].deflate,
65 "don't allow Deflate compression", OPT_A2COPY,
66 &ccp_allowoptions[0].deflate },
67 { "-deflate", o_bool, &ccp_wantoptions[0].deflate,
68 "don't allow Deflate compression", OPT_A2COPY,
69 &ccp_allowoptions[0].deflate },
70 { "nodeflatedraft", o_bool, &ccp_wantoptions[0].deflate_draft,
71 "don't use draft deflate #", OPT_A2COPY,
72 &ccp_allowoptions[0].deflate_draft },
73 { "predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
74 "request Predictor-1", 1, &ccp_allowoptions[0].predictor_1 },
75 { "nopredictor1", o_bool, &ccp_wantoptions[0].predictor_1,
76 "don't allow Predictor-1", OPT_A2COPY,
77 &ccp_allowoptions[0].predictor_1 },
78 { "-predictor1", o_bool, &ccp_wantoptions[0].predictor_1,
79 "don't allow Predictor-1", OPT_A2COPY,
80 &ccp_allowoptions[0].predictor_1 },
86 * Protocol entry points from main code.
88 static void ccp_init __P((int unit));
89 static void ccp_open __P((int unit));
90 static void ccp_close __P((int unit, char *));
91 static void ccp_lowerup __P((int unit));
92 static void ccp_lowerdown __P((int));
93 static void ccp_input __P((int unit, u_char *pkt, int len));
94 static void ccp_protrej __P((int unit));
95 static int ccp_printpkt __P((u_char *pkt, int len,
96 void (*printer) __P((void *, char *, ...)),
98 static void ccp_datainput __P((int unit, u_char *pkt, int len));
100 struct protent ccp_protent = {
119 fsm ccp_fsm[NUM_PPP];
120 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
121 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
122 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
123 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
126 * Callbacks for fsm code.
128 static void ccp_resetci __P((fsm *));
129 static int ccp_cilen __P((fsm *));
130 static void ccp_addci __P((fsm *, u_char *, int *));
131 static int ccp_ackci __P((fsm *, u_char *, int));
132 static int ccp_nakci __P((fsm *, u_char *, int));
133 static int ccp_rejci __P((fsm *, u_char *, int));
134 static int ccp_reqci __P((fsm *, u_char *, int *, int));
135 static void ccp_up __P((fsm *));
136 static void ccp_down __P((fsm *));
137 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
138 static void ccp_rack_timeout __P((void *));
139 static char *method_name __P((ccp_options *, ccp_options *));
141 static fsm_callbacks ccp_callbacks = {
160 * Do we want / did we get any compression?
162 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
163 || (opt).predictor_1 || (opt).predictor_2)
166 * Local state (mainly for handling reset-reqs and reset-acks).
168 static int ccp_localstate[NUM_PPP];
169 #define RACK_PENDING 1 /* waiting for reset-ack */
170 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
172 #define RACKTIMEOUT 1 /* second */
174 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
187 abits = rbits = strtol(str, &endp, 0);
188 if (endp != str && *endp == ',') {
190 abits = strtol(str, &endp, 0);
192 if (*endp != 0 || endp == str) {
193 option_error("invalid parameter '%s' for bsdcomp option", *argv);
196 if ((rbits != 0 && (rbits < BSD_MIN_BITS || rbits > BSD_MAX_BITS))
197 || (abits != 0 && (abits < BSD_MIN_BITS || abits > BSD_MAX_BITS))) {
198 option_error("bsdcomp option values must be 0 or %d .. %d",
199 BSD_MIN_BITS, BSD_MAX_BITS);
203 ccp_wantoptions[0].bsd_compress = 1;
204 ccp_wantoptions[0].bsd_bits = rbits;
206 ccp_wantoptions[0].bsd_compress = 0;
208 ccp_allowoptions[0].bsd_compress = 1;
209 ccp_allowoptions[0].bsd_bits = abits;
211 ccp_allowoptions[0].bsd_compress = 0;
223 abits = rbits = strtol(str, &endp, 0);
224 if (endp != str && *endp == ',') {
226 abits = strtol(str, &endp, 0);
228 if (*endp != 0 || endp == str) {
229 option_error("invalid parameter '%s' for deflate option", *argv);
232 if ((rbits != 0 && (rbits < DEFLATE_MIN_SIZE || rbits > DEFLATE_MAX_SIZE))
233 || (abits != 0 && (abits < DEFLATE_MIN_SIZE
234 || abits > DEFLATE_MAX_SIZE))) {
235 option_error("deflate option values must be 0 or %d .. %d",
236 DEFLATE_MIN_SIZE, DEFLATE_MAX_SIZE);
240 ccp_wantoptions[0].deflate = 1;
241 ccp_wantoptions[0].deflate_size = rbits;
243 ccp_wantoptions[0].deflate = 0;
245 ccp_allowoptions[0].deflate = 1;
246 ccp_allowoptions[0].deflate_size = abits;
248 ccp_allowoptions[0].deflate = 0;
254 * ccp_init - initialize CCP.
260 fsm *f = &ccp_fsm[unit];
263 f->protocol = PPP_CCP;
264 f->callbacks = &ccp_callbacks;
267 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
268 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
269 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
270 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
272 ccp_wantoptions[0].deflate = 1;
273 ccp_wantoptions[0].deflate_size = DEFLATE_MAX_SIZE;
274 ccp_wantoptions[0].deflate_correct = 1;
275 ccp_wantoptions[0].deflate_draft = 1;
276 ccp_allowoptions[0].deflate = 1;
277 ccp_allowoptions[0].deflate_size = DEFLATE_MAX_SIZE;
278 ccp_allowoptions[0].deflate_correct = 1;
279 ccp_allowoptions[0].deflate_draft = 1;
281 ccp_wantoptions[0].bsd_compress = 1;
282 ccp_wantoptions[0].bsd_bits = BSD_MAX_BITS;
283 ccp_allowoptions[0].bsd_compress = 1;
284 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
286 ccp_allowoptions[0].predictor_1 = 1;
290 * ccp_open - CCP is allowed to come up.
296 fsm *f = &ccp_fsm[unit];
298 if (f->state != OPENED)
299 ccp_flags_set(unit, 1, 0);
302 * Find out which compressors the kernel supports before
303 * deciding whether to open in silent mode.
306 if (!ANY_COMPRESS(ccp_gotoptions[unit]))
307 f->flags |= OPT_SILENT;
313 * ccp_close - Terminate CCP.
316 ccp_close(unit, reason)
320 ccp_flags_set(unit, 0, 0);
321 fsm_close(&ccp_fsm[unit], reason);
325 * ccp_lowerup - we may now transmit CCP packets.
331 fsm_lowerup(&ccp_fsm[unit]);
335 * ccp_lowerdown - we may not transmit CCP packets.
341 fsm_lowerdown(&ccp_fsm[unit]);
345 * ccp_input - process a received CCP packet.
348 ccp_input(unit, p, len)
353 fsm *f = &ccp_fsm[unit];
357 * Check for a terminate-request so we can print a message.
360 fsm_input(f, p, len);
361 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
362 syslog(LOG_NOTICE, "Compression disabled by peer.");
365 * If we get a terminate-ack and we're not asking for compression,
368 if (oldstate == REQSENT && p[0] == TERMACK
369 && !ANY_COMPRESS(ccp_gotoptions[unit]))
370 ccp_close(unit, "No compression negotiated");
374 * Handle a CCP-specific code.
377 ccp_extcode(f, code, id, p, len)
385 if (f->state != OPENED)
387 /* send a reset-ack, which the transmitter will see and
388 reset its compression state. */
389 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
393 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
394 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
395 UNTIMEOUT(ccp_rack_timeout, f);
407 * ccp_protrej - peer doesn't talk CCP.
413 ccp_flags_set(unit, 0, 0);
414 fsm_lowerdown(&ccp_fsm[unit]);
418 * ccp_resetci - initialize at start of negotiation.
424 ccp_options *go = &ccp_gotoptions[f->unit];
427 *go = ccp_wantoptions[f->unit];
428 all_rejected[f->unit] = 0;
431 * Check whether the kernel knows about the various
432 * compression methods we might request.
434 if (go->bsd_compress) {
435 opt_buf[0] = CI_BSD_COMPRESS;
436 opt_buf[1] = CILEN_BSD_COMPRESS;
437 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
438 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
439 go->bsd_compress = 0;
442 if (go->deflate_correct) {
443 opt_buf[0] = CI_DEFLATE;
444 opt_buf[1] = CILEN_DEFLATE;
445 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
446 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
447 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
448 go->deflate_correct = 0;
450 if (go->deflate_draft) {
451 opt_buf[0] = CI_DEFLATE_DRAFT;
452 opt_buf[1] = CILEN_DEFLATE;
453 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
454 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
455 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
456 go->deflate_draft = 0;
458 if (!go->deflate_correct && !go->deflate_draft)
461 if (go->predictor_1) {
462 opt_buf[0] = CI_PREDICTOR_1;
463 opt_buf[1] = CILEN_PREDICTOR_1;
464 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
467 if (go->predictor_2) {
468 opt_buf[0] = CI_PREDICTOR_2;
469 opt_buf[1] = CILEN_PREDICTOR_2;
470 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
476 * ccp_cilen - Return total length of our configuration info.
482 ccp_options *go = &ccp_gotoptions[f->unit];
484 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
485 + (go->deflate? CILEN_DEFLATE: 0)
486 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
487 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
491 * ccp_addci - put our requests in a packet.
494 ccp_addci(f, p, lenp)
500 ccp_options *go = &ccp_gotoptions[f->unit];
504 * Add the compression types that we can receive, in decreasing
505 * preference order. Get the kernel to allocate the first one
506 * in case it gets Acked.
509 p[0] = go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT;
510 p[1] = CILEN_DEFLATE;
511 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
512 p[3] = DEFLATE_CHK_SEQUENCE;
514 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
519 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
524 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
526 if (p != p0 && go->deflate_correct && go->deflate_draft) {
527 p[0] = CI_DEFLATE_DRAFT;
528 p[1] = CILEN_DEFLATE;
529 p[2] = p[2 - CILEN_DEFLATE];
530 p[3] = DEFLATE_CHK_SEQUENCE;
534 if (go->bsd_compress) {
535 p[0] = CI_BSD_COMPRESS;
536 p[1] = CILEN_BSD_COMPRESS;
537 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
539 p += CILEN_BSD_COMPRESS; /* not the first option */
542 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
544 p += CILEN_BSD_COMPRESS;
547 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
548 go->bsd_compress = 0;
552 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
556 /* XXX Should Predictor 2 be preferable to Predictor 1? */
557 if (go->predictor_1) {
558 p[0] = CI_PREDICTOR_1;
559 p[1] = CILEN_PREDICTOR_1;
560 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
563 p += CILEN_PREDICTOR_1;
566 if (go->predictor_2) {
567 p[0] = CI_PREDICTOR_2;
568 p[1] = CILEN_PREDICTOR_2;
569 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
572 p += CILEN_PREDICTOR_2;
576 go->method = (p > p0)? p0[0]: -1;
582 * ccp_ackci - process a received configure-ack, and return
583 * 1 iff the packet was OK.
591 ccp_options *go = &ccp_gotoptions[f->unit];
595 if (len < CILEN_DEFLATE
596 || p[0] != (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
597 || p[1] != CILEN_DEFLATE
598 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
599 || p[3] != DEFLATE_CHK_SEQUENCE)
602 len -= CILEN_DEFLATE;
603 /* XXX Cope with first/fast ack */
606 if (go->deflate_correct && go->deflate_draft) {
607 if (len < CILEN_DEFLATE
608 || p[0] != CI_DEFLATE_DRAFT
609 || p[1] != CILEN_DEFLATE
610 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
611 || p[3] != DEFLATE_CHK_SEQUENCE)
614 len -= CILEN_DEFLATE;
617 if (go->bsd_compress) {
618 if (len < CILEN_BSD_COMPRESS
619 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
620 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
622 p += CILEN_BSD_COMPRESS;
623 len -= CILEN_BSD_COMPRESS;
624 /* XXX Cope with first/fast ack */
625 if (p == p0 && len == 0)
628 if (go->predictor_1) {
629 if (len < CILEN_PREDICTOR_1
630 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
632 p += CILEN_PREDICTOR_1;
633 len -= CILEN_PREDICTOR_1;
634 /* XXX Cope with first/fast ack */
635 if (p == p0 && len == 0)
638 if (go->predictor_2) {
639 if (len < CILEN_PREDICTOR_2
640 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
642 p += CILEN_PREDICTOR_2;
643 len -= CILEN_PREDICTOR_2;
644 /* XXX Cope with first/fast ack */
645 if (p == p0 && len == 0)
655 * ccp_nakci - process received configure-nak.
656 * Returns 1 iff the nak was OK.
664 ccp_options *go = &ccp_gotoptions[f->unit];
665 ccp_options no; /* options we've seen already */
666 ccp_options try; /* options to ask for next time */
668 memset(&no, 0, sizeof(no));
671 if (go->deflate && len >= CILEN_DEFLATE
672 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
673 && p[1] == CILEN_DEFLATE) {
676 * Peer wants us to use a different code size or something.
677 * Stop asking for Deflate if we don't understand his suggestion.
679 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
680 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
681 || p[3] != DEFLATE_CHK_SEQUENCE)
683 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
684 try.deflate_size = DEFLATE_SIZE(p[2]);
686 len -= CILEN_DEFLATE;
687 if (go->deflate_correct && go->deflate_draft
688 && len >= CILEN_DEFLATE && p[0] == CI_DEFLATE_DRAFT
689 && p[1] == CILEN_DEFLATE) {
691 len -= CILEN_DEFLATE;
695 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
696 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
699 * Peer wants us to use a different number of bits
700 * or a different version.
702 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
703 try.bsd_compress = 0;
704 else if (BSD_NBITS(p[2]) < go->bsd_bits)
705 try.bsd_bits = BSD_NBITS(p[2]);
706 p += CILEN_BSD_COMPRESS;
707 len -= CILEN_BSD_COMPRESS;
711 * Predictor-1 and 2 have no options, so they can't be Naked.
713 * XXX What should we do with any remaining options?
719 if (f->state != OPENED)
725 * ccp_rejci - reject some of our suggested compression methods.
733 ccp_options *go = &ccp_gotoptions[f->unit];
734 ccp_options try; /* options to request next time */
739 * Cope with empty configure-rejects by ceasing to send
740 * configure-requests.
742 if (len == 0 && all_rejected[f->unit])
745 if (go->deflate && len >= CILEN_DEFLATE
746 && p[0] == (go->deflate_correct? CI_DEFLATE: CI_DEFLATE_DRAFT)
747 && p[1] == CILEN_DEFLATE) {
748 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
749 || p[3] != DEFLATE_CHK_SEQUENCE)
750 return 0; /* Rej is bad */
751 if (go->deflate_correct)
752 try.deflate_correct = 0;
754 try.deflate_draft = 0;
756 len -= CILEN_DEFLATE;
757 if (go->deflate_correct && go->deflate_draft
758 && len >= CILEN_DEFLATE && p[0] == 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 try.deflate_draft = 0;
765 len -= CILEN_DEFLATE;
767 if (!try.deflate_correct && !try.deflate_draft)
770 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
771 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
772 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
774 try.bsd_compress = 0;
775 p += CILEN_BSD_COMPRESS;
776 len -= CILEN_BSD_COMPRESS;
778 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
779 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
781 p += CILEN_PREDICTOR_1;
782 len -= CILEN_PREDICTOR_1;
784 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
785 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
787 p += CILEN_PREDICTOR_2;
788 len -= CILEN_PREDICTOR_2;
794 if (f->state != OPENED)
801 * ccp_reqci - processed a received configure-request.
802 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
806 ccp_reqci(f, p, lenp, dont_nak)
812 int ret, newret, res;
814 int len, clen, type, nb;
815 ccp_options *ho = &ccp_hisoptions[f->unit];
816 ccp_options *ao = &ccp_allowoptions[f->unit];
822 memset(ho, 0, sizeof(ccp_options));
823 ho->method = (len > 0)? p[0]: -1;
827 if (len < 2 || p[1] < 2 || p[1] > len) {
838 case CI_DEFLATE_DRAFT:
839 if (!ao->deflate || clen != CILEN_DEFLATE
840 || (!ao->deflate_correct && type == CI_DEFLATE)
841 || (!ao->deflate_draft && type == CI_DEFLATE_DRAFT)) {
847 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
848 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
849 || p[3] != DEFLATE_CHK_SEQUENCE
850 || nb > ao->deflate_size || nb < DEFLATE_MIN_SIZE) {
853 p[2] = DEFLATE_MAKE_OPT(ao->deflate_size);
854 p[3] = DEFLATE_CHK_SEQUENCE;
855 /* fall through to test this #bits below */
861 * Check whether we can do Deflate with the window
862 * size they want. If the window is too big, reduce
863 * it until the kernel can cope and nak with that.
864 * We only check this for the first option.
868 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
870 break; /* it's OK now */
871 if (res < 0 || nb == DEFLATE_MIN_SIZE || dont_nak) {
873 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
878 p[2] = DEFLATE_MAKE_OPT(nb);
883 case CI_BSD_COMPRESS:
884 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
889 ho->bsd_compress = 1;
890 ho->bsd_bits = nb = BSD_NBITS(p[2]);
891 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
892 || nb > ao->bsd_bits || nb < BSD_MIN_BITS) {
895 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, ao->bsd_bits);
896 /* fall through to test this #bits below */
902 * Check whether we can do BSD-Compress with the code
903 * size they want. If the code size is too big, reduce
904 * it until the kernel can cope and nak with that.
905 * We only check this for the first option.
909 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
912 if (res < 0 || nb == BSD_MIN_BITS || dont_nak) {
914 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
920 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
926 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
933 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
939 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
946 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
956 if (newret == CONFNAK && dont_nak)
958 if (!(newret == CONFACK || (newret == CONFNAK && ret == CONFREJ))) {
959 /* we're returning this option */
960 if (newret == CONFREJ && ret == CONFNAK)
964 BCOPY(p, retp, clen);
972 if (ret != CONFACK) {
973 if (ret == CONFREJ && *lenp == retp - p0)
974 all_rejected[f->unit] = 1;
982 * Make a string name for a compression method (or 2).
985 method_name(opt, opt2)
986 ccp_options *opt, *opt2;
988 static char result[64];
990 if (!ANY_COMPRESS(*opt))
992 switch (opt->method) {
994 case CI_DEFLATE_DRAFT:
995 if (opt2 != NULL && opt2->deflate_size != opt->deflate_size)
996 sprintf(result, "Deflate%s (%d/%d)",
997 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
998 opt->deflate_size, opt2->deflate_size);
1000 sprintf(result, "Deflate%s (%d)",
1001 (opt->method == CI_DEFLATE_DRAFT? "(old#)": ""),
1004 case CI_BSD_COMPRESS:
1005 if (opt2 != NULL && opt2->bsd_bits != opt->bsd_bits)
1006 sprintf(result, "BSD-Compress (%d/%d)", opt->bsd_bits,
1009 sprintf(result, "BSD-Compress (%d)", opt->bsd_bits);
1011 case CI_PREDICTOR_1:
1012 return "Predictor 1";
1013 case CI_PREDICTOR_2:
1014 return "Predictor 2";
1016 sprintf(result, "Method %d", opt->method);
1022 * CCP has come up - inform the kernel driver and log a message.
1028 ccp_options *go = &ccp_gotoptions[f->unit];
1029 ccp_options *ho = &ccp_hisoptions[f->unit];
1032 ccp_flags_set(f->unit, 1, 1);
1033 if (ANY_COMPRESS(*go)) {
1034 if (ANY_COMPRESS(*ho)) {
1035 if (go->method == ho->method) {
1036 syslog(LOG_NOTICE, "%s compression enabled",
1037 method_name(go, ho));
1039 strlcpy(method1, sizeof(method1), method_name(go, NULL));
1040 syslog(LOG_NOTICE, "%s / %s compression enabled",
1041 method1, method_name(ho, NULL));
1044 syslog(LOG_NOTICE, "%s receive compression enabled",
1045 method_name(go, NULL));
1046 } else if (ANY_COMPRESS(*ho))
1047 syslog(LOG_NOTICE, "%s transmit compression enabled",
1048 method_name(ho, NULL));
1052 * CCP has gone down - inform the kernel driver.
1058 if (ccp_localstate[f->unit] & RACK_PENDING)
1059 UNTIMEOUT(ccp_rack_timeout, f);
1060 ccp_localstate[f->unit] = 0;
1061 ccp_flags_set(f->unit, 1, 0);
1065 * Print the contents of a CCP packet.
1067 static char *ccp_codenames[] = {
1068 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1069 "TermReq", "TermAck", "CodeRej",
1070 NULL, NULL, NULL, NULL, NULL, NULL,
1071 "ResetReq", "ResetAck",
1075 ccp_printpkt(p, plen, printer, arg)
1078 void (*printer) __P((void *, char *, ...));
1081 u_char *p0, *optend;
1086 if (plen < HEADERLEN)
1090 len = (p[2] << 8) + p[3];
1091 if (len < HEADERLEN || len > plen)
1094 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
1095 && ccp_codenames[code-1] != NULL)
1096 printer(arg, " %s", ccp_codenames[code-1]);
1098 printer(arg, " code=0x%x", code);
1099 printer(arg, " id=0x%x", id);
1108 /* print list of possible compression methods */
1112 if (optlen < 2 || optlen > len)
1116 optend = p + optlen;
1119 case CI_DEFLATE_DRAFT:
1120 if (optlen >= CILEN_DEFLATE) {
1121 printer(arg, "deflate%s %d",
1122 (code == CI_DEFLATE_DRAFT? "(old#)": ""),
1123 DEFLATE_SIZE(p[2]));
1124 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
1125 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
1126 if (p[3] != DEFLATE_CHK_SEQUENCE)
1127 printer(arg, " check %d", p[3]);
1131 case CI_BSD_COMPRESS:
1132 if (optlen >= CILEN_BSD_COMPRESS) {
1133 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
1135 p += CILEN_BSD_COMPRESS;
1138 case CI_PREDICTOR_1:
1139 if (optlen >= CILEN_PREDICTOR_1) {
1140 printer(arg, "predictor 1");
1141 p += CILEN_PREDICTOR_1;
1144 case CI_PREDICTOR_2:
1145 if (optlen >= CILEN_PREDICTOR_2) {
1146 printer(arg, "predictor 2");
1147 p += CILEN_PREDICTOR_2;
1152 printer(arg, " %.2x", *p++);
1159 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1160 print_string(p, len, printer, arg);
1167 /* dump out the rest of the packet in hex */
1169 printer(arg, " %.2x", *p++);
1175 * We have received a packet that the decompressor failed to
1176 * decompress. Here we would expect to issue a reset-request, but
1177 * Motorola has a patent on resetting the compressor as a result of
1178 * detecting an error in the decompressed data after decompression.
1179 * (See US patent 5,130,993; international patent publication number
1180 * WO 91/10289; Australian patent 73296/91.)
1182 * So we ask the kernel whether the error was detected after
1183 * decompression; if it was, we take CCP down, thus disabling
1184 * compression :-(, otherwise we issue the reset-request.
1187 ccp_datainput(unit, pkt, len)
1195 if (f->state == OPENED) {
1196 if (ccp_fatal_error(unit)) {
1198 * Disable compression by taking CCP down.
1200 syslog(LOG_ERR, "Lost compression sync: disabling compression");
1201 ccp_close(unit, "Lost compression sync");
1204 * Send a reset-request to reset the peer's compressor.
1205 * We don't do that if we are still waiting for an
1206 * acknowledgement to a previous reset-request.
1208 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
1209 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
1210 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1211 ccp_localstate[f->unit] |= RACK_PENDING;
1213 ccp_localstate[f->unit] |= RREQ_REPEAT;
1219 * Timeout waiting for reset-ack.
1222 ccp_rack_timeout(arg)
1227 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
1228 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
1229 TIMEOUT(ccp_rack_timeout, f, RACKTIMEOUT);
1230 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
1232 ccp_localstate[f->unit] &= ~RACK_PENDING;