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.13 1995/12/18 03:43:40 paulus Exp $";
33 #include <sys/ioctl.h>
34 #include <net/ppp-comp.h>
40 struct protent ccp_protent = {
41 PPP_CCP, ccp_init, ccp_input, ccp_protrej,
42 ccp_lowerup, ccp_lowerdown, ccp_open, ccp_close,
43 ccp_printpkt, NULL, 1, "CCP"
47 ccp_options ccp_wantoptions[NUM_PPP]; /* what to request the peer to use */
48 ccp_options ccp_gotoptions[NUM_PPP]; /* what the peer agreed to do */
49 ccp_options ccp_allowoptions[NUM_PPP]; /* what we'll agree to do */
50 ccp_options ccp_hisoptions[NUM_PPP]; /* what we agreed to do */
53 * Callbacks for fsm code.
55 static void ccp_resetci __P((fsm *));
56 static int ccp_cilen __P((fsm *));
57 static void ccp_addci __P((fsm *, u_char *, int *));
58 static int ccp_ackci __P((fsm *, u_char *, int));
59 static int ccp_nakci __P((fsm *, u_char *, int));
60 static int ccp_rejci __P((fsm *, u_char *, int));
61 static int ccp_reqci __P((fsm *, u_char *, int *, int));
62 static void ccp_up __P((fsm *));
63 static void ccp_down __P((fsm *));
64 static int ccp_extcode __P((fsm *, int, int, u_char *, int));
65 static void ccp_rack_timeout __P(());
67 static fsm_callbacks ccp_callbacks = {
86 * Do we want / did we get any compression?
88 #define ANY_COMPRESS(opt) ((opt).deflate || (opt).bsd_compress \
89 || (opt).predictor_1 || (opt).predictor_2)
92 * Local state (mainly for handling reset-reqs and reset-acks).
94 static int ccp_localstate[NUM_PPP];
95 #define RACK_PENDING 1 /* waiting for reset-ack */
96 #define RREQ_REPEAT 2 /* send another reset-req if no reset-ack */
98 #define RACKTIMEOUT 1 /* second */
100 static int all_rejected[NUM_PPP]; /* we rejected all peer's options */
103 * ccp_init - initialize CCP.
109 fsm *f = &ccp_fsm[unit];
112 f->protocol = PPP_CCP;
113 f->callbacks = &ccp_callbacks;
116 memset(&ccp_wantoptions[unit], 0, sizeof(ccp_options));
117 memset(&ccp_gotoptions[unit], 0, sizeof(ccp_options));
118 memset(&ccp_allowoptions[unit], 0, sizeof(ccp_options));
119 memset(&ccp_hisoptions[unit], 0, sizeof(ccp_options));
121 ccp_wantoptions[0].bsd_compress = 1;
122 ccp_wantoptions[0].bsd_bits = 12; /* default value */
124 ccp_allowoptions[0].bsd_compress = 1;
125 ccp_allowoptions[0].bsd_bits = BSD_MAX_BITS;
127 ccp_allowoptions[0].predictor_1 = 1;
131 * ccp_open - CCP is allowed to come up.
137 fsm *f = &ccp_fsm[unit];
139 if (f->state != OPENED)
140 ccp_flags_set(unit, 1, 0);
141 if (!ANY_COMPRESS(ccp_wantoptions[unit]))
142 f->flags |= OPT_SILENT;
147 * ccp_close - Terminate CCP.
150 ccp_close(unit, reason)
154 ccp_flags_set(unit, 0, 0);
155 fsm_close(&ccp_fsm[unit], reason);
159 * ccp_lowerup - we may now transmit CCP packets.
165 fsm_lowerup(&ccp_fsm[unit]);
169 * ccp_lowerdown - we may not transmit CCP packets.
175 fsm_lowerdown(&ccp_fsm[unit]);
179 * ccp_input - process a received CCP packet.
182 ccp_input(unit, p, len)
187 fsm *f = &ccp_fsm[unit];
191 * Check for a terminate-request so we can print a message.
194 fsm_input(f, p, len);
195 if (oldstate == OPENED && p[0] == TERMREQ && f->state != OPENED)
196 syslog(LOG_NOTICE, "Compression disabled by peer.");
199 * If we get a terminate-ack and we're not asking for compression,
202 if (oldstate == REQSENT && p[0] == TERMACK
203 && !ANY_COMPRESS(ccp_gotoptions[unit]))
204 ccp_close(unit, "No compression negotiated");
208 * Handle a CCP-specific code.
211 ccp_extcode(f, code, id, p, len)
219 if (f->state != OPENED)
221 /* send a reset-ack, which the transmitter will see and
222 reset its compression state. */
223 fsm_sdata(f, CCP_RESETACK, id, NULL, 0);
227 if (ccp_localstate[f->unit] & RACK_PENDING && id == f->reqid) {
228 ccp_localstate[f->unit] &= ~(RACK_PENDING | RREQ_REPEAT);
229 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
241 * ccp_protrej - peer doesn't talk CCP.
247 ccp_flags_set(unit, 0, 0);
248 fsm_lowerdown(&ccp_fsm[unit]);
252 * ccp_resetci - initialize at start of negotiation.
259 ccp_options *go = &ccp_gotoptions[f->unit];
262 *go = ccp_wantoptions[f->unit];
263 all_rejected[f->unit] = 0;
266 * Check whether the kernel knows about the various
267 * compression methods we might request.
269 if (go->bsd_compress) {
270 opt_buf[0] = CI_BSD_COMPRESS;
271 opt_buf[1] = CILEN_BSD_COMPRESS;
272 opt_buf[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, BSD_MIN_BITS);
273 if (ccp_test(f->unit, opt_buf, CILEN_BSD_COMPRESS, 0) <= 0)
274 go->bsd_compress = 0;
277 opt_buf[0] = CI_DEFLATE;
278 opt_buf[1] = CILEN_DEFLATE;
279 opt_buf[2] = DEFLATE_MAKE_OPT(DEFLATE_MIN_SIZE);
280 opt_buf[3] = DEFLATE_CHK_SEQUENCE;
281 if (ccp_test(f->unit, opt_buf, CILEN_DEFLATE, 0) <= 0)
284 if (go->predictor_1) {
285 opt_buf[0] = CI_PREDICTOR_1;
286 opt_buf[1] = CILEN_PREDICTOR_1;
287 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_1, 0) <= 0)
290 if (go->predictor_2) {
291 opt_buf[0] = CI_PREDICTOR_2;
292 opt_buf[1] = CILEN_PREDICTOR_2;
293 if (ccp_test(f->unit, opt_buf, CILEN_PREDICTOR_2, 0) <= 0)
299 * ccp_cilen - Return total length of our configuration info.
305 ccp_options *go = &ccp_gotoptions[f->unit];
307 return (go->bsd_compress? CILEN_BSD_COMPRESS: 0)
308 + (go->deflate? CILEN_DEFLATE: 0)
309 + (go->predictor_1? CILEN_PREDICTOR_1: 0)
310 + (go->predictor_2? CILEN_PREDICTOR_2: 0);
314 * ccp_addci - put our requests in a packet.
317 ccp_addci(f, p, lenp)
323 ccp_options *go = &ccp_gotoptions[f->unit];
327 * Add the compression types that we can receive, in decreasing
328 * preference order. Get the kernel to allocate the first one
329 * in case it gets Acked.
333 p[1] = CILEN_DEFLATE;
334 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
335 p[3] = DEFLATE_CHK_SEQUENCE;
337 res = ccp_test(f->unit, p, CILEN_DEFLATE, 0);
342 if (res < 0 || go->deflate_size <= DEFLATE_MIN_SIZE) {
347 p[2] = DEFLATE_MAKE_OPT(go->deflate_size);
350 if (go->bsd_compress) {
351 p[0] = CI_BSD_COMPRESS;
352 p[1] = CILEN_BSD_COMPRESS;
353 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
355 p += CILEN_BSD_COMPRESS; /* not the first option */
358 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 0);
360 p += CILEN_BSD_COMPRESS;
363 if (res < 0 || go->bsd_bits <= BSD_MIN_BITS) {
364 go->bsd_compress = 0;
368 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits);
372 /* XXX Should Predictor 2 be preferable to Predictor 1? */
373 if (go->predictor_1) {
374 p[0] = CI_PREDICTOR_1;
375 p[1] = CILEN_PREDICTOR_1;
376 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_1, 0) <= 0) {
379 p += CILEN_PREDICTOR_1;
382 if (go->predictor_2) {
383 p[0] = CI_PREDICTOR_2;
384 p[1] = CILEN_PREDICTOR_2;
385 if (p == p0 && ccp_test(f->unit, p, CILEN_PREDICTOR_2, 0) <= 0) {
388 p += CILEN_PREDICTOR_2;
396 * ccp_ackci - process a received configure-ack, and return
397 * 1 iff the packet was OK.
405 ccp_options *go = &ccp_gotoptions[f->unit];
409 if (len < CILEN_DEFLATE
410 || p[0] != CI_DEFLATE || p[1] != CILEN_DEFLATE
411 || p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
412 || p[3] != DEFLATE_CHK_SEQUENCE)
415 len -= CILEN_DEFLATE;
416 /* XXX Cope with first/fast ack */
420 if (go->bsd_compress) {
421 if (len < CILEN_BSD_COMPRESS
422 || p[0] != CI_BSD_COMPRESS || p[1] != CILEN_BSD_COMPRESS
423 || p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
425 p += CILEN_BSD_COMPRESS;
426 len -= CILEN_BSD_COMPRESS;
427 /* XXX Cope with first/fast ack */
428 if (p == p0 && len == 0)
431 if (go->predictor_1) {
432 if (len < CILEN_PREDICTOR_1
433 || p[0] != CI_PREDICTOR_1 || p[1] != CILEN_PREDICTOR_1)
435 p += CILEN_PREDICTOR_1;
436 len -= CILEN_PREDICTOR_1;
437 /* XXX Cope with first/fast ack */
438 if (p == p0 && len == 0)
441 if (go->predictor_2) {
442 if (len < CILEN_PREDICTOR_2
443 || p[0] != CI_PREDICTOR_2 || p[1] != CILEN_PREDICTOR_2)
445 p += CILEN_PREDICTOR_2;
446 len -= CILEN_PREDICTOR_2;
447 /* XXX Cope with first/fast ack */
448 if (p == p0 && len == 0)
458 * ccp_nakci - process received configure-nak.
459 * Returns 1 iff the nak was OK.
467 ccp_options *go = &ccp_gotoptions[f->unit];
468 ccp_options no; /* options we've seen already */
469 ccp_options try; /* options to ask for next time */
471 memset(&no, 0, sizeof(no));
474 if (go->deflate && len >= CILEN_DEFLATE
475 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
478 * Peer wants us to use a different code size or something.
479 * Stop asking for Deflate if we don't understand his suggestion.
481 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
482 || DEFLATE_SIZE(p[2]) < DEFLATE_MIN_SIZE
483 || p[3] != DEFLATE_CHK_SEQUENCE)
485 else if (DEFLATE_SIZE(p[2]) < go->deflate_size)
486 go->deflate_size = DEFLATE_SIZE(p[2]);
488 len -= CILEN_DEFLATE;
491 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
492 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
495 * Peer wants us to use a different number of bits
496 * or a different version.
498 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION)
499 try.bsd_compress = 0;
500 else if (BSD_NBITS(p[2]) < go->bsd_bits)
501 try.bsd_bits = BSD_NBITS(p[2]);
502 p += CILEN_BSD_COMPRESS;
503 len -= CILEN_BSD_COMPRESS;
507 * Predictor-1 and 2 have no options, so they can't be Naked.
509 * XXX What should we do with any remaining options?
515 if (f->state != OPENED)
521 * ccp_rejci - reject some of our suggested compression methods.
529 ccp_options *go = &ccp_gotoptions[f->unit];
530 ccp_options try; /* options to request next time */
535 * Cope with empty configure-rejects by ceasing to send
536 * configure-requests.
538 if (len == 0 && all_rejected[f->unit])
541 if (go->deflate && len >= CILEN_DEFLATE
542 && p[0] == CI_DEFLATE && p[1] == CILEN_DEFLATE) {
543 if (p[2] != DEFLATE_MAKE_OPT(go->deflate_size)
544 || p[3] != DEFLATE_CHK_SEQUENCE)
545 return 0; /* Rej is bad */
548 len -= CILEN_DEFLATE;
550 if (go->bsd_compress && len >= CILEN_BSD_COMPRESS
551 && p[0] == CI_BSD_COMPRESS && p[1] == CILEN_BSD_COMPRESS) {
552 if (p[2] != BSD_MAKE_OPT(BSD_CURRENT_VERSION, go->bsd_bits))
554 try.bsd_compress = 0;
555 p += CILEN_BSD_COMPRESS;
556 len -= CILEN_BSD_COMPRESS;
558 if (go->predictor_1 && len >= CILEN_PREDICTOR_1
559 && p[0] == CI_PREDICTOR_1 && p[1] == CILEN_PREDICTOR_1) {
561 p += CILEN_PREDICTOR_1;
562 len -= CILEN_PREDICTOR_1;
564 if (go->predictor_2 && len >= CILEN_PREDICTOR_2
565 && p[0] == CI_PREDICTOR_2 && p[1] == CILEN_PREDICTOR_2) {
567 p += CILEN_PREDICTOR_2;
568 len -= CILEN_PREDICTOR_2;
574 if (f->state != OPENED)
581 * ccp_reqci - processed a received configure-request.
582 * Returns CONFACK, CONFNAK or CONFREJ and the packet modified
586 ccp_reqci(f, p, lenp, dont_nak)
592 int ret, newret, res;
594 int len, clen, type, nb;
595 ccp_options *ho = &ccp_hisoptions[f->unit];
596 ccp_options *ao = &ccp_allowoptions[f->unit];
602 memset(ho, 0, sizeof(ccp_options));
606 if (len < 2 || p[1] < 2 || p[1] > len) {
617 if (!ao->deflate || clen != CILEN_DEFLATE) {
623 ho->deflate_size = nb = DEFLATE_SIZE(p[2]);
624 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL
625 || p[3] != DEFLATE_CHK_SEQUENCE
626 || nb > ao->deflate_size) {
628 nb = ao->deflate_size;
631 * Check whether we can do Deflate with the window
632 * size they want. If the window is too big, reduce
633 * it until the kernel can cope and nak with that.
636 if (nb < DEFLATE_MIN_SIZE) {
638 p[2] = DEFLATE_MAKE_OPT(ho->deflate_size);
641 p[2] = DEFLATE_MAKE_OPT(nb);
642 res = ccp_test(f->unit, p, CILEN_DEFLATE, 1);
652 if (newret == CONFNAK && !dont_nak) {
653 if (nb >= DEFLATE_MIN_SIZE) {
654 p[2] = DEFLATE_MAKE_OPT(nb);
655 p[3] = DEFLATE_CHK_SEQUENCE;
662 case CI_BSD_COMPRESS:
663 if (!ao->bsd_compress || clen != CILEN_BSD_COMPRESS) {
668 ho->bsd_compress = 1;
669 ho->bsd_bits = nb = BSD_NBITS(p[2]);
670 if (BSD_VERSION(p[2]) != BSD_CURRENT_VERSION
671 || nb > ao->bsd_bits) {
676 * Check whether we can do BSD_Compress with the code
677 * size they want. If the code size is too big, reduce
678 * it until the kernel can cope and nak with that.
681 if (nb < BSD_MIN_BITS) {
683 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION,
687 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
688 res = ccp_test(f->unit, p, CILEN_BSD_COMPRESS, 1);
698 if (newret == CONFNAK && !dont_nak) {
699 if (nb >= BSD_MIN_BITS) {
700 p[2] = BSD_MAKE_OPT(BSD_CURRENT_VERSION, nb);
708 if (!ao->predictor_1 || clen != CILEN_PREDICTOR_1) {
714 if (ccp_test(f->unit, p, CILEN_PREDICTOR_1, 1) <= 0) {
720 if (!ao->predictor_2 || clen != CILEN_PREDICTOR_2) {
726 if (ccp_test(f->unit, p, CILEN_PREDICTOR_2, 1) <= 0) {
736 if (newret == CONFNAK && dont_nak)
738 if (!(newret == CONFACK || newret == CONFNAK && ret == CONFREJ)) {
739 /* we're returning this option */
740 if (newret == CONFREJ && ret == CONFNAK)
744 BCOPY(p, retp, clen);
752 if (ret != CONFACK) {
753 if (ret == CONFREJ && *lenp == retp - p0)
754 all_rejected[f->unit] = 1;
762 * CCP has come up - inform the kernel driver.
768 ccp_options *go = &ccp_gotoptions[f->unit];
769 ccp_options *ho = &ccp_hisoptions[f->unit];
771 ccp_flags_set(f->unit, 1, 1);
772 if (ANY_COMPRESS(*go) || ANY_COMPRESS(*ho))
773 syslog(LOG_NOTICE, "%s enabled",
774 ANY_COMPRESS(*go)? ANY_COMPRESS(*ho)? "Compression":
775 "Receive compression": "Transmit compression");
779 * CCP has gone down - inform the kernel driver.
785 if (ccp_localstate[f->unit] & RACK_PENDING)
786 UNTIMEOUT(ccp_rack_timeout, (caddr_t) f);
787 ccp_localstate[f->unit] = 0;
788 ccp_flags_set(f->unit, 1, 0);
792 * Print the contents of a CCP packet.
794 char *ccp_codenames[] = {
795 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
796 "TermReq", "TermAck", "CodeRej",
797 NULL, NULL, NULL, NULL, NULL, NULL,
798 "ResetReq", "ResetAck",
802 ccp_printpkt(p, plen, printer, arg)
805 void (*printer) __P((void *, char *, ...));
813 if (plen < HEADERLEN)
817 len = (p[2] << 8) + p[3];
818 if (len < HEADERLEN || len > plen)
821 if (code >= 1 && code <= sizeof(ccp_codenames) / sizeof(char *)
822 && ccp_codenames[code-1] != NULL)
823 printer(arg, " %s", ccp_codenames[code-1]);
825 printer(arg, " code=0x%x", code);
826 printer(arg, " id=0x%x", id);
835 /* print list of possible compression methods */
839 if (optlen < 2 || optlen > len)
846 if (optlen >= CILEN_DEFLATE) {
847 printer(arg, "deflate %d", DEFLATE_SIZE(p[2]));
848 if (DEFLATE_METHOD(p[2]) != DEFLATE_METHOD_VAL)
849 printer(arg, " method %d", DEFLATE_METHOD(p[2]));
850 if (p[3] != DEFLATE_CHK_SEQUENCE)
851 printer(arg, " check %d", p[3]);
855 case CI_BSD_COMPRESS:
856 if (optlen >= CILEN_BSD_COMPRESS) {
857 printer(arg, "bsd v%d %d", BSD_VERSION(p[2]),
859 p += CILEN_BSD_COMPRESS;
863 if (optlen >= CILEN_PREDICTOR_1) {
864 printer(arg, "predictor 1");
865 p += CILEN_PREDICTOR_1;
869 if (optlen >= CILEN_PREDICTOR_2) {
870 printer(arg, "predictor 2");
871 p += CILEN_PREDICTOR_2;
876 printer(arg, " %.2x", *p++);
882 /* dump out the rest of the packet in hex */
884 printer(arg, " %.2x", *p++);
890 * We have received a packet that the decompressor failed to
891 * decompress. Here we would expect to issue a reset-request, but
892 * Motorola has a patent on resetting the compressor as a result of
893 * detecting an error in the decompressed data after decompression.
894 * (See US patent 5,130,993; international patent publication number
895 * WO 91/10289; Australian patent 73296/91.)
897 * So we ask the kernel whether the error was detected after
898 * decompression; if it was, we take CCP down, thus disabling
899 * compression :-(, otherwise we issue the reset-request.
902 ccp_datainput(unit, pkt, len)
910 if (f->state == OPENED) {
911 if (ccp_fatal_error(unit)) {
913 * Disable compression by taking CCP down.
915 syslog(LOG_ERR, "Lost compression sync: disabling compression");
916 ccp_close(unit, "Lost compression sync");
919 * Send a reset-request to reset the peer's compressor.
920 * We don't do that if we are still waiting for an
921 * acknowledgement to a previous reset-request.
923 if (!(ccp_localstate[f->unit] & RACK_PENDING)) {
924 fsm_sdata(f, CCP_RESETREQ, f->reqid = ++f->id, NULL, 0);
925 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
926 ccp_localstate[f->unit] |= RACK_PENDING;
928 ccp_localstate[f->unit] |= RREQ_REPEAT;
934 * Timeout waiting for reset-ack.
937 ccp_rack_timeout(arg)
940 fsm *f = (fsm *) arg;
942 if (f->state == OPENED && ccp_localstate[f->unit] & RREQ_REPEAT) {
943 fsm_sdata(f, CCP_RESETREQ, f->reqid, NULL, 0);
944 TIMEOUT(ccp_rack_timeout, (caddr_t) f, RACKTIMEOUT);
945 ccp_localstate[f->unit] &= ~RREQ_REPEAT;
947 ccp_localstate[f->unit] &= ~RACK_PENDING;