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.12 1995/10/27 03:42:52 paulus Exp $";
33 #include <sys/ioctl.h>
34 #include <net/ppp-comp.h>
41 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
42 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
43 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
44 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
47 * Callbacks for fsm code.
49 static void ccp_resetci __P((fsm *));
50 static int ccp_cilen __P((fsm *));
51 static void ccp_addci __P((fsm *, u_char *, int *));
52 static int ccp_ackci __P((fsm *, u_char *, int));
53 static int ccp_nakci __P((fsm *, u_char *, int));
54 static int ccp_rejci __P((fsm *, u_char *, int));
55 static int ccp_reqci __P((fsm *, u_char *, int *, int));
56 static void ccp_up __P((fsm *));
57 static void ccp_down __P((fsm *));
58 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
59 static void ccp_rack_timeout __P(());
61 static fsm_callbacks ccp_callbacks = {
80 * Do we want / did we get any compression?
82 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
83 || (opt).predictor_1 || (opt).predictor_2)
86 * Local state (mainly for handling reset-reqs and reset-acks).
88 static int ccp_localstate[NUM_PPP];
89 #define RACK_PENDING 1 /* waiting for reset-ack */
90 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
92 #define RACKTIMEOUT 1 /* second */
94 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
97 * ccp_init - initialize CCP.
103 fsm *f = &ccp_fsm[unit];
106 f->protocol = PPP_CCP;
107 f->callbacks = &ccp_callbacks;
110 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
111 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
112 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
113 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
115 ccp_wantoptions[0].bsd_compress = 1;
116 ccp_wantoptions[0].bsd_bits = 12; /* default value */
118 ccp_allowoptions[0].bsd_compress = 1;
119 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
123 * ccp_open - CCP is allowed to come up.
129 fsm *f = &ccp_fsm[unit];
131 if (f->state != OPENED)
132 ccp_flags_set(unit, 1, 0);
133 if (!ANY_COMPRESS(ccp_wantoptions[unit]))
134 f->flags |= OPT_SILENT;
139 * ccp_close - Terminate CCP.
145 ccp_flags_set(unit, 0, 0);
146 fsm_close(&ccp_fsm[unit]);
150 * ccp_lowerup - we may now transmit CCP packets.
156 fsm_lowerup(&ccp_fsm[unit]);
160 * ccp_lowerdown - we may not transmit CCP packets.
166 fsm_lowerdown(&ccp_fsm[unit]);
170 * ccp_input - process a received CCP packet.
173 ccp_input(unit, p, len)
178 fsm *f = &ccp_fsm[unit];
182 * Check for a terminate-request so we can print a message.
185 fsm_input(f, p, len);
186 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
187 syslog(LOG_NOTICE, "Compression disabled by peer.");
190 * If we get a terminate-ack and we're not asking for compression,
193 if (oldstate == REQSENT && p[0] == TERMACK
194 && !ANY_COMPRESS(ccp_gotoptions[unit]))
199 * Handle a CCP-specific code.
202 ccp_extcode(f, code, id, p, len)
210 if (f->state != OPENED)
212 /* send a reset-ack, which the transmitter will see and
213 reset its compression state. */
214 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
218 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
219 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
220 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
232 * ccp_protrej - peer doesn't talk CCP.
238 ccp_flags_set(unit, 0, 0);
239 fsm_lowerdown(&ccp_fsm[unit]);
243 * ccp_resetci - initialize at start of negotiation.
250 ccp_options *go = &ccp_gotoptions[f->unit];
253 *go = ccp_wantoptions[f->unit];
254 all_rejected[f->unit] = 0;
257 * Check whether the kernel knows about the various
258 * compression methods we might request.
260 if (go->bsd_compress) {
261 opt_buf[0] = CI_BSD_COMPRESS;
262 opt_buf[1] = CILEN_BSD_COMPRESS;
263 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
264 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
265 go->bsd_compress = 0;
268 opt_buf[0] = CI_DEFLATE;
269 opt_buf[1] = CILEN_DEFLATE;
270 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
271 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
272 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
275 if (go->predictor_1) {
276 opt_buf[0] = CI_PREDICTOR_1;
277 opt_buf[1] = CILEN_PREDICTOR_1;
278 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
281 if (go->predictor_2) {
282 opt_buf[0] = CI_PREDICTOR_2;
283 opt_buf[1] = CILEN_PREDICTOR_2;
284 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
290 * ccp_cilen - Return total length of our configuration info.
296 ccp_options *go = &ccp_gotoptions[f->unit];
298 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
299 + (go->deflate? CILEN_DEFLATE: 0)
300 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
301 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
305 * ccp_addci - put our requests in a packet.
308 ccp_addci(f, p, lenp)
314 ccp_options *go = &ccp_gotoptions[f->unit];
318 * Add the compression types that we can receive, in decreasing
319 * preference order. Get the kernel to allocate the first one
320 * in case it gets Acked.
324 p[1] = CILEN_DEFLATE;
325 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
326 p[3] = DEFLATE_CHK_SEQUENCE;
328 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
333 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
338 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
341 if (go->bsd_compress) {
342 p[0] = CI_BSD_COMPRESS;
343 p[1] = CILEN_BSD_COMPRESS;
344 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
346 p += CILEN_BSD_COMPRESS; /* not the first option */
349 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
351 p += CILEN_BSD_COMPRESS;
354 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
355 go->bsd_compress = 0;
359 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
363 /* XXX Should Predictor 2 be preferable to Predictor 1? */
364 if (go->predictor_1) {
365 p[0] = CI_PREDICTOR_1;
366 p[1] = CILEN_PREDICTOR_1;
367 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
370 p += CILEN_PREDICTOR_1;
373 if (go->predictor_2) {
374 p[0] = CI_PREDICTOR_2;
375 p[1] = CILEN_PREDICTOR_2;
376 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
379 p += CILEN_PREDICTOR_2;
387 * ccp_ackci - process a received configure-ack, and return
388 * 1 iff the packet was OK.
396 ccp_options *go = &ccp_gotoptions[f->unit];
400 if (len < CILEN_DEFLATE
401 || p[0] != CI_DEFLATE || p[1] != CILEN_DEFLATE
402 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
403 || p[3] != DEFLATE_CHK_SEQUENCE)
406 len -= CILEN_DEFLATE;
407 /* XXX Cope with first/fast ack */
411 if (go->bsd_compress) {
412 if (len < CILEN_BSD_COMPRESS
413 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
414 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
416 p += CILEN_BSD_COMPRESS;
417 len -= CILEN_BSD_COMPRESS;
418 /* XXX Cope with first/fast ack */
419 if (p == p0 && len == 0)
422 if (go->predictor_1) {
423 if (len < CILEN_PREDICTOR_1
424 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
426 /* XXX Cope with first/fast ack */
427 if (p == p0 && len == 0)
430 if (go->predictor_2) {
431 if (len < CILEN_PREDICTOR_2
432 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
434 /* XXX Cope with first/fast ack */
435 if (p == p0 && len == 0)
445 * ccp_nakci - process received configure-nak.
446 * Returns 1 iff the nak was OK.
454 ccp_options *go = &ccp_gotoptions[f->unit];
455 ccp_options no; /* options we've seen already */
456 ccp_options try; /* options to ask for next time */
458 memset(&no, 0, sizeof(no));
461 if (go->deflate && len >= CILEN_DEFLATE
462 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
465 * Peer wants us to use a different code size or something.
466 * Stop asking for Deflate if we don't understand his suggestion.
468 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
469 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
470 || p[3] != DEFLATE_CHK_SEQUENCE)
472 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
473 go->deflate_size = DEFLATE_SIZE(p[2]);
475 len -= CILEN_DEFLATE;
478 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
479 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
482 * Peer wants us to use a different number of bits
483 * or a different version.
485 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
486 try.bsd_compress = 0;
487 else if (BSD_NBITS(p[2]) < go->bsd_bits)
488 try.bsd_bits = BSD_NBITS(p[2]);
489 p += CILEN_BSD_COMPRESS;
490 len -= CILEN_BSD_COMPRESS;
494 * Predictor-1 and 2 have no options, so they can't be Naked.
496 * XXX What should we do with any remaining options?
502 if (f->state != OPENED)
508 * ccp_rejci - reject some of our suggested compression methods.
516 ccp_options *go = &ccp_gotoptions[f->unit];
517 ccp_options try; /* options to request next time */
522 * Cope with empty configure-rejects by ceasing to send
523 * configure-requests.
525 if (len == 0 && all_rejected[f->unit])
528 if (go->deflate && len >= CILEN_DEFLATE
529 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
530 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
531 || p[3] != DEFLATE_CHK_SEQUENCE)
532 return 0; /* Rej is bad */
535 len -= CILEN_DEFLATE;
537 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
538 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
539 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
541 try.bsd_compress = 0;
542 p += CILEN_BSD_COMPRESS;
543 len -= CILEN_BSD_COMPRESS;
545 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
546 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
548 p += CILEN_PREDICTOR_1;
549 len -= CILEN_PREDICTOR_1;
551 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
552 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
554 p += CILEN_PREDICTOR_2;
555 len -= CILEN_PREDICTOR_2;
561 if (f->state != OPENED)
568 * ccp_reqci - processed a received configure-request.
569 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
573 ccp_reqci(f, p, lenp, dont_nak)
579 int ret, newret, res;
581 int len, clen, type, nb;
582 ccp_options *ho = &ccp_hisoptions[f->unit];
583 ccp_options *ao = &ccp_allowoptions[f->unit];
589 memset(ho, 0, sizeof(ccp_options));
593 if (len < 2 || p[1] < 2 || p[1] > len) {
604 if (!ao->deflate || clen != CILEN_DEFLATE) {
610 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
611 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
612 || p[3] != DEFLATE_CHK_SEQUENCE
613 || nb > ao->deflate_size) {
615 nb = ao->deflate_size;
618 * Check whether we can do Deflate with the window
619 * size they want. If the window is too big, reduce
620 * it until the kernel can cope and nak with that.
623 if (nb < DEFLATE_MIN_SIZE) {
625 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
628 p[2] = DEFLATE_MAKE_OPT(nb);
629 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
639 if (newret == CONFNAK && !dont_nak) {
640 if (nb >= DEFLATE_MIN_SIZE) {
641 p[2] = DEFLATE_MAKE_OPT(nb);
642 p[3] = DEFLATE_CHK_SEQUENCE;
649 case CI_BSD_COMPRESS:
650 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
655 ho->bsd_compress = 1;
656 ho->bsd_bits = nb = BSD_NBITS(p[2]);
657 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
658 || nb > ao->bsd_bits) {
663 * Check whether we can do BSD_Compress with the code
664 * size they want. If the code size is too big, reduce
665 * it until the kernel can cope and nak with that.
668 if (nb < BSD_MIN_BITS) {
670 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
674 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
675 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
685 if (newret == CONFNAK && !dont_nak) {
686 if (nb >= BSD_MIN_BITS) {
687 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
695 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
701 if (ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
707 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
713 if (ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
723 if (newret == CONFNAK && dont_nak)
725 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
726 /* we're returning this option */
727 if (newret == CONFREJ && ret == CONFNAK)
731 BCOPY(p, retp, clen);
739 if (ret != CONFACK) {
740 if (ret == CONFREJ && *lenp == retp - p0)
741 all_rejected[f->unit] = 1;
749 * CCP has come up - inform the kernel driver.
755 ccp_options *go = &ccp_gotoptions[f->unit];
756 ccp_options *ho = &ccp_hisoptions[f->unit];
758 ccp_flags_set(f->unit, 1, 1);
759 if (ANY_COMPRESS(*go) || ANY_COMPRESS(*ho))
760 syslog(LOG_NOTICE, "%s enabled",
761 ANY_COMPRESS(*go)? ANY_COMPRESS(*ho)? "Compression":
762 "Receive compression": "Transmit compression");
766 * CCP has gone down - inform the kernel driver.
772 if (ccp_localstate[f->unit] & RACK_PENDING)
773 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
774 ccp_localstate[f->unit] = 0;
775 ccp_flags_set(f->unit, 1, 0);
779 * Print the contents of a CCP packet.
781 char *ccp_codenames[] = {
782 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
783 "TermReq", "TermAck", "CodeRej",
784 NULL, NULL, NULL, NULL, NULL, NULL,
785 "ResetReq", "ResetAck",
789 ccp_printpkt(p, plen, printer, arg)
792 void (*printer) __P((void *, char *, ...));
800 if (plen < HEADERLEN)
804 len = (p[2] << 8) + p[3];
805 if (len < HEADERLEN || len > plen)
808 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
809 && ccp_codenames[code-1] != NULL)
810 printer(arg, " %s", ccp_codenames[code-1]);
812 printer(arg, " code=0x%x", code);
813 printer(arg, " id=0x%x", id);
822 /* print list of possible compression methods */
826 if (optlen < 2 || optlen > len)
833 if (optlen >= CILEN_DEFLATE) {
834 printer(arg, "deflate %d", DEFLATE_SIZE(p[2]));
835 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
836 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
837 if (p[3] != DEFLATE_CHK_SEQUENCE)
838 printer(arg, " check %d", p[3]);
842 case CI_BSD_COMPRESS:
843 if (optlen >= CILEN_BSD_COMPRESS) {
844 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
846 p += CILEN_BSD_COMPRESS;
850 if (optlen >= CILEN_PREDICTOR_1) {
851 printer(arg, "predictor 1");
852 p += CILEN_PREDICTOR_1;
856 if (optlen >= CILEN_PREDICTOR_2) {
857 printer(arg, "predictor 2");
858 p += CILEN_PREDICTOR_2;
863 printer(arg, " %.2x", *p++);
869 /* dump out the rest of the packet in hex */
871 printer(arg, " %.2x", *p++);
877 * We have received a packet that the decompressor failed to
878 * decompress. Here we would expect to issue a reset-request, but
879 * Motorola has a patent on resetting the compressor as a result of
880 * detecting an error in the decompressed data after decompression.
881 * (See US patent 5,130,993; international patent publication number
882 * WO 91/10289; Australian patent 73296/91.)
884 * So we ask the kernel whether the error was detected after
885 * decompression; if it was, we take CCP down, thus disabling
886 * compression :-(, otherwise we issue the reset-request.
889 ccp_datainput(unit, pkt, len)
897 if (f->state == OPENED) {
898 if (ccp_fatal_error(unit)) {
900 * Disable compression by taking CCP down.
902 syslog(LOG_ERR, "Lost compression sync: disabling compression");
906 * Send a reset-request to reset the peer's compressor.
907 * We don't do that if we are still waiting for an
908 * acknowledgement to a previous reset-request.
910 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
911 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
912 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
913 ccp_localstate[f->unit] |= RACK_PENDING;
915 ccp_localstate[f->unit] |= RREQ_REPEAT;
921 * Timeout waiting for reset-ack.
924 ccp_rack_timeout(arg)
927 fsm *f = (fsm *) arg;
929 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
930 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
931 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
932 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
934 ccp_localstate[f->unit] &= ~RACK_PENDING;